<template>
  <page-layer>
    <template #header>
      <el-date-picker v-model="dateRange"
                      :shortcuts="shortcuts"
                      style="width: 220px"
                      end-placeholder="结束日期"
                      range-separator="至"
                      start-placeholder="开始日期"
                      type="daterange"/>
      <el-checkbox v-model="history" label="历史" @change="historyFunc"/>
      <el-button icon="Search" type="primary" @click="getHuiZhenDataClick">检索</el-button>
      <el-button icon="Upload" type="primary" @click="baoCunHuiZhenClick" v-if="!history">保存</el-button>
      <el-button icon="Printer" type="success" @click="daYingClick">打印</el-button>
      <el-button icon="View" type="info" @click="dianJiChaKanHuanZheJianYan">查看患者检验</el-button>
      <el-button type="primary" @click="enterMedicalOrder" v-if="!history">医嘱录入</el-button>
      <el-tag type="danger">会诊意见限制 1100 个字</el-tag>
    </template>
    <template #main>
      <el-container>
        <el-aside width="500px">
          <xc-table :local-data="huanZeData" :final-height="windowSize.h / 1.1" @row-click="dianJiDanYuanGe">
            <el-table-column label="床号" prop="bedNo" width="40px"></el-table-column>
            <el-table-column label="住院号_次数" prop="inpatientNo">
              <template #default="scope"> {{ scope.row.inpatientNo }}_{{ scope.row.admissTimes }}</template>
            </el-table-column>
            <el-table-column label="姓名_申请次数" prop="name">
              <template #default="scope"> {{ scope.row.name }}_{{ scope.row.reqTimes }}</template>
            </el-table-column>
            <el-table-column label="患者科室" prop="deptCodeName">
              <template #default="scope">
                {{ scope.row.deptCodeName }}
              </template>
            </el-table-column>
            <el-table-column label="会诊科室" prop="reqDept1Name"></el-table-column>
            <el-table-column label="会诊级别" prop="hzType">
              <template #default="scope">
                {{ hzJiBie(scope.row.hzType) }}
              </template>
            </el-table-column>
          </xc-table>
        </el-aside>
        <el-main>
          <div style="overflow: auto;" :style="{height: windowSize.h - 30 + 'px'}">
            <print-the-consultation-form ref="daYingHuiZhenRef"/>
          </div>

        </el-main>
      </el-container>
    </template>
  </page-layer>
</template>

<script setup>
import {getHuanZheXinXi, getHuiZhenData, wanChenHuiZhen} from '@/api/case-front-sheet/jie-shou-hui-zhen'
import store from '@/store'
import {getDateRangeFormatDate, getDatetime, getFormatDatetime} from '@/utils/date'
import {ElMessage, ElMessageBox} from 'element-plus'
import {computed, onMounted, ref} from 'vue'
import {stringIsBlank} from '@/utils/blank-utils'
import {shortcuts} from '@/data/shortcuts'
import router from '@/router'
import {getServerDateApi} from '@/api/public-api'
import PageLayer from "@/layout/PageLayer";
import PrintTheConsultationForm from "@/components/hui-zhen-da-ying/PrintTheConsultationForm.vue";
import XcTable from "@/components/xiao-chan/xc-table/XcTable.vue";

////////////////////////////// 获取屏幕高度 /////////////////////////////////////////

const windowSize = computed(() => {
  return store.state.app.windowSize
})

const daYingHuiZhenRef = ref(null)

onMounted(() => {
  dateRange.value[0] = shortcuts[0].value[0]
  dateRange.value[1] = shortcuts[0].value[1]
  getHuiZhenDataClick()
})

////////////////////////////// 获取患者信息    /////////////////////////////////////////
const dateRange = ref([])
const huanZeData = ref([])
const history = ref(true)

const getHuiZhenDataClick = () => {
  let {startTime, endTime} = getDateRangeFormatDate(dateRange.value)
  getHuiZhenData(startTime, endTime, history.value).then((res) => {
    console.log(res)
    huanZeData.value = res
  })
}

const historyFunc = () => {
  huanZeData.value = []
  huanZheXinXi.value = {}
  daYingHuiZhenRef.value.huiZhenXinXiXiangQing({})
}

//////////////////////////////////////////// 点击右边的表格 ///////////////////////////////////////////////////////////
const huanZheXinXi = ref({})

const dianJiDanYuanGe = (val) => {
  getHuanZheXinXi(val.admissTimes, val.reqTimes, val.inpatientNo).then((res) => {
    huanZheXinXi.value = res
    daYingHuiZhenRef.value.huiZhenXinXiXiangQing(res)
  })
}

/**
 * 点击保存 会诊信息
 * 要判断 一下 是仅保存 还是 完成会诊
 */
const baoCunHuiZhenClick = () => {
  if (!huanZheXinXi.value.name) {
    ElMessage({
      message: '请先选择患者',
      showClose: true,
      type: 'error',
    })
    return
  }
  if (stringIsBlank(huanZheXinXi.value.hzComment)) {
    return ElMessage({
      message: '请填写会诊意见',
      showClose: true,
      type: 'error',
    })
  }
  ElMessageBox.confirm('是否保存【' + huanZheXinXi.value.name + '】的会诊信息', '提示', {
    type: 'warning',
    distinguishCancelAndClose: true,
    cancelButtonText: '仅保存',
    confirmButtonText: '完成会诊',
  })
      .then(() => {
        huanZheXinXi.value.statusFlag = 2
        // 完成 会诊 发送 axios
        wanChenHuiZhen(huanZheXinXi.value)
        getHuiZhenDataClick()
        huanZheXinXi.value = {}
        daYingHuiZhenRef.value.huanZheXinXi.value = {}
      })
      .catch((e) => {
        if (e === 'cancel') {
          huanZheXinXi.value.statusFlag = 1
          wanChenHuiZhen(huanZheXinXi.value)
        }
      })
}

///////////////////////////////////////////////////////// 开启打印 ///////////////////////////////////////////////////////////////////////////////

const daYingClick = () => {
  if (!huanZheXinXi.value.inpatientNo) {
    ElMessage.error({
      message: '请先选择患者',
      showClose: true,
    })
    return
  }
  daYingHuiZhenRef.value.daYing()
}

///////////////////////////// 查看患者检验
const dianJiChaKanHuanZheJianYan = () => {
  if (!huanZheXinXi.value.inpatientNo) {
    return ElMessage({
      message: '请先选择患者',
      showClose: true,
      type: 'error',
    })
  } else {
    let end = ''
    getServerDateApi().then((res) => {
      end = getFormatDatetime(res, 'YYYY-MM-DD')
      router.push({
        name: 'inspectionReportIndex',
        params: {
          passRule: true,
          patNo: huanZheXinXi.value.inpatientNo,
          start: huanZheXinXi.value.admissDate,
          end: end,
        },
      })
    })
  }
}

// 跳转到医嘱录入页面
const enterMedicalOrder = () => {
  if (!huanZheXinXi.value.inpatientNo) {
    return ElMessage({
      message: '请先选择患者',
      showClose: true,
      type: 'error',
    })
  } else {
    router.push({
      name: 'yiZhuLuRu',
      params: {
        inpatientNo: huanZheXinXi.value.inpatientNo,
        admissTimes: huanZheXinXi.value.admissTimes
      }
    })
  }
}

// 会诊级别
function hzJiBie(val) {
  switch (val) {
    case '1':
      return '主治医师'
    case '2':
      return '副主任医生'
    case '3':
      return '主任医生'
  }
}

function hzLeiBie(val) {
  switch (val) {
    case '1':
      return '常规会诊'
    case '2':
      return '疑难病例会诊'
    case '3':
      return '急救会诊'
  }
}
</script>

<style></style>
