import React from "react";
import { inject, observer } from "mobx-react";
import { Button } from "antd";
import { toJS } from "mobx";
import { WeaDialog, WeaLocaleProvider, WeaRightMenu, WeaTab } from "ecCom";
import { WeaTableNew } from "comsMobx";

const getLabel = WeaLocaleProvider.getLabel;
const { WeaTable } = WeaTableNew;
const store = ["todoClientStore"];

@inject(...store)
@observer
export default class WhiteWorkflowDialog extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      addDialogVisible: false
    };
  }
  closeDialog = () => {
    const { todoClientStore } = this.props;
    todoClientStore.setState({ WhiteWorkflowDialogVisible: false, rowSelectIds_wf: [] })
  }

  getRightMenu = () => {
    const { todoClientStore } = this.props;
    const { rowSelectIds_wf } = toJS(todoClientStore.status);
    let btnArr = [
      {
        key: 'rb_1',
        icon: <i className='icon-coms-New-Flow' />,
        content: `${getLabel(611, '添加')}`,
        onClick: () => this.onRightMenuClick('rb_1')
      },
      {
        key: 'rb_2',
        icon: <i className='icon-coms-delete' />,
        content: `${getLabel(32136, '批量删除')}`,
        onClick: () => this.onRightMenuClick('rb_2'),
        disabled: rowSelectIds_wf.length === 0
      },
    ]
    return btnArr;
  }
  getRightMenu2 = () => {
    const { todoClientStore } = this.props;
    const { rowSelectIds_wf } = toJS(todoClientStore.status);
    let btnArr = [
      {
        key: 'rb2_1',
        icon: <i className='icon-coms-Approval' />,
        content: `${getLabel(16631, '确认')}`,
        onClick: () => {
          todoClientStore.workflowForm.validateForm().then(f => {
            if (f.isValid) {
              let formParams = todoClientStore.workflowForm.getFormParams();
              let params = { operate: 'dowf', sysid: todoClientStore.status.sysid, workflowids: formParams.workFlowId }
              todoClientStore.getOfsSendOperation(params, (result) => {
                this.setState({ addDialogVisible: false });
              })
            } else {
              f.showErrors();
            }
          })
        }
      },
    ]
    return btnArr;
  }
  onRightMenuClick = (type, recordId) => {
    const { todoClientStore } = this.props;
    const { status } = todoClientStore;
    const { sysid, rowSelectIds_wf, rowSelectIds_hrm } = toJS(status);

    if (type == 'rb_1') {
      //添加
      this.setState({ addDialogVisible: true });
      todoClientStore.getOfsSendWhiteWorkflowForm();
    } else if (type == 'rb_2') {
      //删除
      if (rowSelectIds_wf.length > 0) {
        todoClientStore.confirmModal(`${getLabel(385625, '确定要删除选择的记录吗？')}`, todoClientStore.getOfsSendOperation, { operate: 'dowfdel', dataid: rowSelectIds_wf, sysid: sysid });
      }
    }
  }

  onOperatesClick = (record, index, operate, flag) => {
    const { todoClientStore } = this.props;
    const { sysid } = toJS(todoClientStore.status);
    if (operate.index == "0") {
      //流程白名单
      let params = { operate: 'dowfdel', dataid: record.randomFieldId, sysid: sysid };
      todoClientStore.confirmModal(`${getLabel(15097, '确定要删除吗？')}`, todoClientStore.getOfsSendOperation, params);
    }
  }

  getRowSelection = () => {
    return {
      onChange: (selectedKey, selectedRow) => {
        let datas = [];
        selectedRow && selectedRow.forEach(data => {
          data && datas.push(data.randomFieldId);
        });
        this.props.todoClientStore.setState({
          rowSelectIds_wf: datas
        })
      }
    }
  }

  setShowSearchAd = (bool) => this.props.todoClientStore.setState({ showSearchAd: bool });

  hideSearchAd = () => this.props.todoClientStore.setState({ showSearchAd: false });

  closeChooseWorkflow = () => this.setState({ addDialogVisible: false });

  render () {
    const { todoClientStore } = this.props;
    const { status, tableStore_whitewf } = toJS(todoClientStore);
    const { showSearchAd, rowSelectIds_wf, WhiteWorkflowDialogVisible } = toJS(status);

    return <div>
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@hwbqif`}
        title={getLabel('382191', '流程白名单')}
        onCancel={this.closeDialog}
        visible={WhiteWorkflowDialogVisible}
        style={{ width: 900, height: 600 }}
        hasScroll
        moduleName="integration"
        buttons={[]}
        moreBtn={[]}
      >
        <WeaRightMenu
          ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@whi4vv`}
          datas={this.getRightMenu()}
          collectParams={[]}
        >
          <WeaTab
            ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@v3f8u8`}
            keyParam="viewcondition"
            selectedKey={0}
            advanceHeight={90}
            showSearchAd={showSearchAd}
            setShowSearchAd={this.setShowSearchAd}
            hideSearchAd={this.hideSearchAd}
            buttons={[
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@swwli2@integrationTodoclientwhiteWorkflowButtonrb_1`} type="primary" onClick={() => this.onRightMenuClick('rb_1')}>{getLabel('611', '添加')}</Button>,
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@74q1wn@integrationTodoclientwhiteWorkflowButtonrb_2`} type="primary" disabled={rowSelectIds_wf.length === 0} onClick={() => this.onRightMenuClick('rb_2')}>{getLabel('32136', '批量删除')}</Button>,
            ]}
          />
          <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@pyyzbx`}
            comsWeaTableStore={tableStore_whitewf}
            rowSelection={this.getRowSelection()}
            hasOrder={true}
            needScroll={true}
            onOperatesClick={this.onOperatesClick}
          />
        </WeaRightMenu>
      </WeaDialog>
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@iwaw0o`}
        title={getLabel('532033', '选择流程')}
        onCancel={this.closeChooseWorkflow}
        visible={this.state.addDialogVisible}
        style={{ width: 650, height: 300 }}
        hasScroll
        moduleName="integration"
        buttons={[
          <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@gsly97@integrationTodoclientwhiteWorkflowButtonSubmit`} type="primary" onClick={() => {
            todoClientStore.workflowForm.validateForm().then(f => {
              if (f.isValid) {
                let formParams = todoClientStore.workflowForm.getFormParams();
                let params = { operate: 'dowf', sysid: todoClientStore.status.sysid, workflowids: formParams.workFlowId }
                todoClientStore.getOfsSendOperation(params, (result) => {
                  this.setState({ addDialogVisible: false });
                })
              } else {
                f.showErrors();
              }
            })
          }}>{getLabel('16631', '确认')}</Button>,
        ]}
        moreBtn={[]}
      >
        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@zpqkt6`}
          datas={this.getRightMenu2()}
          collectParams={[]}>

          {todoClientStore.workflowForm.render({ col: 1, center: true }, {})}
        </WeaRightMenu>

      </WeaDialog>
    </div>
  }
}
