/*
* 收费项弹框
* */

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 ShowChargeDetail extends Component {

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

  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] : ''
  }

  componentDidMount() {
    this.initData();
    // this.loadData();
  }

  initData() {
    this.columns = [
      {
        title: this.setMessage('ApplicationFormaaazx','descripts'),
        dataIndex: 'itemDesc',
        width: 220,
        align: 'left',
      }, {
        title: this.setMessage('ApplicationFormQuantity','descripts'),
        dataIndex: 'qty',
        width: 80,
        align: 'left',
      }, {
        title: this.setMessage('ApplicationFormsdg','descripts'),
        dataIndex: 'unitDesc',
        width: 80,
        align: 'left',
      }, {
        title: this.setMessage('ApplicationFormxcvu','descripts'),
        dataIndex: 'price',
        width: 100,
        align: 'right',
      }, {
        title:this.setMessage('ApplicationFormbn','descripts'),
        dataIndex: 'prices',
        width: 100,
        align: 'right',
        render:(text,data) =>{
        return <span>{(parseFloat(data.qty) * parseFloat(data.price)).toFixed(2)}</span>
        }
      }, {
        title: this.setMessage('ApplicationFormtht','descripts'),
        dataIndex: 'discPrice',
        width: 100,
        align: 'right',
      }, {
        title: this.setMessage('ApplicationFormgth','descripts'),
        dataIndex: 'patPrice',
        width: 100,
        align: 'right',
      },
    ]
  }

  loadData() {
    var params = {
      params: [{
        medID: this.state.medID,
        admID: this.state.patientData.admID,
        hospID: this.state.userData.hospID,
        locID: this.state.userData.locID
      }]

    }
    $http.post('urlDeault', this, {
      code: "03050100",
      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() {
  }



  render() {
    var totalCount = 0, discount = 0;
    var { dataSource } = this.state;
    if (dataSource) {
      for (var i = 0; i < dataSource.length; i++) {
        totalCount += parseFloat(dataSource[i].qty) * parseFloat(dataSource[i].price)
        discount += parseFloat(dataSource[i].discPrice)
      }
    }
    return <div>
      <Modal className='charge-detail-modal'
        title={this.setMessage('ApplicationFormcvvb','descripts')}
        maskClosable={false}
        footer={null}
        visible={this.props.isShow}
        onCancel={this.closeModal.bind(this)}>
        <Table columns={this.columns} bordered
          dataSource={this.state.dataSource}
          title={() => {
          return <span><span>{this.setMessage('ApplicationFormsdfas','descripts')}{this.state.dataSource ? this.state.dataSource.length : 0}</span>
              <span style={{ marginLeft: 10 }}>{this.setMessage('ApplicationFormcbf','descripts')}{totalCount || 0}</span>
              <span style={{ marginLeft: 10 }}>{this.setMessage('ApplicationFormhhhg','descripts')}{discount || 0}</span>
            </span>
          }}
          rowKey={(data) => {
            return data.id
          }}
          pagination={false}></Table>
      </Modal></div>
  }
}

export default Form.create()(ShowChargeDetail);