<template>
  <div ref="headerRef">
    <div v-show="!isDualScreen">
      <emr-select-pat :pat-info="patientInfo" @selected="selected"/>
      住院号：
      <el-input v-model="query.patNo" style="width: 120px"/>
      次数 ({{ query.times }}) ：
      <emr-leave-hospital-patient @rowClick="disPatients"/>
      <el-button @click="allPatientsInTheHospital">全院患者</el-button>
      <el-button @click="patientListDrawer = !patientListDrawer">患者列表</el-button>
      出院天数：{{ dischargeDays }}
      <span style="color: red">出院七天后就无法编辑患者病历</span>
    </div>
  </div>
  <div v-if="show">
    <div ref="divRef">
      <huan-zhe-xin-xi v-show="!isDualScreen"
                       :huan-zhe-xin-xi="patientInfo"
                       @isShow="patientInfoIsShow"/>
    </div>
    <emr-main
        ref="emrMainRef"
        :huan-zhe-xin-xi="patientInfo"
        :max-height="maxHeight"
        :is-dual-screen="isDualScreen"/>
  </div>
  <emr-patient-list @rowClick="listRowClick" v-model="patientListDrawer"/>
</template>

<script setup name='Home'>
import EmrMain from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/EmrMain.vue";
import {getDisPatient, getPatientInfo} from "@/api/inpatient/patient";
import router from "@/router";
import HuanZheXinXi from "@/components/zhu-yuan-yi-sheng/HuanZheXinXi.vue";
import {BizException, ExceptionEnum} from "@/utils/BizException";
import {isDisReqEdit} from "@/api/zhu-yuan-yi-sheng/emr-patient";
import {getServerDateApi} from "@/api/public-api";
import {subtractTime} from "@/utils/date";
import {
  emrConfig,
  query,
  resolveRoute
} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";
import EmrPatientList from "@/components/zhu-yuan-yi-sheng/emr/EmrPatientList.vue";
import EmrLeaveHospitalPatient from "@/components/zhu-yuan-yi-sheng/emr/EmrLeaveHospitalPatient.vue";
import EmrSelectPat from "@/components/zhu-yuan-yi-sheng/EmrSelectPat.vue";

const divRef = ref(null)
const headerRef = ref(null)
const emrMainRef = ref(null)
// 最大高度
let maxHeight = $ref()
// 是否显示页面
let show = $ref(false)
// 获取患者信息
let patientInfo = $ref({})
// 距离今天的出院天数
let dischargeDays = $ref(0)
// 患者列表 list
let patientListDrawer = $ref(false)
// 是否是双屏
let isDualScreen = $ref(false)


const selected = ({inpatientNo, admissTimes}) => {
  query.value.patNo = inpatientNo
  query.value.times = admissTimes
  query.value.maxTimes = admissTimes
  allPatientsInTheHospital()
}

// 点击查询出院患者
const disPatients = async () => {
  if (query.value.times === 0 || !query.value.patNo) {
    BizException(ExceptionEnum.MESSAGE_ERROR, '请输入住院号，住院次数不能为 0 ')
  }
  query.value.state = 2
  let te = JSON.stringify(query.value)
  await router.push({
    name: 'emrEditor',
    query: {
      pat: window.btoa(te)
    }
  })
  await routerFunc()
}

// 解析路由数据
const routerFunc = async () => {
  show = false
  if (router.currentRoute.value.query.pat) {
    resolveRoute(router.currentRoute.value.query.pat)
    if (query.value.state !== 2) {
      patientInfo = await getPatientInfo(query.value.patNo);
    }
    if (query.value.state === 1) {
      await queryActPatient()
    } else if (query.value.state === 2) {
      await queryDisPatient()
    } else if (query.value.state === 3) {
      await queryAllPatients(true)
    } else if (query.value.state === 4) {
      await queryAllPatients(false)
    }
    await nextTick()
  } else {
    show = false
  }
}

// 查询在院患者数据
const queryActPatient = async () => {
  show = true
}

// 查询出院患者信息
const queryDisPatient = async () => {
  patientInfo = await getDisPatient(query.value.patNo, query.value.times)
  dischargeDays = subtractTime(await getServerDateApi(), patientInfo.disDate)
  // 如果患者的出院时间大于 7 天就只能看病历和打印病历了
  emrConfig.value.editor = dischargeDays <= 7;
  // 如果超过了七天就去查看是否有申请编辑
  if (!emrConfig.value.editor) {
    emrConfig.value.editor = await isDisReqEdit(query.value.patNo + '_' + query.value.times)
  }
  show = true;
}

// 点击全院患者数据
const allPatientsInTheHospital = async () => {
  if (!query.value.patNo) {
    BizException(ExceptionEnum.MESSAGE_ERROR, '请输入住院号。 ')
  }
  query.value.state = 3
  let te = JSON.stringify(query.value)
  await router.push({
    name: 'emrEditor',
    query: {
      pat: window.btoa(te)
    }
  })
  await routerFunc()
}


const queryAllPatients = async (flag) => {
  query.value.times = patientInfo.admissTimes
  query.value.maxTimes = patientInfo.admissTimes
  show = true
  emrConfig.value.editor = flag
}

const listRowClick = async (val) => {
  let temp = {
    patNo: val.inpatientNo,
    times: val.admissTimes,
    maxTimes: val.admissTimes,
    state: 4
  }
  let te = JSON.stringify(temp)
  await router.push({
    name: 'emrEditor',
    query: {
      pat: window.btoa(te)
    }
  })
  await routerFunc()
}

const patientInfoIsShow = (flag, height) => {
  maxHeight = window.innerHeight - height - 24
}

onMounted(async () => {
  await nextTick()
  await routerFunc()
  //向父项目传值
  window.parent.postMessage("电子病历加载完成。", "*");
  window.dualScreenMode = () => {
    isDualScreen = true
    emrMainRef.value.closeBothSides()
  }
})


</script>

<style scoped lang="scss">

</style>
