import React from 'react';
import { Modal, Transfer, message } from 'antd';

class TransferModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      mockData:[],
      targetKeys:[]
    };
  };

  componentDidMount() {
    if (this.props.onRef) {
      this.props.onRef(this)
    }
  };

  showModal = () => {
    this.GetTransferData();
    this.setState({
      visible: true,
    });
  }

  handleOk = e => {
    this.SettingSave();
  };

  handleCancel = e => {
    this.setState({
      visible: false,
    });
  };

  GetTransferData = () => {
    const json = {
      OrgId: this.props.login.OrgId
    };
    this.props.dispatch({
      type: 'app/getDataByPost',
      url: 'EA/AdjustAccounts/GetTransferData',
      payload: json,
      onlyData: false,
      onComplete: (re) => {
        if (re && re.IsSuccessful && re.Data) {
          const { mockData, targetKeys } = re.Data
          this.setState({
            mockData,
            targetKeys
          });
        }
      }
    })
  };


  SettingSave = () => {
    const json = {
      DefaultValue: this.state.targetKeys,
    };
    this.props.dispatch({
      type: 'app/getDataByPost',
      url: 'EA/AdjustAccounts/KpiTeamSave',
      payload: json,
      onlyData: false,
      onComplete: (re) => {
        if (re && re.IsSuccessful && re.Data) {
          if (re.Data.IsSucceed) {
            message.success(re.Data.Msg);
            this.setState({
              visible: false,
            }, () => { this.props.onLoadData() });
          }
          else
            message.error(re.Data.Msg);
        }
      }
    })
  };
  filterOption = (inputValue, option) => option.title.indexOf(inputValue) > -1;

  handleChange = targetKeys => {
    this.setState({ targetKeys });
  };

  handleSearch = (dir, value) => {
  };

  render() {
    const { mockData,targetKeys } = this.state
    return (
      <Modal title="班组配置" visible={this.state.visible} onOk={this.handleOk} onCancel={this.handleCancel}>
        <Transfer
          dataSource={mockData}
          showSearch
          filterOption={this.filterOption}
          targetKeys={targetKeys}
          onChange={this.handleChange}
          onSearch={this.handleSearch}
          render={item => item.title}
        />
      </Modal>
    )
  }
}
export default TransferModal