/*
 * Creator:     刘丹
 * CreatDate:   2023-11-13
 * descripts：  门诊医生医嘱录入  汇总打印 
*/
import React from 'react';
import { Tabs } from 'antd';
import store from 'store';
import { $http } from 'containers/config/https';
import "./style/SummaryPrinting.less"
import { Util } from 'tools/index';
import PrintingOneStep from 'pages/workstation/component/PrintingOneStep'; // 一键打印
import PrintingPrescription from 'pages/workstation/component/PrintingPrescription'; // 处方打印
import PrintingApplication from 'pages/workstation/component/PrintingApplication'; // 申请单打印
// import PrintingTreatmentSheet from './component/PrintingTreatmentSheet'; // 治疗单打印
import InspectionNoticePage from 'pages/workstation/component/InspectionNoticePage'; // 检验告知单
import PrintingGuidanceSheet from 'pages/workstation/component/PrintingGuidanceSheet'; // 导诊单打印
import SalesOrderPrinting from 'pages/optometry/printTemplate/SalesOrderPrinting'; // 销售单打印
import SoftMirrorPrescription from 'pages/optometry/printTemplate/SoftMirrorPrescription'; // 软镜处方打印
/*以下为新病历新复制的打印文件start*/
import ApplicationFormForGlasses from './component/ApplicationFormForGlasses'; // 配镜申请单
import PrescriptionPrinting from './component/PrescriptionPrinting'; // 处方单打印
import OptometrySheet from './component/OptometrySheet'; // 验光单打印
/*以下为新病历新复制的打印文件end*/
const TabPane = Tabs.TabPane;

class EmrSummaryPrint extends React.Component {
  constructor(porps) {
    super(porps);
    let userData = React.$getSessionData();
    this.state = {
      userData,
      activeKey: userData?.groupID === '375' ? '7' : '5', // 验配师默认显示配镜申请单打印
      userInfo: {}, //用户数据
      categoryData: {},
    }
  }

  componentDidMount = () => {
    let newCategory;
    if ('paras' in this.props && this.props.paras && this.props.paras.params) { // 侧菜单获取类别参数
      newCategory = this.props.paras.params;
    } else {
      if ('location' in this.props && this.props.location.state && this.props.location.state.params) { // 头菜单获取类别参数
        newCategory = this.props.location.state.params;
        React.$setSessionData('SummaryPrinting', newCategory, false);
      } else { // 头菜单刷新界面时获取类别
        newCategory = React.$getSessionData('SummaryPrinting', false);
      }
    }
    let categoryData = Util.getObjByUrlStr(newCategory);
    this.setState({ categoryData }, () => {
      if (categoryData?.interfaceType !== 'optometry') { // 视光专用
        this.getOneStepData()
      }
      this.childStep && this.childStep.getOneStepData && this.childStep.getOneStepData();
      this.getFormDate()
    });
  }

  getOneStepData = () => {
    let data = {}
    data = {
      params: [{
        userID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
        admID: sessionStorage.patientData ? JSON.parse(sessionStorage.patientData).admID : "",
        hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : ""
      }],
    }
    $http.post('urlS', this, {
      code: '03050148',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            btnStatus: res.button
          })
          if (res.button && res.button.onekey && res.button.onekey != 'Y') {
            this.setState({
              activeKey: '4' //如果不显示一键打印就默认显示导诊单
            })
          }
        }
      },
    })
  }

  // 获取form表单字段
  getFormDate = () => {
    let userData = JSON.parse(sessionStorage.getItem('userData'));
    this.setState({
      userData,
    }, () => {
      let data = {
        params: [{
          compontName: "Print", // 汇总打印
          reactCode: ['Print'],
          type: 'C',
        }]
      }
      $http.post('urlS', this, {
        code: '01040073',
        data: data,
        success: function (res) {
          if (+res.errorCode === 0) {
            this.setState({ localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN, })
          }
        }
      })
    })
  }

  // 导航栏切换
  callback = (key) => {
    this.setState({ activeKey: key })
    if (key == "1") {
      this.childRef && this.childRef.getPrescription && this.childRef.getPrescription()
    } if (key == "5") {
      this.childStep && this.childStep.getOneStepData && this.childStep.getOneStepData()
    }
  }

  onRef = (ref) => {
    this.childRef = ref
  }

  onStep = (ref) => {
    this.childStep = ref
  }

  redireBack = () => {
    let routerData = store.getState().otherData.routerData;
    let path = '';
    for (var i = 0; i < routerData.length; i++) {
      if (routerData[i].title === '用户信息') {
        path = routerData[i].path;
        break;
      }
    }
    this.props.history.push(path);
  }

  render () {
    const { categoryData } = this.state;
    let tabsHeight = store.getState().tableHeight.y + 150 + 'px'
    return (
      <div className="summaryPrintingDom" style={{ borderTop: '1px solid #e8e8e8' }}>
        <Tabs
          activeKey={this.state.activeKey}
          onChange={this.callback}
          tabPosition="left"
          type="card"
          className="new_department_tabs"
          style={{ width: '100%', height: tabsHeight }}
        >
          {(!Util.isEmpty(this.state.btnStatus) && this.state.btnStatus.onekey == 'Y') && categoryData?.interfaceType !== 'optometry' ? <TabPane tab="一键打印" key="5">
            <PrintingOneStep callback={this.callback} onStep={this.onStep}></PrintingOneStep>
          </TabPane> : null}
          {(!Util.isEmpty(this.state.btnStatus) && this.state.btnStatus.presc == 'Y') && categoryData?.interfaceType !== 'optometry' ? <TabPane tab="处方打印" key="1">
            <PrintingPrescription localeProvider={this.state.localeProvider} onRef={this.onRef} />
          </TabPane> : null}
          {(!Util.isEmpty(this.state.btnStatus) && this.state.btnStatus.application == 'Y') && categoryData?.interfaceType !== 'optometry' ? <TabPane tab="申请单打印" key="2">
            <PrintingApplication localeProvider={this.state.localeProvider} />
          </TabPane> : null}
          {/* <TabPane tab="治疗单打印" key="3"> */}
          {/* <PrintingTreatmentSheet /> */}
          {/* </TabPane> */}
          {(!Util.isEmpty(this.state.btnStatus) && this.state.btnStatus.inspection == 'Y') && categoryData?.interfaceType !== 'optometry' ? <TabPane tab="检验告知单" key="6">
            <InspectionNoticePage />
          </TabPane> : null}
          {(!Util.isEmpty(this.state.btnStatus) && this.state.btnStatus.guide == 'Y') && categoryData?.interfaceType !== 'optometry' ? <TabPane tab="导诊单打印" key="4">
            <PrintingGuidanceSheet localeProvider={this.state.localeProvider} />
          </TabPane> : null}
          {categoryData?.interfaceType === 'optometry' ? <TabPane tab="验光单打印" key="5">
            <OptometrySheet />
          </TabPane> : null}
          {/* 门诊也需要打印处方单，根据标志判断 */}
          {categoryData?.interfaceType === 'optometry' || (!Util.isEmpty(this.state.btnStatus) && this.state.btnStatus.optoPresc == 'Y') ? (
            <TabPane tab={categoryData?.interfaceType === 'optometry' ? '处方单打印' : '配镜处方'} key="16">
              <PrescriptionPrinting />
            </TabPane>
          ) : null}
          {categoryData?.interfaceType === 'optometry' ? <TabPane tab="配镜申请单打印" key="7">
            <ApplicationFormForGlasses />
          </TabPane> : null}
          {categoryData?.interfaceType === 'optometry' ? <TabPane tab="配镜申请单打印（其他）" key="17">
            <ApplicationFormForGlasses queryCode="17080003" printCode="17030090" />
          </TabPane> : null}
          {categoryData?.interfaceType === 'optometry' ? <TabPane tab="软镜处方打印" key="18">
            <SoftMirrorPrescription />
          </TabPane> : null}
          {categoryData?.interfaceType === 'optometry' ? <TabPane tab="销售单打印" key="19">
            <SalesOrderPrinting />
          </TabPane> : null}
        </Tabs>
      </div >
    )
  }
}
export default EmrSummaryPrint
