<template>
  <page-layer>
    <template #header>
      <el-input v-model="patNo" clearable style="width: 220px">
        <template #prepend>住院号/门诊ID</template>
      </el-input>
      <el-select v-model="medType" style="width: 140px" placeholder="医疗类别">
        <el-option v-for="item in medTypes" :key="item.code" :value="item.code" :label="item.name"></el-option>
      </el-select>
      <el-date-picker
          v-model="dateRange"
          type="daterange"
          :shortcuts="maxHalfYear"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
      ></el-date-picker>
      <el-divider direction="vertical"></el-divider>
      <el-button type="primary" @click="getVisitInfo">就诊信息</el-button>
      <el-button type="primary" @click="getDiseInfo">诊断信息</el-button>
      <el-button type="primary" @click="getSetlInfo">结算信息</el-button>
      <el-button type="primary" @click="getFeeDetl">费用明细</el-button>
      <el-button type="primary" @click="getCumInfo">累计信息</el-button>
    </template>
    <template #main>
      <el-tag type="success">就诊信息</el-tag>
      <el-table :data="mdtrtInfos" stripe highlight-current-row @row-click="handleClickMdtrtInfo">
        <el-table-column prop="mdtrtId" label="就诊ID"></el-table-column>
        <el-table-column prop="psnNo" label="人员编号"></el-table-column>
        <el-table-column prop="psnCertTypeName" label="人员证件类型"></el-table-column>
        <el-table-column prop="certno" label="证件号码"></el-table-column>
        <el-table-column prop="psnName" label="人员姓名"></el-table-column>
        <el-table-column prop="gendName" label="性别"></el-table-column>
        <el-table-column prop="natyName" label="民族"></el-table-column>
        <el-table-column prop="brdy" label="出生日期"></el-table-column>
        <el-table-column prop="insutypeName" label="险种类型"></el-table-column>
        <el-table-column prop="psnTypeName" label="人员类别"></el-table-column>
        <el-table-column prop="insuOptinsName" label="参保地区"></el-table-column>
        <el-table-column prop="empName" label="单位名称"></el-table-column>
        <el-table-column prop="begntime" label="开始时间"></el-table-column>
        <el-table-column prop="endtime" label="结束时间"></el-table-column>
        <el-table-column prop="mdtrtCertTypeName" label="就诊凭证类型"></el-table-column>
        <el-table-column prop="medTypeName" label="医疗类别"></el-table-column>
        <el-table-column prop="opterId" label="经办人ID"></el-table-column>
        <el-table-column prop="opterName" label="经办人姓名"></el-table-column>
        <el-table-column prop="optTime" label="经办时间"></el-table-column>
        <el-table-column prop="iptOtpNo" label="住院号/门诊号"></el-table-column>
        <el-table-column prop="atddrNo" label="主治医生编码"></el-table-column>
        <el-table-column prop="chfpdrName" label="主诊医生姓名"></el-table-column>
        <el-table-column prop="admDeptCodg" label="入院科室编码"></el-table-column>
        <el-table-column prop="admDeptName" label="入院科室名称"></el-table-column>
        <el-table-column prop="admBed" label="入院床位"></el-table-column>
        <el-table-column prop="dscgMaindiagCode" label="住院主诊断编码"></el-table-column>
        <el-table-column prop="dscgMaindiagName" label="住院主诊断名称"></el-table-column>
        <el-table-column prop="dscgDeptCodg" label="出院科室编码"></el-table-column>
        <el-table-column prop="dscgDeptName" label="出院科室名称"></el-table-column>
        <el-table-column prop="dscgBed" label="出院床位"></el-table-column>
        <el-table-column prop="dscgWayName" label="离院方式"></el-table-column>
        <el-table-column prop="memo" label="备注"></el-table-column>
      </el-table>
      <el-dialog v-model="showDiagnosisInfo" title="诊断信息" :close-on-click-modal="false" width="80%">
        <el-table :data="diseInfos" stripe height="400">
          <el-table-column prop="mdtrtId" label="就诊ID"></el-table-column>
          <el-table-column prop="psnNo" label="人员编号"></el-table-column>
          <el-table-column prop="diagInfoId" label="诊断信息ID"></el-table-column>
          <el-table-column prop="inoutDiagTypeName" label="出入院诊断类别"></el-table-column>
          <el-table-column prop="diagTypeName" label="诊断类别"></el-table-column>
          <el-table-column prop="maindiagFlagName" label="主诊断标志"></el-table-column>
          <el-table-column prop="diagSrtNo" label="诊断排序号"></el-table-column>
          <el-table-column prop="diagCode" label="诊断代码"></el-table-column>
          <el-table-column prop="diagName" label="诊断名称"></el-table-column>
          <el-table-column prop="diagDept" label="诊断科室"></el-table-column>
          <el-table-column prop="diseDorNo" label="诊断医生编码"></el-table-column>
          <el-table-column prop="diseDorName" label="诊断医生姓名"></el-table-column>
          <el-table-column prop="diagTime" label="诊断时间"></el-table-column>
          <el-table-column prop="opterId" label="经办人ID"></el-table-column>
          <el-table-column prop="opterName" label="经办人姓名"></el-table-column>
        </el-table>
      </el-dialog>
      <el-dialog v-model="showSetlInfo" title="结算信息" :close-on-click-modal="false" width="70%">
        <Setlinfo :setlinfo="setlInfo"/>
      </el-dialog>
      <MedfeeAnalyse v-if="showFeeDetl" :mdtrt-id="mdtrtInfo.mdtrtId" @close="showFeeDetl = false"/>
      <el-dialog title="人员累计信息查询" v-model="showCumInfo" width="50%">
        <Cuminfo :psn-no="mdtrtInfo.psnNo"/>
      </el-dialog>
    </template>
  </page-layer>
</template>

<script>
import {ref, onMounted} from 'vue'
import {maxHalfYear} from '@/data/shortcuts'
import {getMedAllTypes} from '@/api/medical-insurance/si-dict'
import {ElMessage} from 'element-plus'
import {queryDiagnosisInfo, querySettlementInfo, queryVisitInfo} from '@/api/medical-insurance/si-query'
import {formatDatetime} from '@/utils/date'
import MedfeeAnalyse from '../../../components/medical-insurance/medfee-analyse/Index.vue'
import Setlinfo from '../../../components/medical-insurance/setlinfo/Index.vue'
import Cuminfo from '../../../components/medical-insurance/cuminfo/Index.vue'
import PageLayer from "@/layout/PageLayer";

export default {
  components: {
    PageLayer,
    MedfeeAnalyse,
    Setlinfo,
    Cuminfo,
  },
  setup() {
    const patNo = ref('')
    const medTypes = ref([])
    const medType = ref(null)
    const dateRange = ref(null)
    const mdtrtInfos = ref([])
    const mdtrtInfo = ref({})
    const diseInfos = ref([])
    const setlInfo = ref({})
    const feeDetls = ref([])
    const showDiagnosisInfo = ref(false)
    const showSetlInfo = ref(false)
    const showFeeDetl = ref(false)
    const showCumInfo = ref(false)

    const nullPatNo = () => {
      if (!patNo.value) {
        ElMessage({
          message: '请输入患者住院号/门诊ID！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return true
      }
      return false
    }

    const nullMedType = () => {
      if (!medType.value) {
        ElMessage({
          message: '请选择医疗类别！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return true
      }
      return false
    }

    const nullDateRange = () => {
      if (!dateRange.value) {
        ElMessage({
          message: '请选择日期范围！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return true
      }
      return false
    }

    const nullMdtrtInfo = () => {
      if (!mdtrtInfo.value.mdtrtId) {
        ElMessage({
          message: '请选择一条就诊信息！',
          type: 'warning',
          duration: 2000,
          showClose: true,
        })
        return true
      }
      return false
    }

    const getVisitInfo = () => {
      if (nullPatNo() || nullMedType() || nullDateRange()) {
        return
      }
      const param = {
        patNo: patNo.value,
        medType: medType.value,
        begntime: formatDatetime(dateRange.value[0]),
        endtime: formatDatetime(dateRange.value[1]),
      }
      queryVisitInfo(param).then((res) => {
        mdtrtInfos.value = res
      })
    }

    const handleClickMdtrtInfo = (row) => {
      mdtrtInfo.value = row
    }

    const getDiseInfo = () => {
      if (nullMdtrtInfo()) {
        return
      }
      const param = {
        psnNo: mdtrtInfo.value.psnNo,
        mdtrtId: mdtrtInfo.value.mdtrtId,
      }
      queryDiagnosisInfo(param).then((res) => {
        diseInfos.value = res
        showDiagnosisInfo.value = true
      })
    }

    const getSetlInfo = () => {
      if (nullMdtrtInfo()) {
        return
      }
      const param = {
        psnNo: mdtrtInfo.value.psnNo,
        mdtrtId: mdtrtInfo.value.mdtrtId,
      }
      querySettlementInfo(param).then((res) => {
        setlInfo.value = res
        showSetlInfo.value = true
      })
    }

    const getFeeDetl = () => {
      if (nullMdtrtInfo()) {
        return
      }
      showFeeDetl.value = true
    }

    const getCumInfo = () => {
      if (nullMdtrtInfo()) {
        return
      }
      showCumInfo.value = true
    }

    onMounted(() => {
      getMedAllTypes().then((res) => {
        medTypes.value = res
      })
    })

    return {
      patNo,
      dateRange,
      maxHalfYear,
      medTypes,
      medType,
      getVisitInfo,
      mdtrtInfos,
      mdtrtInfo,
      handleClickMdtrtInfo,
      getDiseInfo,
      diseInfos,
      feeDetls,
      getSetlInfo,
      getFeeDetl,
      showDiagnosisInfo,
      setlInfo,
      showSetlInfo,
      showFeeDetl,
      getCumInfo,
      showCumInfo,
    }
  },
}
</script>
