import React from "react";
import { inject, observer } from "mobx-react";
import { Button } from "antd";
import { toJS } from "mobx";
import { WeaDialog, WeaLocaleProvider, WeaRightMenu, WeaTab, WeaAuth } from "ecCom";
import { WeaTableNew } from "comsMobx";
import { GET_OFS_SEND_WHITE_HRM_CONDITIONS } from '../../public/constants';

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

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

  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;

    let btnArr = [
      {
        key: 'rb2_1',
        icon: <i className='icon-coms-Approval' />,
        content: `${getLabel(16631, '确认')}`,
        onClick: () => {
          let params = { operate: 'douser', sysid: todoClientStore.status.sysid, workflowids: todoClientStore.status.whiteWorkflowIds }
          todoClientStore.getOfsSendOperation(params, (result) => {
            this.setState({ addDialogVisible: false });
          })
        }
      },
    ]
    return btnArr;
  }
  onRightMenuClick = (type) => {
    const { todoClientStore } = this.props;
    const { status } = todoClientStore;
    const { sysid, rowSelectIds_hrm } = toJS(status);

    if (type == 'rb_1') {
      //添加
      this.setState({ addDialogVisible: true });

    } else if (type == 'rb_2') {
      //删除
      if (rowSelectIds_hrm.length > 0) {
        todoClientStore.confirmModal(`${getLabel(385625, '确定要删除选择的记录吗？')}`, todoClientStore.getOfsSendOperation, { operate: 'douserdel', dataid: rowSelectIds_hrm, sysid: sysid });
      }
    }
  }

  onOperatesClick = (record, index, operate) => {
    const { todoClientStore } = this.props;
    const { sysid } = toJS(todoClientStore.status);
    if (operate.index == "0") {
      //流程白名单
      let params = { operate: 'douserdel', 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_hrm: datas
        })
      }
    }
  }

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

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

  render () {
    const { todoClientStore } = this.props;
    const { status, tableStore_whitehrm } = toJS(todoClientStore);
    const { showSearchAd, rowSelectIds_hrm, auth } = toJS(status);

    return <div>
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@eltpak`}
        title={getLabel('382192', '人员白名单')}
        onCancel={this.closeDialog}
        visible={status.WhiteHrmDialogVisible}
        style={{ width: 900, height: 600 }}
        hasScroll
        moduleName="integration"
        buttons={[]}
        moreBtn={[]}
      >
        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@nha8g2`}
          datas={this.getRightMenu()}
          collectParams={[]}>

          <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@k7srk1`}
            keyParam="viewcondition"
            selectedKey={0}
            advanceHeight={90}
            showSearchAd={showSearchAd}
            setShowSearchAd={this.setShowSearchAd}
            hideSearchAd={this.hideSearchAd}
            buttons={[
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@lz9rdp@integrationTodoclientwhiteHrmButtonrb_1`} type="primary" onClick={() => this.onRightMenuClick('rb_1')}>{getLabel('611', '添加')}</Button>,
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qft1uh@integrationTodoclientwhiteHrmButtonrb_2`} type="primary" disabled={rowSelectIds_hrm.length === 0} onClick={() => this.onRightMenuClick('rb_2')}>{getLabel('32136', '批量删除')}</Button>,
            ]}
          />
          <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@m7v3ok`}
            comsWeaTableStore={tableStore_whitehrm}
            hasOrder={true}
            needScroll={true}
            rowSelection={this.getRowSelection()}
            onOperatesClick={this.onOperatesClick}
          />

        </WeaRightMenu>
      </WeaDialog>
      <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@qyppw2`}
        visible={this.state.addDialogVisible}
        conditions={GET_OFS_SEND_WHITE_HRM_CONDITIONS(auth)}
        title={getLabel('532047', '选择对象')}
        hasScroll
        moduleName="integration"
        onOk={v => {
          let params = Object.assign({ operate: 'douser', sysid: todoClientStore.status.sysid }, v);
          todoClientStore.getOfsSendOperation(params, (result) => {
            this.setState({ addDialogVisible: false });
          })
        }}
        onCancel={() => {
          this.setState({ addDialogVisible: false });
        }}
      />
    </div>
  }
}
