/*
 * Create:      柿子
 * CreateDate:  2022/12/10
 * Describe：   查看验光单 / 查看已写过的病历
 * */
import React, { Component } from 'react';
import { Modal, Empty, Spin, Button } from 'antd';
import { $loadTemplate } from 'pages/lodop/Common/Print.js';
import ElectronicCaseTemplate from 'pages/common/ElectronicCaseTemplate.jsx';
import CaseInformationLogNew from 'pages/newEmr/CaseInformationLog';
class ViewWrittenMedicalRecords extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      rowData: {},
      loading: false,
      emrLoadingFlag: true, // 是否加载病历
      recordData: {
        emrVisible: false,
        emrAdmID: '',
        recordID: '',
        emrToken: ''
      },
    };
  }

  componentDidMount () {
    this.props.onRef && this.props.onRef(this);
  }

  // 修改弹窗状态
  modifyVisible = (visible, rowData) => {
    if (rowData?.recordData?.emrVisible) {
      this.setState({
        recordData: rowData.recordData,
        visible: visible,
        emrLoadingFlag: true,
        rowData: rowData.row,
      });
      return;
    }
    let emrLoadingFlag = rowData && 'admID' in rowData && rowData.admID && 'patEMRRecordID' in rowData && rowData.patEMRRecordID;
    this.setState({ visible, rowData, emrLoadingFlag, recordData: {} }, () => {
      emrLoadingFlag && visible && this.handleLoadingMedicalRecords();
    });
  }

  // 病历加载
  handleLoadingMedicalRecords = async () => {
    try {
      this.setState({ loading: true });
      const { rowData } = this.state;
      let data = {
        params: [{
          patEMRRecordID: rowData?.patEMRRecordID || '',
          admID: rowData?.admID || '',
          power: ''
        }]
      }
      let res = await React.$asyncPost(this, '06050008', data);
      this.setState({ loading: false });
      let templatePath = res.result?.templatePath || '';
      templatePath ? this.caseTemplateChild && this.caseTemplateChild.setElementTextByID(
        templatePath,
        res.result?.dataSource || [],
        'N',
        "boolFalse",
        rowData?.patSexDesc || '',
      ) : this.caseTemplateChild && this.caseTemplateChild?.NewFile();
    } catch (error) {
      this.setState({ loading: false });
    }
  };

  // 打印【验光单(N) | 处方单(Y)】
  handlePrint = async (type) => {
    try {
      const { rowData } = this.state;
      let data = {
        params: [{
          patOptometryRecordIDArr: [(rowData?.optometryRecordID || '')],
          prescFlag: type
        }]
      }
      let res = await React.$asyncPost(this, '17030079', data);
      let dataArr = res?.result || [];
      dataArr && dataArr.map((item, i) => {
        $loadTemplate(item.template, item.printStr);
      });
    } catch (error) {
      console.log(error);
    }
  }

  render () {
    const { title, editorID } = this.props;
    const { visible, emrLoadingFlag, loading } = this.state;
    let param = {
      isDelete: true,
      formView: 'Normal',
      isBasicOperation: true,
      prCont: false,
      isFont: true,
      isInsert: 'hidden',
      noPrintPreviewFlag: 'N', // 加载病历不打印预览
    }

    return (
      <Modal
        width="56vw"
        title={title || '查看病历'}
        destroyOnClose={true}
        footer={null}
        visible={visible}
        forceRender={true}
        bodyStyle={{ padding: '12px' }}
        onCancel={() => this.modifyVisible(false)}
      >
        <Spin tip="数据加载中，请稍后……" spinning={loading}>
          <div style={{ minHeight: '450px' }}>
            <div style={{ marginBottom: '12px', textAlign: 'right', paddingRight: '20px' }}>
              <Button ghost type="primary" style={{ marginRight: '20px' }} onClick={() => this.handlePrint('N')}>验光单打印</Button>
              <Button ghost type="primary" onClick={() => this.handlePrint('Y')}>处方单打印</Button>
            </div>
            <div style={{ height: '72vh', display: emrLoadingFlag ? 'block' : 'none' }}>
              {this.state.recordData.emrVisible
                ? <CaseInformationLogNew
                  token={this.state.recordData.emrToken}
                  admID={this.state.recordData.admID}
                  hideSort={true}
                  recordID={this.state.recordData.recordID}
                />
                : <ElectronicCaseTemplate
                  param={param}
                  editorID={editorID || 'ViewWrittenMedicalRecords'}
                  onRef={ref => this.caseTemplateChild = ref}
                />}
            </div>
            {!emrLoadingFlag ? <Empty style={{ marginTop: 200 }} description="暂无数据" /> : ''}
          </div>
        </Spin>
      </Modal>
    )
  }
};

export default ViewWrittenMedicalRecords;