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 = () => {
    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.checkArchiveConfigDatabaseTableExists(()=>{
            archiveStore.getArchiveConfigDatabaseOperation();
        });
        break;
    }
  }

  render() {
    const {archiveStore} = this.props;
    const status = toJS(archiveStore.status);
    let title=getLabel('31691,126143', '注册归档数据库');
    if(archiveStore.settingRegisterID!=""){
      title=getLabel('26473,126143', '编辑归档数据库');
    }
    return <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@6oihgn`}
      title={title}
      onCancel={()=> archiveStore.setState({DatabaseDialogVisible: false})}
      visible={status.DatabaseDialogVisible}
      style={{width: 700, height: 400}}
      hasScroll
      moduleName="integration"
      buttons={[
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@1i8vgr@integrationWorkflowarchiveDatabaseButtonSave`} 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@tkzoyn`} 
        datas={this.getRightMenu()}
        collectParams={{
          favname: `${getLabel(83203, "流程归档集成")}`,
          favouritetype: 1,
          objid: 0,
          link: "wui/engine.html#/integration/WorkflowArchive",
          importantlevel: 1
      }}>
      {archiveStore.settingRegisterDatabaseForm.render({col: 1, center: true},{
        onChange: (v)=> {
          if (v.name) {
            let checkParams = {
              tableName: 'exp_dbdetail',
              fieldName: 'name',
              fieldValue: archiveStore.settingRegisterDatabaseForm.getFormParams().name.replace(/^(\s*)|(\s*)$/g, ''),
              idName: 'id',
              idValue: archiveStore.settingRegisterID
            };
            checkTableFieldValueUnique(checkParams).then(result => {
              if(result&&result.isNotUnique) {
                archiveStore.settingRegisterDatabaseForm.showError('name',getLabel('129943','名称已存在'));
              }

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

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