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

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

@inject(...store)
@observer
export default class WorkflowViewFormDialog extends React.Component {
  closeDialog() {
    const {todoCenterStore} = this.props;
    todoCenterStore.setState({WorkflowViewFormDialogVisible: false})
  }

  getRightMenu() {
    const {todoCenterStore} = this.props;
    const {status: {onlyRead, operateIndex}} = todoCenterStore;

    if(onlyRead) {
      return [
        {
          key: 'BTN_SAVE',
          icon: <i className='icon-coms-go-back'/>,
          content: `${getLabel(309, '关闭')}`,
          onClick: () => this.closeDialog()
        }
      ]
    }else {
        return [
          {
            key: 'BTN_SAVE',
            icon: <i className='icon-coms-Preservation'/>,
            content: getLabel('30986','保存'),
            onClick: () => this.saveDialog()
          }
        ]
    }
  }

  saveDialog = () => {
    const {todoCenterStore} = this.props;
    const {newForm2, status: {operateIndex}} = todoCenterStore;
    onCheck(newForm2, () => {
      let params = Object.assign({}, newForm2.getFormParams(), {id: operateIndex, operation: operateIndex ? 'edit' : 'add'});
      todoCenterStore.ReceiveRequestInfoByJson(params, () => {
        todoCenterStore.setState({WorkflowViewFormDialogVisible: false});
      });
    })
  }

  getFormSearchs = (form, datas=[]) => {
    const { isFormInit } = form;
    let group = [];
    const formParams = form.getFormParams();
    isFormInit && datas && datas.map((c, index) => {
      let items = [];
      c && c.items && c.items.map(fields => {
        let key = fields.domkey[0];
        if(key != "syscode") {
          if(key === "workflowname" && fields.conditionType && fields.conditionType.toLocaleUpperCase() === "BROWSER") {
            fields.browserConditionParam.dataParams = {
              ...fields.browserConditionParam.dataParams,
              sysid: form.getFormParams().sysid
            }
          }
          if(key === "sysid" && fields.conditionType && fields.conditionType.toLocaleUpperCase() === "BROWSER") {
            fields.browserConditionParam.hasAdvanceSerach = false;
          }
          if(["creatorid", "userid", "workflowname"].includes(key) && (!form.getFormParams().sysid)) {
            fields.viewAttr = 1;
          }
          items.push({
            com: (
              <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@x9862c@integrationTodoCenterViewForm${key}`}
                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@qarpkh@integrationTodoCenterViewForm${key}`} fieldConfig={fields} form={form} formParams={formParams} onChange={v => {
                  if(key === "sysid") {
                    form.updateFields({
                      receiver: "",
                      userid: [{value: "", valuespan: "", valueObj: [{value: "", id: ""}]}],
                      creator: "",
                      creatorid: [{value: "", valuespan: "", valueObj: [{value: "", id: ""}]}],
                      workflowname: [{value: "", valuespan: "", valueObj: [{value: "", id: ""}]}],
                      syscode: (v.sysid.valueObj && v.sysid.valueObj[0]) ? v.sysid.valueObj[0].syscode || "" : ""
                    })
                  }else if(["creatorid", "userid"].includes(key)) {
                    this.props.todoCenterStore.OfsHrmResourceTransfer({
                      userid: form.getFormParams()[key],
                      sysid: form.getFormParams().sysid
                    }, (value) => {
                      if("creatorid" === key) {
                        form.updateFields({creator: value});
                      } else if("userid" === key) {
                        form.updateFields({receiver: value});
                      }
                    });
                  }
                }}/>
              </WeaFormItem>),
            colSpan: 1
          });
        }
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@aseo1c@integrationTodoCenterViewForm${index}`} center={true} col={1} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} />)
    });
    return group;
  }

  render() {
    const {
      todoCenterStore
    } = this.props, {
      newForm2,
      status
    } = todoCenterStore, {
      onlyRead, 
      operateIndex, 
      newForm2Condition=[]
    } = toJS(status);
    let title = "";
    if(onlyRead) {
      title = getLabel('524618','流程详细数据');
    }else {
      if(operateIndex) {
        title = getLabel('516502','编辑流程数据'); 
      }else {
        title = getLabel('516503','新建流程数据');
      }
    }
    return <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@0o31w8`}
      title={title}
      onCancel={() => this.closeDialog()}
      visible={status.WorkflowViewFormDialogVisible}
      style={{width: 700, height: 600}}
      hasScroll
      moduleName="integration"
      buttons={
        onlyRead 
          ? [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@2jszj0@integrationTodoCenterEditDetailButtonClose`} type="primary" onClick={() => this.closeDialog()}>{getLabel('309', '关闭')}</Button>]
          : [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@iwz53v@integrationTodoCenterEditDetailButtonSave`} type="primary" onClick={() => this.saveDialog()}>{getLabel('30986','保存')}</Button>]
      }
      moreBtn={{
        datas : this.getRightMenu(),
        collectParams: []
      }}
    >
      <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@pygjoa`}
        datas={this.getRightMenu()}
        collectParams={[]}>
        {this.getFormSearchs(newForm2, newForm2Condition)}
        {/* {todoCenterStore.newForm2.render({col: 1, center: true})} */}
      </WeaRightMenu>
    </WeaDialog>
  }
}
