import React from "react";
import { inject, observer } from "mobx-react";
import { Button } from "antd";
import {checkTableFieldValueUnique}  from "../../apis/CommonService";
import { toJS } from "mobx";
import {
  WeaDialog,
  WeaLocaleProvider,
  WeaRightMenu,
} from "ecCom";

const getLabel = WeaLocaleProvider.getLabel;
const store = ["archiveStore"];

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

  getBtns = () => {
    return [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@1pdjm4@integrationWorkflowarchiveLocalButtonSave`} type="primary" onClick={() => this.onRightMenuClick("BTN_SAVE")}>{getLabel('86', '保存')}</Button>
    ]
  }

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

  render() {
    const { archiveStore } = this.props;
    const status = toJS(archiveStore.status);
    let title = getLabel('31691,126142', '注册归档本地');
    if (archiveStore.settingRegisterID != "") {
      title = getLabel('26473,126142', '编辑归档本地');
    }
    return <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@kgfsgs`}
      title={title}
      onCancel={() => archiveStore.setState({ LocalDialogVisible: false })}
      visible={status.LocalDialogVisible}
      style={{ width: 700, height: 300 }}
      hasScroll
      moduleName="integration"
      buttons={this.getBtns()}
      moreBtn={{
        datas: this.getRightMenu(),
        collectParams: []
      }}
    >
      <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@ndamdd`}
        datas={this.getRightMenu()}
        collectParams={{
          favname: `${getLabel(83203, "流程归档集成")}`,
          favouritetype: 1,
          objid: 0,
          link: "wui/engine.html#/integration/WorkflowArchive",
          importantlevel: 1
        }}
      >
        {archiveStore.settingRegisterLocalForm.render({ col: 1, center: true },{
          onChange: (v)=> {
            if (v.name) {
              let checkParams = {
                tableName: 'exp_localdetail',
                fieldName: 'name',
                fieldValue: archiveStore.settingRegisterLocalForm.getFormParams().name.replace(/^(\s*)|(\s*)$/g, ''),
                idName: 'id',
                idValue: archiveStore.settingRegisterID
              };
              checkTableFieldValueUnique(checkParams).then(result => {
                if(result&&result.isNotUnique) {
                  archiveStore.settingRegisterLocalForm.showError('name',getLabel('129943','名称已存在'));
                  // message.error(getLabel('387654','标识已存在'));
                }

              });
            }
          },
          isHide: (key, formParams) => {}

        })}
      </WeaRightMenu>
    </WeaDialog>
  }
}
