/*
 * Create:      柿子
 * CreateDate:  2024/02/28
 * Describe：   护士操作功能组件
 * */
import React, { Component } from 'react';
import { message } from 'antd';
import { $loadTemplate } from 'pages/lodop/Common/Print.js';
import DivideBed from './component/DivideBed'; // 分床
import TransferDepartment from './component/TransferDepartment'; // 转科
import DischargeFromHospital from './component/DischargeFromHospital'; // 退院 - 移入退院区
import WardInformationDetails from './component/WardInformationDetails'; // 病区信息明细
import HandlingDischargeProcedures from './component/HandlingDischargeProcedures';
import ShowMedicalRecordModal from 'pages/workstation/component/ShowMedicalRecordModal';
import ModificationPatientInfo from 'pages/registercard/component/ModificationPatientInfo';
import PatientInfoDetail from 'pages/nurse/Patient/PatientInfoDetail'; // 修改入院信息
import PatientCourseInfo from 'pages/nurse/Patient/PatientCourseInfo'; // 患者历程

class NurseFunctions extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userData: React.$getSessionData(),
      emrVisible: false,
      emrAdmID: '',
    }
  }

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

  // 护士站操作方法集【栗：选中一床之后鼠标移入其他床位上点击换床，rowData：当前操作床位信息 bedInfo：选中床位信息】
  handleMethodSet = (code, bedInfo, rowData) => {
    console.log('code', code);
    console.log('bedInfo', bedInfo);
    console.log('rowData', rowData);
    switch (code) {
      case 'BedCard': // 打印床头卡
        this.handlePrintBedCard(bedInfo, 'BedCard');
        break;
      case 'WristBand': // 打印腕带
        this.handlePrintBedCard(bedInfo, 'Wristband');
        break;
      case 'ModifyPatInfo': // 修改病人信息
        this.handleModifyPatInfo(bedInfo);
        break;
      case 'ModifyAdmInfo': // 修改入院信息
        this.patInfoDetailRef.modifyVisible(true, bedInfo?.admID || '');
        break;
      case 'LookUpEMR': // 查看病历
        this.modifyEMRModalVisible(true, bedInfo);
        break;
      case 'Discharge': // 出院
        this.dischargeProceduresRef?.modifyVisible(true, bedInfo);
        break;
      case 'TransLoc': // 转科
        this.transLocRef?.modifyVisible(true, bedInfo);
        break;
      case 'MoveToWaitZone': // 移入等候区
        this.handleMoveToWaitZone(bedInfo, 'WaitRoom');
        break;
      case 'MoveToDisWaitZone': // 移入出院等候区
        this.handleMoveToWaitZone(bedInfo, 'DisWaitRoom');
        break;
      case 'MoveToWithdrawalArea': // 退院 - 移入退院区
        this.dischargeFormRef?.modifyVisible(true, bedInfo);
        break;
      case 'WardInfo': // 病区信息
        this.wardInfoRef?.modifyVisible(true, bedInfo);
        break;
      case 'PatientCourse': // 患者历程
        this.patConsumeRef?.showModal(bedInfo?.admID || '', bedInfo?.patID || '');
        break;
      case 'SeparateBeds': // 分床
        this.divideBedRef?.modifyVisible(true, bedInfo);
        break;
      case 'AllotBed': // 换床
        this.handleAllotBed(bedInfo, rowData);
        break;
      case 'WrappedBed': // 包床
        this.handleWrappedBed(bedInfo, rowData?.admID || '', '2');
        break;
      case 'CancelWrappedBed': // 结束包床
        this.handleWrappedBed(bedInfo, bedInfo?.admID || '', '4');
        break;
      case 'BedOccupancy': // 占床
        this.handleBedOccupancy(bedInfo, '04');
        break;
      case 'CancelBedOccupancy': // 取消占床
        this.handleBedOccupancy(bedInfo, '02');
        break;
      default:
        console.log('code', code);
    }
  };

  // 修改患者信息
  handleModifyPatInfo = async (record) => {
    try {
      let data = {
        params: [{
          ...record
        }]
      }
      let res = await React.$asyncPost(this, '03050274', data);
      this.modifyPatInfoChild && this.modifyPatInfoChild.showModifDom(res.result);
    } catch (error) {
      console.log(error);
    }
  };

  // 修改病历弹窗
  modifyEMRModalVisible = (visible, record) => {
    if (visible && !(record && record.admID)) {
      message.error('就诊ID不存在，请稍后重试！');
      return;
    }
    this.setState({ emrVisible: visible, emrAdmID: record?.admID || '' });
  };

  // 打印腕带
  handlePrintBedCard = async (record, type, e) => {
    try {
      let data = {
        params: [{
          admID: record?.admID || '',
          bedID: record?.bedID || '',
        }]
      }
      let res = await React.$asyncPost(this, type === 'BedCard' ? '04140012' : '04140013', data);
      // BedCard - 打印床头卡 | Wristband - 打印腕带
      let templateCode = (type === 'BedCard') ? 'BedCard' : 'Wristband';
      $loadTemplate(templateCode, res.result); // 加载模板
    } catch (error) {
      console.log(error);
    }
  };

  // 移入等候区
  handleMoveToWaitingArea = async (record, type) => {
    try {
      let data = {
        params: [{
          admID: record?.admID || '',
          bedID: record?.id || record?.bedID || '',
          type: ''
        }]
      }
      let res = await React.$asyncPost(this, '04020003', data);
      console.log(res);
    } catch (error) {
      console.log(error);
    }
  };

  // 换床
  handleAllotBed = async (bedInfo, record) => {
    try {
      const { userData } = this.state;
      let data = {
        params: [{
          patInfo: { // 当前床位
            bedID: record?.id || record?.bedID || '',
            userID: userData?.userID || '',
            admID: record?.admID || '',
            hospID: userData?.hospID || '',
            transType: '4',
            transStatus: '1',
          },
          exchangePatInfo: { // 换床床位
            bedID: bedInfo?.id || bedInfo?.bedID || '',
            userID: userData?.userID || '',
            admID: bedInfo?.admID || '',
            hospID: userData?.hospID || '',
            transType: '4',
            transStatus: '1',
          }
        }]
      }
      let res = await React.$asyncPost(this, '04020005', data);
      message.success(res?.errorMessage || '换床成功!');
      this.props.handleQuery && this.props.handleQuery();
    } catch (error) {
      console.log(error);
    }
  };

  // 包床 | 取消包床
  handleWrappedBed = async (bedInfo, admID, flag) => {
    try {
      const { userData } = this.state;
      let data = {
        params: [{
          bedID: bedInfo?.id || bedInfo?.bedID || '',
          userID: userData?.userID || '',
          admID,
          hospID: userData?.hospID || '',
          status: flag, // 2为包床
        }]
      }
      let res = await React.$asyncPost(this, '04020006', data);
      message.success(res?.errorMessage || (flag === '2' ? '包床成功!' : '结束包床成功'));
      this.props.handleQuery && this.props.handleQuery();
    } catch (error) {
      console.log(error);
    }
  };

  // 占床 | 取消占床
  handleBedOccupancy = async (record, statusCode) => {
    try {
      let data = {
        params: [{
          bedStatuCode: statusCode,
          bedID: record?.id || record?.bedID || '',
        }]
      }
      let res = await React.$asyncPost(this, '04020026', data);
      message.success(res.errorMessage || (statusCode === '04' ? '占床成功' : '取消占床成功'));
      this.props.handleQuery && this.props.handleQuery();
    } catch (error) {
      console.log(error)
    }
  };

  // 移入等候区 | 移入出院等候区
  handleMoveToWaitZone = async (bedInfo, statusCode) => {
    try {
      const { userData } = this.state;
      let data = {
        params: [{
          bedID: '',
          userID: userData?.userID || '',
          admID: bedInfo?.admID || '',
          hospID: userData?.hospID || '',
          transType: '1',
          status: '3',
          transStatus: '0',
          code: statusCode
        }]
      }
      let res = await React.$asyncPost(this, '04020003', data);
      message.success(res?.errorMessage || '操作成功');
      this.props.handleQuery && this.props.handleQuery();
    } catch (error) {
      console.log(error)
    }
  };

  render() {
    const { modifyPatInfoFlag, lookUpEMRFlag, separateBedsFlag, dischargeFlag, transLocFlag, moveToWithdrawalAreaFlag, wardInfoFlag, modifyAdmInfoFlag, patientCourseFlag } = this.props;
    const { emrVisible, emrAdmID } = this.state;

    return (
      <div>
        {/* 修改病人信息 */}
        {modifyPatInfoFlag !== 'N' && (
          <ModificationPatientInfo
            OnChildref={ref => this.modifyPatInfoChild = ref}
            position="fixed"
          />
        )}

        {/* 查看病历 */}
        {emrVisible && lookUpEMRFlag !== 'N' ? (
          <ShowMedicalRecordModal
            admTypeCode="O"
            viewEMRFlag="Y"
            prCont="N"
            initTemplate="N"
            modalTitle="查看病历"
            isOperable={false}
            isBasicOperation="hidden"
            isShow={emrVisible} // 查看病历按钮显示隐藏控制标志
            historyAdmID={emrAdmID}
            onHide={() => this.modifyEMRModalVisible(false, {})}
          />
        ) : ''}

        {/* 分床 */}
        {separateBedsFlag !== 'N' && (
          <DivideBed onRef={ref => this.divideBedRef = ref} />
        )}

        {/* 出院 */}
        {dischargeFlag !== 'N' && (
          <HandlingDischargeProcedures onRef={ref => this.dischargeProceduresRef = ref} />
        )}

        {/* 转科 */}
        {transLocFlag !== 'N' && (
          <TransferDepartment onRef={ref => this.transLocRef = ref} />
        )}

        {/* 退院 - 移入退院区 */}
        {moveToWithdrawalAreaFlag !== 'N' && (
          <DischargeFromHospital onRef={ref => this.dischargeFormRef = ref} />
        )}

        {/* 病区信息明细 */}
        {wardInfoFlag !== 'N' && (
          <WardInformationDetails onRef={ref => this.wardInfoRef = ref} />
        )}

        {/* 病人基本信息详情 - 修改入院信息 */}
        {modifyAdmInfoFlag !== 'N' && (
          <PatientInfoDetail
            onRef={ref => this.patInfoDetailRef = ref}
            getPatients={this.getObtainChangedData}
          />
        )}

        {/* 患者历程 */}
        {patientCourseFlag !== 'N' && (
          <PatientCourseInfo
            onRef={ref => this.patConsumeRef = ref}
          />
        )}
      </div>
    )
  }
};

export default NurseFunctions;