import React from 'react'
import { connect } from 'dva'
import { Modal, Form, Transfer, message} from 'antd'

class WriteOffNetworkModal extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      visible: false,
      loading: false,
      mainData: [],
      mainTargetKeys: [],
      assistData: [],
      assistTargetKeys: []
    }
  }

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

  showModal = (value) => {
    this.setState({
      visible: true
    }, () => {
      this.loadModalData()
    })
  }

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


  loadModalData = () => {
    const json = {
      OrgId: this.props.login.OrgId
    };
    this.props.dispatch({
      type: 'app/getDataByPost',
      url: 'EA/AdjustAccounts/GetWriteOffTransferData',
      payload: json,
      onlyData: false,
      onComplete: (re) => {
        if (re && re.IsSuccessful && re.Data) {
          const { assistData, assistTargetKeys ,mainData, mainTargetKeys} = re.Data
          this.setState({
            assistData,
            assistTargetKeys,
            mainData,
            mainTargetKeys
          });
        }
      }
    })
  }

  SettingSave = () => {
    const json = {
      OrgId:this.props.login.OrgId,
      AssistTargetKeys: this.state.assistTargetKeys,
      MainTargetKeys:this.state.mainTargetKeys
    };
    this.props.dispatch({
      type: 'app/getDataByPost',
      url: 'EA/AdjustAccounts/WriteOffSettingSave',
      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;

  assistHandleChange = targetKeys => {
    this.setState({ assistTargetKeys:targetKeys });
  };
  mainHandleChange = targetKeys => {
    this.setState({ mainTargetKeys:targetKeys });
  };


  render() {
    const { assistData, assistTargetKeys ,mainData, mainTargetKeys} = this.state

    return (
      <Modal title="添加生产节点" visible={this.state.visible} onOk={this.handleOk} onCancel={this.handleCancel}>
        <div style={{fontWeight:'bold',fontSize:16}}>辅助生产节点</div>
        <Transfer
          dataSource={assistData}
          showSearch
          filterOption={this.filterOption}
          targetKeys={assistTargetKeys}
          onChange={this.assistHandleChange}
          render={item => item.title}
        />
        <div style={{fontWeight:'bold',fontSize:16,marginTop:15}}>核销（主要生产）节点</div>
        <Transfer
          dataSource={mainData}
          showSearch
          filterOption={this.filterOption}
          targetKeys={mainTargetKeys}
          onChange={this.mainHandleChange}
          render={item => item.title}
        />
      </Modal>
    )
  }
}
export default connect(({ app, editPage, loading, login }) => ({ app, editPage, loading, login }))(Form.create()(WriteOffNetworkModal))