import React from "react";
import {
  inject, 
  observer,
} from "mobx-react";
import {
  Button, 
} from "antd";
import {
  toJS,
} from "mobx";
import {
  WeaDialog, 
  WeaLocaleProvider, 
  WeaRightMenu, 
} from "ecCom";
import {
  getForm,
  checkUnique,
  onCheck,
} from "../../util/IntegrationUtil"

const {getLabel} = WeaLocaleProvider;

@inject("authenticationSourceStore")
@observer
export default class EditFormDialog extends React.Component {

  closeDialog = () => {
    const {authenticationSourceStore} = this.props;
    authenticationSourceStore.setState({showNewFormDialog: false});
  }

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

  getRightMenu() {
    return [{
      key: 'BTN_SAVE',
      icon: <i className='icon-coms-Preservation'/>,
      content : `${getLabel(86,'保存')}`,
      onClick : () => this.onRightMenuClick('BTN_SAVE')
    }];
  }

  onRightMenuClick = (type) => {
    const {
      authenticationSourceStore,
    } = this.props, {
      newForm, 
      status,
    } = authenticationSourceStore, {
      newFormHiddenFields, 
      paramsData,
    } = toJS(status);
    if(type == 'BTN_SAVE'){
      onCheck(newForm, () => {
        checkUnique({
          tableName: 'weaver_sso_src',
          fieldName: 'name_',
          fieldValue: newForm.getFormParams()["name_"].replace(/^(\s*)|(\s*)$/g, ''),
          idName: 'id',
          idValue: newFormHiddenFields.id,
        }, newForm, "name_", name="2", newFormHiddenFields.id, () => {
          let formParams = newForm.getFormParams();
          let params = Object.assign({}, newFormHiddenFields, formParams, {
            orderNum: typeof(formParams.orderNum) === "object" 
              ? formParams.orderNum.join && formParams.orderNum.join(",")
                ? formParams.orderNum 
                : "0"
              : formParams.orderNum
          });
          params.dtinfo_paramsData = JSON.stringify(paramsData);
          authenticationSourceStore.getAuthenticationSourceOperation(params);
        });
      })
    }
  }

  onChangeForm = (form, allParams, fields) => {
    const {
      authenticationSourceStore,
    } = this.props, {
      newFormHiddenFields,
    } = toJS(authenticationSourceStore.status);
    const key = fields.domkey[0], formParams = form.getFormParams();
    if("name_" === key && formParams.name_ && formParams.name_.trim()) {
      checkUnique({
        tableName: 'weaver_sso_src',
        fieldName: 'name_',
        fieldValue: form.getFormParams()[key].replace(/^(\s*)|(\s*)$/g, ''),
        idName: 'id',
        idValue: newFormHiddenFields.id,
      }, form, key, name="2");
    }
    if(typeof(form.getFormParams()[key]) === "string") {
      form.updateFields({[key]: form.getFormParams()[key] ? form.getFormParams()[key].trim() : ""});
    }
  }

  render() {
    const {
      authenticationSourceStore,
    } = this.props, {
      status,
      newForm,
    } = authenticationSourceStore, {
      showNewFormDialog,
      newFormHiddenFields,
      newFormCondition=[],
    } = toJS(status);

    return <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@t1k33y`}
      title={newFormHiddenFields.id ? getLabel('524315','编辑认证源') : getLabel('524316','新建认证源')}
      moduleName="integration"
      onCancel={this.closeDialog}
      visible={showNewFormDialog}
      style={{width: 800, height: 600}}
      hasScroll
      buttons={this.getBtns()}
      moreBtn={{
        datas: this.getRightMenu(),
        collectParams: []
      }}
    >
      <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@e21t50`}
        datas={this.getRightMenu()}
        collectParams={[]}
      >
        {getForm(newForm, newFormCondition, true, this.onChangeForm, this && this.props && this.props.ecId || '', "integrationAuthSourceEditForm")}
      </WeaRightMenu>
    </WeaDialog>
  }
}
