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

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 ShowUseTemplateModal 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({ template: nextProp.template }, () => {
        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: 'sort',
        width: 130,
        align: 'center',
        render: (text, data, index) => {
          return <span>{index + 1}</span>
        }
      }, {
        title: this.setMessage('ApplicationFormdescripts', 'descripts'),
        dataIndex: 'descripts',
        width: 80,
        align: 'center',
      }, {
        title:  this.setMessage('ApplicationFormoperate', 'descripts'),
        dataIndex: '3',
        width: 120,
        align: 'center',
        render: (text, data) => {
          return <span style={{ cursor: 'pointer', color: 'rgb(64, 169, 255)' }}
        onClick={this.hanldeDel.bind(this, data)}>{this.setMessage('ApplicationFormDelete', 'descripts')}</span>
        }
      }
    ]
  }

  loadData() {
    var params = {
      params: [{
        userID: this.state.userData.userID,
      }]
    }
    var code = ''
    if (this.state.template == 'chiefComplaint') {
      code = '03050116'
    } else if (this.state.template == 'appPurpose') {
      code = '03050119'
    } else if (this.state.template == 'HPIllness') {
      code = '03050122'
    } else if (this.state.template == 'sign') {
      code = '03050125'
    }
    $http.post('urlDeault', this, {
      code: code,
      data: params,
      success: function (res) {
        if (res.errorCode == '0') {
          this.setState({
            dataSource: res.result
          })
        }
      }
    });
  }

  hanldeDel(data) {
    var params = {
      params: [{
        userID: this.state.userData.userID,
        id: [data.id]
      }]
    }
    var code = ''
    if (this.state.template == 'chiefComplaint') {
      code = '03050115'
    } else if (this.state.template == 'appPurpose') {
      code = '03050118'
    } else if (this.state.template == 'HPIllness') {
      code = '03050121'
    } else if (this.state.template == 'sign') {
      code = '03050124'
    }
    $http.post('urlDeault', this, {
      code: code,
      data: params,
      success: function (res) {
        if (res.errorCode == '0') {
          message.success(this.setMessage('ApplicationFormdeletes', 'descripts'),this.state.userData && this.state.userData.messageTime)
          this.loadData()
        }
      }
    });
  }

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

  //清空输入框
  cleanInput() {
    this.setState({
      dataSource: [],
      rowId: '',
      tempData: ''
    })
  }

  onSubmit(data) {
    if (Util.isEmpty(this.state.rowId)) {
      message.error(this.setMessage('ApplicationFormxcv', 'descripts'),this.state.userData && this.state.userData.messageTime)
      return
    }
    var { onSubmit } = this.props;
    onSubmit && onSubmit(this.state.tempData)
  }

  setElement = (record, index) => {
    return {
      onClick: () => {
        this.setState({
          rowId: record.id,
          tempData: record.descripts
        })
      },
      onDoubleClick: () => {
        this.onSubmit()
      }
    }
  }

  setRowClassName = (record) => {
    return record.id === this.state.rowId ? 'clickRowStyle' : '';
  }

  render() {
    var title = ''
    if (this.state.template == 'chiefComplaint') {
      title = this.setMessage('ApplicationFormbb', 'descripts')
    } else if (this.state.template == 'appPurpose') {
      title = this.setMessage('ApplicationFormbv', 'descripts')
    } else if (this.state.template == 'HPIllness') {
      title = this.setMessage('ApplicationFormnb', 'descripts')
    } else if (this.state.template == 'sign') {
      title = this.setMessage('ApplicationFormbvx', 'descripts')
    }
    return <div>
      <Modal className='charge-detail-modal'
        title={title}
        maskClosable={false}
        okText={this.state.userData.language == 'EN' ? 'Submit' : '确定'}
        cancelText={this.state.userData.language == 'EN' ? 'Cancel' : '取消'}
        visible={this.props.isShow}
        onOk={this.onSubmit.bind(this)}
        onCancel={this.closeModal.bind(this)}>
        <Table columns={this.columns} bordered
          dataSource={this.state.dataSource}
          onRow={this.setElement}
          rowClassName={this.setRowClassName}
          rowKey={(data) => {
            return data.id
          }}
          pagination={false}></Table>
      </Modal></div>
  }
}

export default Form.create()(ShowUseTemplateModal);