/*
 * Create:      柿子
 * CreateDate:  2022/10/21
 * Describe：   汇总打印 - 配镜申请单
 * */
import React, { Component } from 'react';
import { Row, Col, Button, Checkbox, message } from 'antd';
import { $loadTemplate } from 'pages/lodop/Common/Print.js';
import store from 'store';
import AcBarcode from 'ac-barcode';
import PublicTablePagination from 'pages/common/PubilcTablePagination';
import './style/index.less';

class ApplicationFormForGlasses extends Component {
  constructor(props) {
    super(props);
    var currentPatData = React.$getSessionData('titleData');
    this.state = {
      currentPatData,
      printData: [],
      columns: [],
      totalWidth: 530
    };
    this.staticOrdColumns = [{
      key: 1,
      title: '品名规格',
      dataIndex: 'medDesc',
      align: 'center',
      width: 150
    }, {
      key: 2,
      title: '原单价',
      dataIndex: 'allPatAmt',
      align: 'center',
      width: 100
    }, {
      key: 3,
      title: '现单价',
      dataIndex: 'patAmt',
      align: 'center',
      width: 100
    }, {
      key: 4,
      title: '数量',
      dataIndex: 'qty',
      align: 'center',
      width: 80
    }, {
      key: 5,
      title: '金额',
      dataIndex: 'totalAmt',
      align: 'center',
      width: 100
    }]
  }

  componentDidMount() {
    this.getPrintData();
    this.getTableColumns();
  }

  // 获取表头数据
  getTableColumns = async () => {
    try {
      let data = {
        params: [{
          compontName: 'ApplicationFormForGlasses',
          type: 'C',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      let columns = res?.result?.C || [];
      if (columns && columns.length > 0) {
        this.setState({
          columns,
          totalWidth: res.totalWidth
        })
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 获取验光单数据
  getPrintData = async () => {
    try {
      let { currentPatData } = this.state;
      let data = {
        params: [{
          admID: currentPatData?.admID || ''
        }]
      }
      let res = await React.$asyncPost(this, this.props.queryCode || '17030074', data);
      let printData = res?.result || [];
      for (var i = 0; i < printData.length; i++) {
        printData[i].checked = true;
        let ordArr = printData[i]?.ordArr || [];
        for (var j = 0; j < ordArr.length; j++) {
          let ordItem = printData[i].ordArr[j];
          printData[i].ordArr[j] = {
            ...ordItem,
            ...(ordItem?.priceObj || {}),
            key: j + 1
          }
        }
      }
      this.setState({ printData });
    } catch (error) {
      this.setState({ printData: [] });
    }
  }

  // 打印勾选
  handleCheckboxChange = (e, index) => {
    let { printData } = this.state;
    printData[index].checked = e.target.checked;
    this.setState({});
  }

  // 打印
  handlePrint = async () => {
    try {
      let { printData, currentPatData } = this.state;
      let glassesRequestIDArr = [];
      for (var i = 0; i < printData.length; i++) {
        if (printData[i].checked) {
          glassesRequestIDArr.push(printData[i]?.glassesRequestID || '');
        }
      }
      if (!(glassesRequestIDArr && glassesRequestIDArr.length > 0)) {
        message.error('请选择需要打印的数据');
        return;
      }
      let data = {
        params: [{
          glassesRequestIDArr,
          admID: currentPatData?.admID || '',
        }]
      }
      let res = await React.$asyncPost(this, this.props.printCode || '17030084', data);
      let dataArr = res?.result || [];
      dataArr && dataArr.map((item, i) => {
        $loadTemplate(item.template, item.printStr);
      });
    } catch (error) {
      console.log(error);
    }
  }

  render() {
    const { printData, columns, totalWidth } = this.state;
    return (
      <div className="application-form-for-glasses">
        <div style={{ textAlign: 'right', marginBottom: 12 }}>
          <Button type="primary" onClick={this.handlePrint}>打印</Button>
        </div>
        <div className="scroll-bar-style" style={{ height: store.getState().tableHeight.y + 70 + 'px', overflow: 'auto', paddingRight: 100 }}>
          {printData && printData.map((item, index) => {
            let optometryObj = item?.optometryObj || {};
            let patObj = item?.patObj || {};
            let requestObj = item?.requestObj || {};
            let admObj = item?.admObj || {};
            return (
              <div key={index} className="affg-print-item">
                <Row>
                  <Col span={17}>
                    <p>NO. {requestObj?.reqNo || admObj?.reqNo || ''}</p>
                    <p className="affg-row">
                      <span style={{ color: '#000', fontSize: '16px' }}>{patObj?.patName || admObj?.patName || ''}</span>
                      <span>{patObj?.patSexDesc || admObj?.patSexDesc || ''}</span>
                      <span>{patObj?.patAge || admObj?.patAge || ''}</span>
                      <span>{patObj?.patBirthDate || admObj?.patBirthDate || ''}</span>
                      <span>电话：{patObj?.patMobile || admObj?.patMobile || ''}</span>
                    </p>
                    <p className="flex-between-center" style={{ paddingRight: 24 }}>
                      <span>入单人：{requestObj?.createUserDesc || admObj?.docDesc || ''}</span>
                      <span>{requestObj?.createDateTime || admObj?.admDate || ''}</span>
                    </p>
                    <PublicTablePagination
                      param={{
                        y: 'N',
                        rowKey: 'key',
                        height: 'auto',
                        x: totalWidth,
                        bordered: false,
                        data: item?.ordArr || [],
                        componentName: 'ApplicationFormForGlasses',
                        columns: columns && columns.length > 0 ? columns : this.staticOrdColumns,
                      }}
                      getColumns={this.getTableColumns}
                    />
                  </Col>
                  <Col className="affg-print-item-optometry" span={7} style={{ paddingLeft: 24 }}>
                    <div style={{ textAlign: 'right' }}>
                      <Checkbox checked={item.checked} onChange={(e) => this.handleCheckboxChange(e, index, item)}>打印</Checkbox>
                    </div>
                    远用处方
                    <div>
                      右：{optometryObj?.disRight || ''}
                    </div>
                    <div style={{ marginBottom: 6 }}>
                      左：{optometryObj?.disLeft || ''}
                    </div>
                    近用处方
                    <div>
                      右：{optometryObj?.nearRight || ''}
                    </div>
                    <div>
                      左：{optometryObj?.nearLeft || ''}
                    </div>
                    <div>
                      瞳距远用：{optometryObj?.field35 || ''}
                    </div>
                    <div>
                      瞳距近用：{optometryObj?.field36 || ''}
                    </div>
                    <div>
                      ADD：{optometryObj?.ADD || ''}
                    </div>
                  </Col>
                </Row>
                <Row>
                  <Col span={16}>
                    <p className="affg-print-item-footer-row">
                      <span>总金额：{requestObj?.sumAmt || admObj?.sumAmt || ''}</span>
                      <span>预交金：{requestObj?.depositAmt || admObj?.depositAmt || 0}</span>
                      <span>余款：{(Number(requestObj?.sumAmt || admObj?.sumAmt || 0)) - (Number(requestObj?.depositAmt || admObj?.depositAmt || 0))}</span>
                    </p>
                    <p className="affg-print-item-footer-row">
                      <span>验光师：{requestObj?.optometryUserDesc || admObj?.optometryUserDesc || ''}</span>
                      <span>经手人：{requestObj?.createUserDesc || admObj?.docDesc || ''}</span>
                      <span>医生：{requestObj?.docDesc || admObj?.docDesc || ''}</span>
                    </p>
                  </Col>
                  <Col span={8}>
                    {(patObj?.patNo || admObj?.patNo) ? <AcBarcode value={patObj?.patNo || admObj?.patNo} width={2} height={48} fontSize={14} margin={3} /> : ''}
                  </Col>
                </Row>
              </div>
            )
          })}
        </div>
      </div>
    )
  }
};

export default ApplicationFormForGlasses;