import React from "react";
import { inject, observer } from "mobx-react";
import { toJS } from "mobx";
import {
  WeaLocaleProvider,
  WeaRightMenu,
  WeaFormItem,
  WeaDialog,
  WeaSearchGroup,
} from "ecCom";
import {WeaSwitch} from "comsMobx";
import {
  onCheck,
} from "../../util/IntegrationUtil";
import {Button} from 'antd';

const {getLabel} = WeaLocaleProvider;

@inject("coremailStore")
@observer
export default class AddOrEdit extends React.Component {

  getCondition = (form, datas=[]) => {
    const {isFormInit} = form;
    let group = [];

    isFormInit && datas && datas.forEach((c, index) => {
      let items = [];
      c.items && c.items.forEach(fields => {
        items.push({
          com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@355b1a@integrationCoremailEditForm${fields.domkey[0]}`}
            label={`${fields.label}`}
            labelCol={{ span: `${fields.labelcol}` }}
            wrapperCol={{ span: `${fields.fieldcol}` }}
            error={form.getError(fields)}
            tipPosition={"bottom"}
          >
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@ry4rpw@integrationCoremailEditForm${fields.domkey[0]}`} fieldConfig={fields} form={form} />
          </WeaFormItem>),
          colSpan: 1
        })
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ungpwb@integrationCoremailEditForm${index}`} col={1} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} center />)
    });
    return group;
  }

  onMenuClick = () => {
    const {coremailStore} = this.props;
    const {newForm, getCoreMailSaveOperation} = coremailStore;

    onCheck(newForm, getCoreMailSaveOperation);
  }

  onTestDatas=()=>{
    const {coremailStore} = this.props;
    const {status, newForm, testCoreMail} = coremailStore;
    const {operateIndex} = status;

    onCheck(newForm, () => {
      let params = Object.assign({}, newForm.getFormParams(), {coremailid: operateIndex,operate:'test',isFormEdit:'1'});
      testCoreMail(params);
    });
  }

  render() {
    const {
      coremailStore,
    } = this.props, {
      status, 
      newForm, 
      flag,
    } = coremailStore, {
      coremailDialogVisible, 
      newFormCondition, 
      operateIndex,
    } = status;
    
    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@9bunac`}
        visible={coremailDialogVisible}
        title={operateIndex ? getLabel('506091','编辑CoreMail集成') : getLabel('506014','新建CoreMail集成')}
        icon={'icon-coms-integration'}
        iconBgcolor={'#1a57a0'}
        style={{width:800, height:600}}
        hasScroll
        loading={!flag}
        onCancel={v => coremailStore.setStatus('coremailDialogVisible', false)}
        buttons={[<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5grw6l@integrationCoremailEditButtonSaveBtn`} type="primary" onClick={() => this.onMenuClick()}>{getLabel(30986, '保存')}</Button>,
		      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@4aijen@integrationCoremailEditButtonTestBtn`} type="primary" onClick={() => this.onTestDatas()}>{getLabel(25496, '测试')}</Button>
		    ]}
        moreBtn={{
          datas: [{
            key: 'BTN_SAVE',
            icon: <i className='icon-coms-Preservation'/>,
            content : getLabel(30986, '保存'),
            onClick : this.onMenuClick
          }],
          collectParams: []
        }}
      >
        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@8zuu4u`}
          datas={[{
            key: 'BTN_SAVE',
            icon: <i className='icon-coms-Preservation'/>,
            content : getLabel(30986, '保存'),
            onClick : this.onMenuClick
          }]}
          collectParams={[]}
        >
          {
            flag 
              ? this.getCondition(newForm, newFormCondition)
              : null
          }
        </WeaRightMenu>
      </WeaDialog>
    );
  }
}
