<template>
  <page-layer>
    <template #header>
      <el-select v-model="medType" placeholder="医疗类别" style="width: 145px" clearable>
        <el-option v-for="item in trueMedTypes" :key="item.code" :label="item.name" :value="item.code"></el-option>
      </el-select>
      <el-select v-model="statusFlag" placeholder="审核状态" style="width: 100px" clearable>
        <el-option v-for="item in statusFlags" :key="item.code" :label="item.name" :value="item.code"></el-option>
      </el-select>
      <el-divider direction="vertical"></el-divider>
      <el-button type="success" icon="Document" :disabled="currentApply.medType === '42'" @click="feeDtle">医保费用明细</el-button>
      <el-button type="primary" icon="User" @click="checkIdInfo">身份信息</el-button>
      <el-button type="primary" icon="Tickets" :disabled="injuryMode" @click="beforeGetTreatinfo">待遇检查</el-button>
      <el-button type="success" icon="Check" @click="beforeHandleApply(true)">审核通过</el-button>
      <el-button type="danger" icon="Close" @click="beforeHandleApply(false)">审核不通过</el-button>
    </template>
    <template #aside>
      <el-table :data="cptApplies" stripe highlight-current-row @row-click="handleClickSettleApply">
        <el-table-column label="姓名" width="70">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <img class="sex-icon" :src="scope.row.gender === 1 ? maleIcon : femaleIcon" />
              {{ scope.row.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="住院号" width="65" align="center">
          <template #default="scope">
            <span v-html="getStatusFlag(scope.row.status, scope.row.patNo)"></span>
          </template>
        </el-table-column>
        <el-table-column prop="wardName" label="病房" width="110"></el-table-column>
      </el-table>
    </template>
    <template #main>
      <div>
        <el-tag>患者基本信息</el-tag>
        <span v-show="currentApply.patNo" style="margin-left: 20px">
              <span>明细总费用：</span>
              <el-tag type="info" effect="dark">{{ currentApply.detailTotalCharge }}</el-tag>
              <span style="margin-left: 12px">账页总费用：</span>
              <el-tag type="info" effect="dark">{{ currentApply.ledgerTotalCharge }}</el-tag>
              <span style="margin-left: 12px">医保总费用：</span>
              <el-tag type="info" effect="dark">{{ currentApply.medinsTotalCharge }}</el-tag>
              <span style="margin-left: 12px">医保报销费用：</span>
              <el-tag type="info" effect="dark">{{ currentApply.fundpay }} ， {{ currentApply.fundpayPercentage }}</el-tag>
            </span>
        <el-descriptions :column="4" border>
          <el-descriptions-item>
            <template #label> 住院号 </template>
            {{ patient.inpatientNo }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 住院次数 </template>
            {{ patient.admissTimes }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 身份证号 </template>
            {{ patient.socialNo }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 姓名 </template>
            {{ patient.name }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 性别 </template>
            {{ patient.sex ? (patient.sex === 1 ? '男' : '女') : '' }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 病区 </template>
            {{ patient.admissWardName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 小科室 </template>
            {{ patient.smallDeptName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 入院医生 </template>
            {{ patient.admissPhysicianName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 管床医生 </template>
            {{ patient.referPhysicianName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 床位 </template>
            {{ patient.bedNo }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 入院日期 </template>
            {{ patient.admissDate }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 登记日期 </template>
            {{ patient.ybRegisterDate }}
          </el-descriptions-item>
        </el-descriptions>
        <div style="margin-top: 8px">
          <el-tag>出院结算/中间断账申请信息</el-tag>
          <el-descriptions :column="4" border>
            <el-descriptions-item>
              <template #label> 申请类别 </template>
              {{ currentApply.type === 1 ? '出院结算' : '中间断账' }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> {{ currentApply.type === 1 ? '出院时间' : '断账时间' }} </template>
              {{ currentApply.settleDatetime }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 医疗类别 </template>
              {{ currentApply.medTypeName }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 险种类型 </template>
              {{ currentApply.insutypeName }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 参保地区 </template>
              {{ currentApply.insuplcAdmdvsName }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 申请人 </template>
              {{ currentApply.inputName }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 申请时间 </template>
              {{ currentApply.inputDatetime }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 申请理由 </template>
              {{ currentApply.inputComment }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 使用个账 </template>
              {{ currentApply.acctUsedFlag ? (currentApply.acctUsedFlag === '1' ? '是' : '否') : '' }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 处理人 </template>
              {{ currentApply.handleStaffName }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 处理时间 </template>
              {{ currentApply.handleDatetime }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label> 处理意见 </template>
              {{ currentApply.handleComment }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
        <div style="margin-top: 8px; display: flex">
          <div style="width: 44%">
            <el-tag>医保入院诊断</el-tag>
            <el-table :data="zyInYbDiags" stripe height="160">
              <el-table-column prop="diagNo" label="序号" width="35"></el-table-column>
              <el-table-column prop="icdCode" label="诊断编码" width="80"></el-table-column>
              <el-table-column prop="icdText" label="诊断名称"></el-table-column>
              <el-table-column prop="opDate" label="诊断时间"></el-table-column>
              <el-table-column prop="operName" label="录入人" width="50"></el-table-column>
            </el-table>
          </div>
          <div style="width: 55%; margin-left: 1%">
            <el-tag>医保出院诊断</el-tag>
            <el-table :data="zyDisDiags" stripe height="160">
              <el-table-column prop="disDiagNo" label="序号" width="35"></el-table-column>
              <el-table-column prop="disDiag" label="诊断编码" width="80"></el-table-column>
              <el-table-column prop="disDiagComment" label="诊断名称"></el-table-column>
              <el-table-column prop="opDiagDate" label="诊断时间"></el-table-column>
              <el-table-column prop="opName" label="录入人" width="50"></el-table-column>
              <el-table-column prop="ssfz" label="分值"></el-table-column>
            </el-table>
          </div>
        </div>
        <div style="height: 8px"></div>
        <el-tag>患者手术记录</el-tag>
        <el-table :data="zySurgeries" stripe height="150">
          <el-table-column prop="ssxh" label="手术序号"></el-table-column>
          <el-table-column prop="ssbm" label="手术编码"></el-table-column>
          <el-table-column prop="ssmc" label="手术名称"></el-table-column>
          <el-table-column prop="ssrq" label="手术时间"></el-table-column>
          <el-table-column prop="ssysName" label="手术医师"></el-table-column>
        </el-table>
      </div>
      <Insuinfo v-if="showPsnInsuinfo" :params="checkTreatmentParams" @close="showPsnInsuinfo = false" @row-click="checkTreatmentinfos" />
      <Treatmentinfo v-if="showTreatmentinfos" :params="checkTreatmentParams" @close="showTreatmentinfos = false" />
      <MedfeeAnalyse v-if="showFeeDetl" type="unsettled" :mdtrt-id="currentApply.mdtrtId" @close="showFeeDetl = false" />
      <IdentifyImage v-if="showIdCardImg" :pat-no="currentApply.patNo" :times="currentApply.times" @close="showIdCardImg = false" />
    </template>
  </page-layer>
</template>

<script>
import { computed, onActivated, ref } from 'vue'
import store from '@/store'
import maleIcon from '@/assets/male-icon.png'
import femaleIcon from '@/assets/female-icon.png'
import { ElMessage, ElMessageBox } from 'element-plus'
import { selectUnhandledApplies, selectPatientInfo, handleApply } from '@/api/medical-insurance/si-settle-apply'
import { statusFlags, trueMedTypes } from '../../../data/index'
import Insuinfo from '../../../components/medical-insurance/insuinfo/Index.vue'
import Treatmentinfo from '../../../components/medical-insurance/treatmentinfo/Index.vue'
import MedfeeAnalyse from '../../../components/medical-insurance/medfee-analyse/Index.vue'
import IdentifyImage from '../../../components/inpatient/IdentifyImage.vue'
import PageLayer from "@/layout/PageLayer";
export default {
  components: {PageLayer, Insuinfo, Treatmentinfo, MedfeeAnalyse, IdentifyImage },
  setup() {
    const currentApply = ref({})
    const windowSize = store.state.app.windowSize
    const showFeeDetl = ref(false)
    const medType = ref('')
    const statusFlag = ref('')
    const patient = ref({})
    const zyInYbDiags = ref([])
    const zyDisDiags = ref([])
    const zySurgeries = ref([])
    const allApplies = ref([])
    const cptApplies = computed(() => {
      return allApplies.value.filter((itm) => {
        if ('' !== statusFlag.value) {
          return medType.value ? itm.medType === medType.value && itm.status === statusFlag.value : itm.status === statusFlag.value
        } else {
          return medType.value ? itm.medType === medType.value : true
        }
      })
    })
    const feeDtle = () => {
      if (!currentApply.value.patNo) {
        ElMessage({
          message: '请先选择患者',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
        return
      }
      showFeeDetl.value = true
    }

    const currentRow = ref({})
    const handleClickSettleApply = (row) => {
      store.commit('ptnt/setInjuryMode', row.medType === '42')
      selectPatientInfo(row)
        .then((res) => {
          currentRow.value = row
          patient.value = res.patient
          currentApply.value = res.apply
          zyInYbDiags.value = res.indiags
          zyDisDiags.value = res.disdiags
          zySurgeries.value = res.surgeries
          if (res.warning) {
            ElMessage({
              message: res.warning,
              type: 'warning',
              duration: 6000,
              showClose: true,
            })
          }
        })
        .catch(() => {
          patient.value = {}
          currentApply.value = {}
          zyInYbDiags.value = []
          zyDisDiags.value = []
          zySurgeries.value = []
        })
    }

    const beforeHandleApply = (approve) => {
      if (!currentApply.value.patNo) {
        ElMessage({
          message: '请先选择患者',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
      } else {
        const title = approve ? '审核通过' : '审核不通过'
        let inputVal = approve ? '同意办理。' : ''
        ElMessageBox.prompt('请输入处理意见：', title, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: inputVal,
        })
          .then(({ value }) => {
            currentApply.value.status = approve ? 1 : 2
            currentApply.value.handleComment = value
            handleApply(currentApply.value).then((res) => {
              currentRow.value.status = currentApply.value.status
              ElMessage({
                message: res,
                type: 'success',
                duration: 2500,
                showClose: true,
              })
            })
          })
          .catch(() => {})
      }
    }
    const showIdCardImg = ref(false)
    const checkIdInfo = () => {
      if (!currentApply.value.patNo) {
        ElMessage({
          message: '请先选择患者',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
      } else {
        showIdCardImg.value = true
      }
    }

    const injuryMode = computed(() => {
      return store.state.ptnt.injuryMode
    })

    const showPsnInsuinfo = ref(false)
    const showTreatmentinfos = ref(false)
    const checkTreatmentParams = ref({})
    const beforeGetTreatinfo = () => {
      checkTreatmentParams.value = {
        medType: currentApply.value.medType,
        patNo: currentApply.value.patNo,
        times: currentApply.value.times,
        ledgerSn: currentApply.value.ledgerSn,
        socialNo: patient.value.socialNo,
        name: patient.value.name,
        admdvs: patient.value.admdvs,
      }
      showPsnInsuinfo.value = true
    }
    const checkTreatmentinfos = (row) => {
      checkTreatmentParams.value.psnNo = row.psnNo
      checkTreatmentParams.value.begntime = patient.value.ybRegisterDate
      checkTreatmentParams.value.insutype = row.insutype
      showPsnInsuinfo.value = false
      showTreatmentinfos.value = true
    }

    onActivated(() => {
      selectUnhandledApplies().then((res) => {
        allApplies.value = res
      })
    })

    return {
      showFeeDetl,
      currentApply,
      cptApplies,
      maleIcon,
      femaleIcon,
      trueMedTypes,
      statusFlags,
      medType,
      statusFlag,
      getStatusFlag,
      patient,
      zyInYbDiags,
      zyDisDiags,
      zySurgeries,
      showIdCardImg,
      handleClickSettleApply,
      beforeHandleApply,
      feeDtle,
      checkIdInfo,
      injuryMode,
      showPsnInsuinfo,
      showTreatmentinfos,
      checkTreatmentParams,
      beforeGetTreatinfo,
      checkTreatmentinfos,
    }
  },
}

function getStatusFlag(status, patNo) {
  // 0：撤销-黑色,1：待审核-橙色,2：已审核-绿色,9：未通过-红色,null/-1：未提交-灰色
  switch (status) {
    case 0:
      return `<span style="color: gray">${patNo}</span>`
    case 1:
      return `<span style="color: green">${patNo}</span>`
    case 2:
      return `<span style="color: red">${patNo}</span>`
  }
}
</script>
