import React from "react";
import {inject, observer} from "mobx-react";
import { Button} from "antd";
import {checkTableFieldValueUnique}  from "../../apis/CommonService";
import {WeaSwitch} from 'comsMobx';
import {toJS} from "mobx";
import {
  WeaDialog,
  WeaLocaleProvider,
  WeaTools,
  WeaRightMenu,
  WeaSearchGroup,
  WeaInput,
  WeaFormItem,
} from "ecCom";

const { getKey } = WeaTools;
const getLabel = WeaLocaleProvider.getLabel;
const store = ["archiveStore"];

@inject(...store)
@observer
export default class NewForm extends React.Component {
  getRightMenu = () => {
    return [{
      key: 'BTN_SAVE',
      icon: <i className="icon-coms-Preservation" />,
      content: `${getLabel('86', '保存')}`,
      onClick: () => this.onRightMenuClick('BTN_SAVE')
    }]
  }

  onRightMenuClick = (type) => {
    const {archiveStore} = this.props;
    switch(type) {
      case 'BTN_SAVE':
        archiveStore.getArchiveConfigSchemeOperationXML();
        break;
    }
  }

  render() {
    const {archiveStore} = this.props;
    const { settingRegisterXMLForm } = archiveStore;
    const status = toJS(archiveStore.status);
    let title=getLabel('31691,125760', '注册XML方案');
    if(archiveStore.settingRegisterID!=""){
      title=getLabel('26473,125760', '编辑XML方案');
    }
    return <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@72vqh3`}
      title={title}
      onCancel={()=> archiveStore.setState({XMLDialogVisible: false})}
      visible={status.XMLDialogVisible}
      style={{width: 900, height: 600}}
      hasScroll
      moduleName="integration"
      buttons={[
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3lldz6@integrationWorkflowarchiveXmlButtonSave`} type="primary" onClick={()=> this.onRightMenuClick('BTN_SAVE')}>{getLabel('86', '保存')}</Button>]}
      moreBtn={{
        datas: this.getRightMenu(),
        collectParams: [{
          favname: `${getLabel(83203, "流程归档集成")}`,
          favouritetype: 1,
          objid: 0,
          link: "wui/engine.html#/integration/WorkflowArchive",
          importantlevel: 1
        }]
      }}
    >
      <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@6x19v7`} 
        datas={this.getRightMenu()}
        collectParams={{
          favname: `${getLabel(83203, "流程归档集成")}`,
          favouritetype: 1,
          objid: 0,
          link: "wui/engine.html#/integration/WorkflowArchive",
          importantlevel: 1
        }}>

        {archiveStore.settingRegisterXMLForm.render({col: 1, center: true}, {
          onChange: (v)=> {
            if (v.FileSaveType) {
              let ops = [...status.regitTypeOptions[v.FileSaveType.value]]
              archiveStore.settingRegisterXMLForm.setField('regitType', {
                options: ops
              })
              archiveStore.settingRegisterXMLForm.updateFields({
                regitType: ops[0].key,
              })
            }

            if (v.XMLType && archiveStore.settingRegisterID == '') {
              let val = v.XMLType.value == '0' ? status.defaultZhongXinXMlTemplate: '';
              archiveStore.settingRegisterXMLForm.updateFields({
                xmltext: val,
              })
            }

            if (v.name) {
              let checkParams = {
                tableName: 'exp_XMLProSettings',
                fieldName: 'name',
                fieldValue: archiveStore.settingRegisterXMLForm.getFormParams().name.replace(/^(\s*)|(\s*)$/g, ''),
                idName: 'id',
                idValue: archiveStore.settingRegisterID
              };
              checkTableFieldValueUnique(checkParams).then(result => {
                if(result&&result.isNotUnique) {
                  archiveStore.settingRegisterXMLForm.showError('name',getLabel('129943','名称已存在'));
                  // message.error(getLabel('387654','标识已存在'));
                }

              });
            }
          },
          isHide: (key, formParams) => {
            let hide = false;
            //设置显隐
            if ('ExpWorkflowRemarkFlag' == key && formParams.ExpWorkflowInfoFlag !== '1') {
              hide = true;
            }
            if ('syncmethodHz' == key && formParams.synType !== '1') {
              hide = true;
            }
            //设置关闭导出流程表单 总开关显隐控制
            if (formParams.ExpWorkflowInfoFlag !== '1' && ['ExpSignFilePath', 'ExpSignFileFlag', 'ExpWorkflowFilePath', 'remarkWDMapFiletype', 'remarkFJMapFiletype'].includes(key)) {
              hide = true;
            }
            if (formParams.ExpWorkflowFileFlag !== '1' && 'ExpWorkflowFileForZipFlag' == key) {
              hide = true;
            }
            if (formParams.ExpWorkflowRemarkFileFlag !== '1' && 'ExpWorkflowRemarkFileForZip' == key) {
              hide = true;
            }
            if ('ExpWorkflowFilePath' == key && (formParams.ExpWorkflowRemarkFileFlag !== '1' && formParams.ExpWorkflowFileFlag !== '1'  || formParams.XMLFileType === '1') ) {
              hide = true;
            }
            if (formParams.ExpWorkflowInfoFlag !== '1' && 'ExpWorkflowInfoPath' == key) {
              hide = true;
            }
            if (formParams.ExpWorkflowRemarkFlag !== '1' && 'ExpSignFileFlag' == key) {
              hide = true;
            }
            if ((formParams.ExpWorkflowRemarkFlag !== '1' || formParams.ExpSignFileFlag !== '1' || formParams.XMLFileType === '1') && 'ExpSignFilePath' == key) {
              hide = true;
            }
            return hide;
          }
        }, true, (item, index) => {
          if(index == 2) {
            let items = [];
            item.items && item.items.map((field, index) => {
              if(getKey(field) == 'ExpWorkflowFilePath') {
                if(archiveStore.settingRegisterXMLForm.getFormParams().XMLType == '0' && archiveStore.settingRegisterXMLForm.getFormParams().ExpWorkflowFileFlag == '1') {
                  items.push({
                    com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ozkmb6@integrationWorkflowarchiveXmlForm${field.domkey[0]}`}
                            label={`${field.label}`}
                            labelCol={{span: `${field.labelcol}`}}
                            wrapperCol={{span: `${field.fieldcol}`}}
                            error={archiveStore.settingRegisterXMLForm.getError(field)}
                            tipPosition="bottom"
                          >
                            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@amx1jc@integrationWorkflowarchiveXmlForm${field.domkey[0]}`} style={{marginLeft:42}} viewAttr={3} value={archiveStore.settingRegisterXMLForm.getFormParams().ExpWorkflowFilePath} hasBorder 
                              onChange={(v)=> {
                                archiveStore.settingRegisterXMLForm.updateFields({
                                  ExpWorkflowFilePath: v
                                })
                              }}
                            />
                          </WeaFormItem>),
                    colSpan: 1,
                    hide: archiveStore.settingRegisterXMLForm.isHide(field, (key, formParams) => {
                      let hide = false;
                      if ('syncmethodHz' == key && formParams.synType !== '1') {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowFileFlag !== '1' && 'ExpWorkflowFileForZipFlag' == key) {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowRemarkFileFlag !== '1' && 'ExpWorkflowRemarkFileForZip' == key) {
                        hide = true;
                      }
                      if ('ExpWorkflowFilePath' == key && (formParams.ExpWorkflowRemarkFileFlag !== '1' && formParams.ExpWorkflowFileFlag !== '1'  || formParams.XMLFileType === '1') ) {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowInfoFlag !== '1' && 'ExpWorkflowInfoPath' == key) {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowRemarkFlag !== '1' && 'ExpSignFileFlag' == key) {
                        hide = true;
                      }
                      if ((formParams.ExpWorkflowRemarkFlag !== '1' || formParams.ExpSignFileFlag !== '1' || formParams.XMLFileType === '1') && 'ExpSignFilePath' == key) {
                        hide = true;
                      }
                      return hide;
                    })
                  })
                }else {
                  items.push({
                    com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@aff2zv@integrationWorkflowarchiveXmlForm${field.domkey[0]}`}
                            label={`${field.label}`}
                            labelCol={{span: `${field.labelcol}`}}
                            wrapperCol={{span: `${field.fieldcol}`}}
                            error={archiveStore.settingRegisterXMLForm.getError(field)}
                            tipPosition="bottom"
                        >
                           <div style={{marginLeft:42}}>
                          <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@8z1k5d@integrationWorkflowarchiveXmlForm${field.domkey[0]}`} form={archiveStore.settingRegisterXMLForm} fieldConfig={field} formParams={archiveStore.settingRegisterXMLForm.getFormParams()} 
                            onChange={(v)=> {
                              if (v.FileSaveType) {
                                let ops = [...status.regitTypeOptions[v.FileSaveType.value]]
                                archiveStore.settingRegisterXMLForm.setField('regitType', {
                                  options: ops
                                })
                                archiveStore.settingRegisterXMLForm.updateFields({
                                  regitType: ops[0].key,
                                })
                              }
                  
                              if (v.XMLType) {
                                const val = v.XMLType.value == '0'?status.defaultZhongXinXMlTemplate: '';
                                archiveStore.settingRegisterXMLForm.updateFields({
                                  xmltext: val,
                                })
                              }
                  
                              if (v.name) {
                                let checkParams = {
                                  tableName: 'exp_XMLProSettings',
                                  fieldName: 'name',
                                  fieldValue: archiveStore.settingRegisterXMLForm.getFormParams().name.replace(/^(\s*)|(\s*)$/g, ''),
                                  idName: 'id',
                                  idValue: archiveStore.settingRegisterID
                                };
                                checkTableFieldValueUnique(checkParams).then(result => {
                                  if(result&&result.isNotUnique) {
                                    archiveStore.settingRegisterXMLForm.showError('name',getLabel('129943','名称已存在'));
                                    // message.error(getLabel('387654','标识已存在'));
                                  }
                  
                                });
                              }
                            }}
                          /></div>
                        </WeaFormItem>),
                    colSpan: 1,
                    hide: archiveStore.settingRegisterXMLForm.isHide(field, (key, formParams) => {
                      let hide = false;
                      if ('syncmethodHz' == key && formParams.synType !== '1') {
                        hide = true;
                      }
                        //打开流程表单 关闭ExpWorkflowFilePath
                        if (formParams.ExpWorkflowInfoFlag !== '1' && 'ExpWorkflowFilePath' == key) {
                          hide = true;
                        }
                        if (formParams.ExpWorkflowRemarkFlag !== '1' && 'ExpWorkflowFilePath' == key) {
                          hide = true;
                        }
                      if (formParams.ExpWorkflowFileFlag !== '1' && 'ExpWorkflowFileForZipFlag' == key) {
                        hide = true;
                        }
                        if (formParams.ExpWorkflowFileFlag == '1' && 'ExpWorkflowFilePath' == key) {
                          hide = false;
                      }
                      if (formParams.ExpWorkflowRemarkFileFlag !== '1' && 'ExpWorkflowRemarkFileForZip' == key) {
                        hide = true;
                      }
                      if ('ExpWorkflowFilePath' == key && (formParams.ExpWorkflowRemarkFileFlag !== '1' && formParams.ExpWorkflowFileFlag !== '1'  || formParams.XMLFileType === '1') ) {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowInfoFlag !== '1' && 'ExpWorkflowInfoPath' == key) {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowRemarkFlag !== '1' && 'ExpSignFileFlag' == key) {
                        hide = true;
                      }
                      if ((formParams.ExpWorkflowRemarkFlag !== '1' || formParams.ExpSignFileFlag !== '1' || formParams.XMLFileType === '1') && 'ExpSignFilePath' == key) {
                        hide = true;
                      }
                      return hide;
                    })
                  })
                }
              }else {
                items.push({
                  com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@koawqx@integrationWorkflowarchiveXmlForm${field.domkey[0]}`}
                          label={`${field.label}`}
                          labelCol={{span: `${field.labelcol}`}}
                          wrapperCol={{span: `${field.fieldcol}`}}
                          error={archiveStore.settingRegisterXMLForm.getError(field)}
                          tipPosition="bottom"
                      >
                        <div style={{marginLeft:42}}>
                        <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@xi5ky5@integrationWorkflowarchiveXmlForm${field.domkey[0]}`} form={archiveStore.settingRegisterXMLForm} fieldConfig={field} formParams={archiveStore.settingRegisterXMLForm.getFormParams()} 
                            onChange={(v)=> {
                         
                           
                              if (v.FileSaveType) {
                                let ops = [...status.regitTypeOptions[v.FileSaveType.value]]
                                archiveStore.settingRegisterXMLForm.setField('regitType', {
                                  options: ops
                                })
                                archiveStore.settingRegisterXMLForm.updateFields({
                                  regitType: ops[0].key,
                                })
                              }
                  
                              if (v.XMLType) {
                                const val = v.XMLType.value == '0'?status.defaultZhongXinXMlTemplate: '';
                                archiveStore.settingRegisterXMLForm.updateFields({
                                  xmltext: val,
                                })
                              }
                  
                              if (v.name) {
                                let checkParams = {
                                  tableName: 'exp_XMLProSettings',
                                  fieldName: 'name',
                                  fieldValue: archiveStore.settingRegisterXMLForm.getFormParams().name.replace(/^(\s*)|(\s*)$/g, ''),
                                  idName: 'id',
                                  idValue: archiveStore.settingRegisterID
                                };
                                checkTableFieldValueUnique(checkParams).then(result => {
                                  if(result&&result.isNotUnique) {
                                    archiveStore.settingRegisterXMLForm.showError('name',getLabel('129943','名称已存在'));
                                    // message.error(getLabel('387654','标识已存在'));
                                  }
                  
                                });
                              }
                            }}
                        /></div>
                      </WeaFormItem>),
                  colSpan: 1,
                  hide: archiveStore.settingRegisterXMLForm.isHide(field, (key, formParams) => {
                    let hide = false;
                    if ('syncmethodHz' == key && formParams.synType !== '1') {
                      hide = true;
                    }
                      if ('ExpWorkflowRemarkFileFlag' == key && formParams.ExpWorkflowRemarkFlag !== '1') {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowInfoFlag !== '1' && 'ExpWorkflowRemarkFileFlag' == key) {
                        hide = true;
                      }
                      if (formParams.ExpWorkflowInfoFlag !== '1' && 'ExpWorkflowRemarkFileForZip' == key) {
                        hide = true;
                      }
                    if (formParams.ExpWorkflowFileFlag !== '1' && 'ExpWorkflowFileForZipFlag' == key) {
                      hide = true;
                    }
                      if ((formParams.ExpWorkflowRemarkFileFlag !== '1' || formParams.ExpWorkflowRemarkFlag !== '1') && 'ExpWorkflowRemarkFileForZip' == key) {
                      hide = true;
                    }
                    if ('ExpWorkflowFilePath' == key && (formParams.ExpWorkflowRemarkFileFlag !== '1' && formParams.ExpWorkflowFileFlag !== '1'  || formParams.XMLFileType === '1') ) {
                      hide = true;
                    }
                    if (formParams.ExpWorkflowInfoFlag !== '1' && 'ExpWorkflowInfoPath' == key) {
                      hide = true;
                    }
                    if (formParams.ExpWorkflowRemarkFlag !== '1' && 'ExpSignFileFlag' == key) {
                      hide = true;
                    }
                    if ((formParams.ExpWorkflowRemarkFlag !== '1' || formParams.ExpSignFileFlag !== '1' || formParams.XMLFileType === '1') && 'ExpSignFilePath' == key) {
                      hide = true;
                    }
                    return hide;
                  })
                })
              }
            })
            return <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@4un8qi`} center={true} col={1} items={items} title={item.title} showGroup={item.defaultshow} needTrigger={item.defaultshow} />
          }
        })}
      </WeaRightMenu>
    </WeaDialog>
  }
}