<template>
  <el-container>
    <el-header style="height: 35px">
      <el-select size="small" v-model="patient.medType" placeholder="请选择医疗类别" style="width: 145px">
        <el-option v-for="item in medTypes" :key="item.code" :label="item.name" :value="item.code"></el-option>
      </el-select>
      <el-select size="small" v-model="bizAppyType" placeholder="业务申请类型" style="width: 145px">
        <el-option v-for="item in bizAppyTypes" :key="item.code" :label="item.name" :value="item.code"></el-option>
      </el-select>
      <el-button style="margin-left: 10px" type="primary" icon="el-icon-location" @click="getPsnInsuinfo(0)">定点信息查询</el-button>
      <el-button style="margin-left: 10px" type="primary" icon="el-icon-tickets" @click="getPsnInsuinfo(1)">待遇检查</el-button>
      <el-button type="success" icon="el-icon-check" @click="getPsnInsuinfo(2)">审核通过</el-button>
      <el-button type="danger" icon="el-icon-close" @click="reject">审核不通过</el-button>
      <el-button type="primary" icon="el-icon-refresh-left" @click="showDrawer">取消医保登记审核</el-button>
    </el-header>
    <el-main>
      <div class="his-info">
        <el-tag>患者基本信息</el-tag>
        <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.birthDate }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 姓名 </template>
            {{ patient.name }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 性别 </template>
            {{ patient.sex }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 参保区划 </template>
            {{ patient.admdvs }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label> 联系电话 </template>
            {{ patient.homeTel }}
          </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>
      <div style="height: 8px"></div>
      <el-tag>医保入院诊断</el-tag>
      <el-table :data="zyInYbDiags" stripe>
        <el-table-column prop="diagNo" label="诊断序号"></el-table-column>
        <el-table-column prop="property" label="诊断属性"></el-table-column>
        <el-table-column prop="icdCode" label="诊断编码"></el-table-column>
        <el-table-column prop="icdText" label="诊断名称"></el-table-column>
        <el-table-column label="诊断类别">
          <template #default="scope">
            {{ getDiagTypeName(scope.row.diagType) }}
          </template>
        </el-table-column>
        <el-table-column prop="opDate" label="诊断时间"></el-table-column>
      </el-table>

      <div class="his-info mt10">
        <el-tag>科室备注信息</el-tag>
        <div class="remark">
          <el-input type="textarea" v-model="patient.remark" disabled></el-input>
          <el-row style="height: 28px; line-height: 28px">
            <el-col :span="2">录入人：</el-col>
            <el-col :span="4">{{ patient.inputName }}</el-col>
            <el-col :span="2">录入时间：</el-col>
            <el-col :span="4">{{ patient.inputDate }}</el-col>
          </el-row>
        </div>
      </div>
      <div class="his-info mt10">
        <el-tag>医保审核信息</el-tag>
        <div class="remark">
          <el-input type="textarea" v-model="patient.ybRemark" clearable></el-input>
        </div>
      </div>
      <el-drawer title="取消医保登记审核" :with-header="false" v-model="drawerVisible" direction="ltr" size="60%">
        <div style="height: 5px"></div>
        <el-button style="margin-left: 15px" type="primary" icon="el-icon-check" @click="beforeHandleRequest(2)">审核通过</el-button>
        <el-button style="margin-left: 15px" type="danger" icon="el-icon-close" @click="beforeHandleRequest(3)">审核不通过</el-button>
        <div style="margin: 0 20px; height: 550px">
          <el-table :data="cancelData" :height="tableHeight" stripe highlight-current-row @row-click="handleClickRow">
            <el-table-column prop="inpatientNo" label="住院号" width="80"></el-table-column>
            <el-table-column prop="admissTimes" label="次数" width="50"></el-table-column>
            <el-table-column label="姓名" width="85">
              <template #default="scope">
                <img style="width: 15px; height: 15px" :src="scope.row.sex === 1 ? maleIcon : femaleIcon" />
                {{ scope.row.name }}
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="科室备注"></el-table-column>
            <el-table-column prop="inputName" label="备注人" width="80"></el-table-column>
            <el-table-column prop="inputDate" label="提交时间"></el-table-column>
          </el-table>
        </div>
      </el-drawer>
      <el-dialog title="人员信息" v-model="showPsnInsuinfo" width="65%">
        <el-tag>{{ injuryMode ? '工伤信息' : '参保信息' }}</el-tag>
        <div v-if="injuryMode">
          <el-table :data="injuryinfo" @row-click="handleClickInjuryinfo" stripe height="180">
            <el-table-column label="电脑号" prop="indiId"></el-table-column>
            <el-table-column label="工伤个人业务序号" prop="serialPers"></el-table-column>
            <el-table-column label="工伤认定号" prop="identifyCode"></el-table-column>
            <el-table-column label="认定申请时间" prop="identyDate"></el-table-column>
            <el-table-column label="受伤部位" prop="injuryPart"></el-table-column>
            <el-table-column label="报告时间" prop="reportDate"></el-table-column>
            <el-table-column label="事故发生时间" prop="accidentDate"></el-table-column>
            <el-table-column label="事故详细情况" prop="accidentDetail"></el-table-column>
          </el-table>
        </div>
        <div v-else>
          <el-table :data="psnBaseinfo.insuinfo" @row-click="handleClickInsuinfo" stripe height="180">
            <el-table-column label="余额" prop="balc"></el-table-column>
            <el-table-column label="险种类型" prop="insutypeName"></el-table-column>
            <el-table-column label="人员类别" prop="psnTypeName"></el-table-column>
            <el-table-column label="参保状态" prop="psnInsuStasName"></el-table-column>
            <el-table-column label="个人参保日期" prop="psnInsuDate"></el-table-column>
            <el-table-column label="暂停参保日期" prop="pausInsuDate"></el-table-column>
            <el-table-column label="公务员标志" prop="cvlservFlagName"></el-table-column>
            <el-table-column label="参保地医保区划" prop="insuplcAdmdvs"></el-table-column>
            <el-table-column label="参保地名称" prop="insuplcAdmdvsName"></el-table-column>
            <el-table-column label="单位名称" prop="empName"></el-table-column>
          </el-table>
          <div style="height: 16px"></div>
          <el-tag>身份信息</el-tag>
          <el-table :data="psnBaseinfo.idetinfo" stripe height="180">
            <el-table-column label="人员身份类别" prop="psnIdetTypeName"></el-table-column>
            <el-table-column label="人员类别等级" prop="psnTypeLv"></el-table-column>
            <el-table-column label="备注" prop="memo"></el-table-column>
            <el-table-column label="开始时间" prop="begntime"></el-table-column>
            <el-table-column label="结束时间" prop="endtime"></el-table-column>
          </el-table>
        </div>
      </el-dialog>
      <el-dialog title="人员待遇信息" v-model="showTrtInfos" width="65%">
        <el-table :data="trtInfos" stripe>
          <el-table-column label="人员编号" prop="psnNo"></el-table-column>
          <el-table-column label="待遇检查类型" prop="trtChkTypeName"></el-table-column>
          <el-table-column label="基金支付类型" prop="fundPayTypeName"></el-table-column>
          <el-table-column label="基金款项待遇享受标志" prop="trtEnjymntFlagName"></el-table-column>
          <el-table-column label="开始日期" prop="begndate"></el-table-column>
          <el-table-column label="结束日期" prop="enddate"></el-table-column>
          <el-table-column label="待遇检查结果" prop="trtChkRslt"></el-table-column>
        </el-table>
      </el-dialog>
      <el-dialog title="人员定点信息" v-model="showFixMedins" width="65%">
        <el-table :data="fixMedins" stripe>
          <el-table-column prop="psnNo" label="人员编号"></el-table-column>
          <el-table-column prop="insutypeName" label="险种类型"></el-table-column>
          <el-table-column prop="fixSrtNo" label="定点排序号"></el-table-column>
          <el-table-column prop="fixmedinsCode" label="定点医药机构编号"></el-table-column>
          <el-table-column prop="fixmedinsName" label="定点医药机构名称"></el-table-column>
          <el-table-column prop="begndate" label="开始日期"></el-table-column>
          <el-table-column prop="enddate" label="结束日期"></el-table-column>
          <el-table-column prop="memo" label="备注"></el-table-column>
        </el-table>
      </el-dialog>
    </el-main>
  </el-container>
</template>

<script>
import { computed, onActivated, onDeactivated, onMounted, ref, watchEffect } from 'vue'
import store from '@/store'
import { cptSex } from '@/utils/computed'
import maleIcon from '@/assets/male-icon.png'
import femaleIcon from '@/assets/female-icon.png'
import { approveYbsf, rejectYbsf, getUnhandledRequests, handleRequest } from '@/api/yibao/yb-verify'
import { ElMessage, ElMessageBox } from 'element-plus'
import { nullPatient } from '@/utils/validate'
import { getBizAppyTypes, getMedTypesByFlag, getDiagTypes } from '@/api/medical-insurance/si-dict'
import { checkPersonTreatment, obtainBasicPersonInfo, queryPersonnelAssignmentInfo } from '@/api/medical-insurance/si-query'
import { getPersonBaseinfo, admissRegistration } from '@/api/medical-insurance/si-injury'
import { admissRegister } from '@/api/medical-insurance/si-inpatient'
import { getZyInYbDiags } from '@/api/yibao/patient'
import { baseinfo } from '@/data/inpatient'
export default {
  setup() {
    const patient = computed(() => {
      return baseinfo()
    })
    const drawerVisible = ref(false)
    const cancelData = ref([])
    const windowSize = store.state.app.windowSize
    const tableHeight = windowSize.h - 50
    const bizAppyType = ref(null)
    const bizAppyTypes = ref([])
    const psnBaseinfo = ref({})
    const fixMedins = ref([])
    const showFixMedins = ref(false)
    const trtInfos = ref([])
    const showTrtInfos = ref(false)
    const showPsnInsuinfo = ref(false)
    const insuRowClickFlag = ref(null)
    const zyInYbDiags = ref([])
    const medTypes = ref([])
    const injuryMode = computed(() => {
      return store.state.ptnt.injuryMode
    })

    const actived = ref(false)
    onActivated(() => {
      actived.value = true
      store.commit('app/setCurrentPageName', 'medInsVerification')
    })

    onDeactivated(() => {
      actived.value = false
      store.commit('app/setCurrentPageName', '')
    })

    watchEffect(() => {
      if (actived.value) {
        if (patient.value.inpatientNo) {
          getZyInYbDiags(patient.value.inpatientNo, patient.value.admissTimes).then((res) => {
            zyInYbDiags.value = res
          })
        }
      }
    })

    const injuryinfo = ref([])

    const getPsnInsuinfo = (flag) => {
      if (nullPatient()) {
        return
      }
      if (!patient.value.medType) {
        ElMessage({
          message: '请选择患者的医疗类别！',
          type: 'warning',
          showClose: true,
        })
        return
      }
      if (flag === 0) {
        if (!bizAppyType.value) {
          ElMessage({
            message: '请选择业务申请类型！',
            type: 'warning',
            showClose: true,
          })
          return
        }
      }

      if (injuryMode.value) {
        getPersonBaseinfo(patient.value).then((res) => {
          injuryinfo.value = res
          showPsnInsuinfo.value = true
        })
      } else {
        insuRowClickFlag.value = flag
        const param = {
          medType: patient.value.medType,
          patNo: patient.value.inpatientNo,
          times: patient.value.admissTimes,
          ledgerSn: patient.value.ledgerSn,
          socialNo: patient.value.socialNo,
          name: patient.value.name,
          admdvs: patient.value.admdvs,
          needSaving: 1,
        }
        obtainBasicPersonInfo(param).then((res) => {
          if (flag === 0) {
            const param = {
              patNo: patient.value.inpatientNo,
              times: patient.value.admissTimes,
              psnNo: res.psnNo,
              bizAppyType: bizAppyType.value,
            }
            queryPersonnelAssignmentInfo(param).then((res) => {
              fixMedins.value = res
              showFixMedins.value = true
            })
          } else {
            patient.value.psnNo = res.psnNo
            psnBaseinfo.value = res
            showPsnInsuinfo.value = true
          }
        })
      }
    }

    const handleClickInjuryinfo = (row) => {
      ElMessageBox.confirm('审核通过将直接进行医保入院登记，确定审核通过？', '提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          const param = {
            baseinfo: patient.value,
            injuryinfo: row,
          }
          admissRegistration(param).then((res) => {
            approveYbsf(patient.value).then(() => {
              ElMessage({
                message: '审核已通过，医保入院登记成功。',
                type: 'success',
                duration: 2500,
                showClose: true,
              })
              showPsnInsuinfo.value = false
            })
          })
        })
        .catch(() => {})
    }

    const handleClickInsuinfo = (row) => {
      if (insuRowClickFlag.value === 1) {
        chkTrtmt(row)
      } else if (insuRowClickFlag.value === 2) {
        admRgstn(row)
      }
    }

    const chkTrtmt = (row) => {
      const param = {
        patNo: patient.value.inpatientNo,
        times: patient.value.admissTimes,
        psnNo: patient.value.psnNo,
        insutype: row.insutype,
        medType: patient.value.medType,
        begntime: patient.value.admissDate,
      }
      checkPersonTreatment(param).then((res) => {
        trtInfos.value = res
        showPsnInsuinfo.value = false
        showTrtInfos.value = true
      })
    }

    const admRgstn = (row) => {
      ElMessageBox.confirm('审核通过将直接进行医保入院登记，确定审核通过？', '提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          patient.value.insutype = row.insutype
          patient.value.psnType = row.psnType
          patient.value.insuplcAdmdvs = row.insuplcAdmdvs
          patient.value.empName = row.empName
          admissRegister(patient.value).then(() => {
            approveYbsf(patient.value).then(() => {
              ElMessage({
                message: '审核已通过，医保入院登记成功。',
                type: 'success',
                duration: 2500,
                showClose: true,
              })
              showPsnInsuinfo.value = false
            })
          })
        })
        .catch(() => {})
    }

    const reject = () => {
      if (nullPatient()) {
        return
      }
      ElMessageBox.confirm('确定审核不通过？', '提示', {
        type: 'warning',
      })
        .then(() => {
          rejectYbsf(patient.value).then(() => {
            ElMessage({
              message: '操作成功。',
              type: 'success',
              duration: 2500,
              showClose: true,
            })
          })
        })
        .catch(() => {})
    }
    const showDrawer = () => {
      getUnhandledRequests(1).then((res) => {
        cancelData.value = res
        drawerVisible.value = true
      })
    }

    const selectedRow = ref({})
    const handleClickRow = (row) => {
      selectedRow.value = row
    }

    const beforeHandleRequest = (flag) => {
      if (!selectedRow.value.inpatientNo) {
        ElMessage({
          message: '请先选择患者',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
      } else {
        const title = flag === 2 ? '审核通过' : '审核不通过'
        let inputVal = flag === 2 ? '同意办理。' : ''
        ElMessageBox.prompt('请输入备注内容：', title, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: inputVal,
        })
          .then(({ value }) => {
            selectedRow.value.statusFlag = flag
            selectedRow.value.ybRemark = value
            handleRequest(selectedRow.value).then((res) => {
              ElMessage({
                message: '操作成功。',
                type: 'success',
                duration: 2500,
                showClose: true,
              })
            })
          })
          .catch(() => {})
      }
    }

    const diagTypes = ref([])
    const getDiagTypeName = (val) => {
      for (let i = 0; i < diagTypes.value.length; i++) {
        if (diagTypes.value[i].code === val) {
          return diagTypes.value[i].name
        }
      }
    }

    onMounted(() => {
      getMedTypesByFlag('hospitalization').then((res) => {
        medTypes.value = res
      })
      getBizAppyTypes().then((res) => {
        bizAppyTypes.value = res
      })
      getDiagTypes().then((res) => {
        diagTypes.value = res
      })
    })

    return {
      patient,
      zyInYbDiags,
      tableHeight,
      drawerVisible,
      injuryMode,
      injuryinfo,
      cancelData,
      maleIcon,
      femaleIcon,
      psnBaseinfo,
      showPsnInsuinfo,
      selectedRow,
      medTypes,
      bizAppyType,
      bizAppyTypes,
      fixMedins,
      showFixMedins,
      trtInfos,
      showTrtInfos,
      cptSex,
      getDiagTypeName,
      filterYesOrNo,
      getPsnInsuinfo,
      handleClickInsuinfo,
      handleClickInjuryinfo,
      reject,
      showDrawer,
      handleClickRow,
      beforeHandleRequest,
    }
  },
}

function filterYesOrNo(val) {
  if (val === '0') {
    return '否'
  } else if (val === '1') {
    return '是'
  }
  return ''
}
</script>

<style scoped>
.remark {
  padding: 5px 10px;
}

.mt10 {
  margin-top: 10px;
}

.yb-tag {
  padding: 5px 0 0 20px;
}
</style>
