/**
 * 
 * 撤销部件弹框
 */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, Button, DatePicker, Table, message, Tooltip } from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;

class ShowRevocationModal extends Component {

  constructor(props) {
    super(props);
    var date = new Date();
    var userData = JSON.parse(sessionStorage.getItem('userData'));
    this.state = {
      leftData: [],
      rightData: [],
      userData: userData,
    }
  }
  componentWillReceiveProps(nextProp) {
    if (nextProp.isShow === true) {
      this.setState({ medID: nextProp.detailSource ? nextProp.detailSource.medID : '' }, () => {
        this.loadData()
        this.loadCommonModal()
      })
    } else if (this.props.isShow === false) {
      this.cleanInput();
    }
  }

  componentDidMount() {
    this.initData();
    // this.loadData();
  }
  loadCommonModal() {
    let data = {
      params: [{
        compontName: "ApplicationForm",
        type: "C",
        reactCode: ['ApplicationForm']
      }]
    }
    $http.post('urlDeault', this, {
      code: "01040073",
      data: data,
      success: (res) => {
        if (+res.errorCode === 0) {
          this.setState({
            localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN
          }, () => {
            this.initData()
          })
        }
      }
    })
  }

  //国际化
  setMessage(name, message) {
    return this.state.localeProvider && this.state.localeProvider[name] ? this.state.localeProvider[name][message] : ''
  }

  initData() {
    this.columns = [
      {
        title: this.setMessage('ApplicationFormsort', 'descripts') || '序号',
        dataIndex: '1',
        width: 130,
        align: 'center',
        render: (text, data, index) => {
          return <span>{index + 1}</span>
        }
      }, {
        title: this.setMessage('ApplicationFormPart', 'descripts') || '部位',
        dataIndex: 'partDesc',
        width: 80,
        align: 'center',
      }, {
        title: this.setMessage('ApplicationFormStatus', 'descripts') || '状态',
        dataIndex: 'statusDesc',
        width: 120,
        align: 'center',
      }, {
        title: this.setMessage('ApplicationFormoperate', 'descripts') || '操作',
        dataIndex: '3',
        width: 120,
        align: 'center',
        render: (text, data) => {
          return data.statusCode == 'CO' ? null : <span style={{ cursor: 'pointer', color: 'rgb(64, 169, 255)' }}
        onClick={this.onSubmit.bind(this, data)}>{this.setMessage('ApplicationFormRevoke', 'descripts')}</span>
        }
      }
    ]
  }

  loadData() {
    var params = {
      params: [{
        appItemID: this.props.itemData,
        userID: this.state.userData.userID,
        locID: this.state.userData.locID
      }]

    }
    $http.post('urlDeault', this, {
      code: "03050113",
      data: params,
      success: function (res) {
        if (res.errorCode == '0') {
          this.setState({
            dataSource: res.result
          })
        }
      }
    });
  }

  closeModal(name) {
    let { onHide } = this.props;
    onHide && onHide();
    this.cleanInput();
  }

  //清空输入框
  cleanInput() {
  }

  onSubmit(data) {
    var params = {
      params: [{
        appItemPartID: data.appItemPartID,
        userID: this.state.userData.userID,
        locID: this.state.userData.locID
      }]
    }
    $http.post('urlDeault', this, {
      code: "03050112",
      data: params,
      success: function (res) {
        if (res.errorCode == '0') {
          message.success(this.setMessage('ApplicationFormundo', 'descripts'),this.state.userData && this.state.userData.messageTime)
          this.loadData()
          var { onSubmit } = this.props;
          onSubmit && onSubmit()
        }
      }
    });
  }

  render() {
    return <div>
      <Modal className='charge-detail-modal'
        title={this.setMessage('ApplicationFormxzz', 'descripts')}
        maskClosable={false}
        okText={this.state.userData.language == 'EN' ? 'Submit' : '确定'}
        cancelText={this.state.userData.language == 'EN' ? 'Cancel' : '取消'}
        footer={null}
        visible={this.props.isShow}
        onOk={this.onSubmit.bind(this)}
        onCancel={this.closeModal.bind(this)}>
        <Table columns={this.columns} bordered
          dataSource={this.state.dataSource}
          rowKey={(data) => {
            return data.id
          }}
          pagination={false}></Table>
      </Modal></div>
  }
}

export default Form.create()(ShowRevocationModal);