<template>
  <page-layer>
    <template #header>
      <el-date-picker v-model="month" :clearable="false" placeholder="出院日期" style="width: 110px" type="month"></el-date-picker>
      <el-divider direction="vertical"></el-divider>
      <el-button type="primary" icon="Search" @click="getAnalyzedSheets">检索</el-button>
      <el-button type="primary" icon="Download" @click="exportExcel">导出Excel</el-button>
    </template>
    <template #main>
      <el-table :height="tableHeight" :data="sheets.slice(pageSize * (currentPage - 1), pageSize * currentPage)" stripe>
        <el-table-column type="expand">
          <template #default="props">
            <div style="display: flex">
              <div style="width: 40%">
                <div style="width: 100%; padding: 4px; background: gray; font-weight: bold; color: white">出院诊断</div>
                <div style="display: flex; font-size: 13px; font-weight: bold; padding: 4px 0">
                  <div style="width: 10%">序号</div>
                  <div style="width: 30%">诊断编码</div>
                  <div>诊断名称</div>
                </div>
                <div v-for="item in props.row.disdiagList">
                  <div style="display: flex; padding: 4px 0; border-bottom: 1px dashed lightskyblue">
                    <div style="width: 10%">{{ item.no }}</div>
                    <div style="width: 30%">{{ item.code }}</div>
                    <div>{{ item.name }}</div>
                  </div>
                </div>
              </div>
              <div v-show="props.row.surgeryList.length > 0" style="width: 55%; margin-left: 30px">
                <div style="width: 100%; padding: 4px; background: gray; font-weight: bold; color: white">手术记录</div>
                <div style="display: flex; font-size: 13px; font-weight: bold; padding: 4px 0">
                  <div style="width: 10%">序号</div>
                  <div style="width: 25%">手术时间</div>
                  <div style="width: 20%">手术编码</div>
                  <div>手术名称</div>
                </div>
                <div v-for="item in props.row.surgeryList">
                  <div style="display: flex; padding: 4px 0; border-bottom: 1px dashed lightskyblue">
                    <div style="width: 10%">{{ item.no }}</div>
                    <div style="width: 25%">{{ item.date }}</div>
                    <div style="width: 20%">{{ item.code }}</div>
                    <div>{{ item.name }}</div>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="bah" label="住院号" width="100"></el-table-column>
        <el-table-column prop="admissTimes" label="次数" width="50"></el-table-column>
        <el-table-column prop="name" label="姓名" width="100"></el-table-column>
        <el-table-column prop="sex" label="性别" width="50"></el-table-column>
        <el-table-column prop="socialNo" label="身份证号"></el-table-column>
        <el-table-column prop="birthDate" label="出生日期" width="100"></el-table-column>
        <el-table-column prop="phone" label="电话" width="100"></el-table-column>
        <el-table-column prop="admissDate" label="入院时间"></el-table-column>
        <el-table-column prop="admissDept" label="入院科室"></el-table-column>
        <el-table-column prop="dismissDate" label="出院时间"></el-table-column>
        <el-table-column prop="dismissDept" label="出院科室"></el-table-column>
        <el-table-column prop="admissDoctorName" label="入院医生" width="100"></el-table-column>
        <el-table-column prop="mainDoctorName" label="主治医生" width="100"></el-table-column>
      </el-table>
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="sheets.length"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 5px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      ></el-pagination>
    </template>
  </page-layer>
</template>

<script setup name="FrontSheetExport">
import store from '@/store'
import { ElMessage } from 'element-plus'
import { formatMonth } from '@/utils/date'
import { fetchSheets } from '@/api/case-front-sheet'
import { Export } from '@/utils/ExportExcel'
import PageLayer from "@/layout/PageLayer";

const windowSize = store.state.app.windowSize
const tableHeight = windowSize.h - 50

let month = $ref(null)
let sheets = $ref([])

let pageSize = $ref(30)
let currentPage = $ref(1)
const handleSizeChange = (val) => {
  pageSize = val
}
const handleCurrentChange = (val) => {
  currentPage = val
}

const getAnalyzedSheets = () => {
  if (!month) {
    ElMessage({
      message: '请先选择出院日期',
      type: 'warning',
      duration: 2500,
      showClose: true,
    })
    return
  }
  month = formatMonth(month)
  fetchSheets(month).then((res) => {
    sheets = res
  })
}

const exportExcel = () => {
  if (sheets.length === 0) {
    ElMessage({
      message: '没有可以导出的数据！',
      type: 'warning',
      duration: 2500,
      showClose: true,
    })
  } else {
    sheets.forEach((itm) => {
      itm.instituionCode = '43010150145'
      itm.instituionName = '长沙泰和医院'
      itm.certtype = itm.country === '中国' ? '1' : '9'
      itm.unitinfo = itm.unitName + '/' + itm.unitPlace
      itm.daytimeSurgery = 0
      itm.blank = '-'

      const diagList = itm.disdiagList
      itm.otherDisdiagSize = diagList.length - 1
      for (let i = 0; i < diagList.length; i++) {
        itm['disdiagCode' + i] = diagList[i].code
        itm['disdiagName' + i] = diagList[i].name
        itm['disdiagAdms' + i] = diagList[0].admissStatus
        itm['disdiagDiss' + i] = diagList[0].dismissStatus
      }

      const surgeryList = itm.surgeryList
      itm.surgerySize = surgeryList.length - 1
      if (surgeryList.length === 0) {
        itm.surgeryCode0 = ''
        itm.surgeryName0 = ''
        itm.surgeryDate0 = ''
        itm.surgeryLevel0 = ''
        itm.surgeryLasttime0 = ''
        itm.surgeryOptor0 = ''
        itm.surgeryAst10 = ''
        itm.surgeryAst20 = ''
        itm.surgeryWjwCh0 = ''
        itm.surgeryAnae0 = ''
        itm.surgeryAnaeLevel0 = ''
        itm.surgeryAnaeName0 = ''
      } else {
        for (let i = 0; i < surgeryList.length; i++) {
          itm['surgeryCode' + i] = surgeryList[i].code
          itm['surgeryName' + i] = surgeryList[i].name
          itm['surgeryDate' + i] = surgeryList[i].date
          itm['surgeryLevel' + i] = surgeryList[i].level
          itm['surgeryLasttime' + i] = ''
          itm['surgeryOptor' + i] = surgeryList[i].operatorName
          itm['surgeryAst1' + i] = surgeryList[i].assistantOneName
          itm['surgeryAst2' + i] = surgeryList[i].assistantTwoName
          itm['surgeryWjwCh' + i] = surgeryList[i].wjwCutHeal
          itm['surgeryAnae' + i] = surgeryList[i].anaesthesia
          itm['surgeryAnaeLevel' + i] = ''
          itm['surgeryAnaeName' + i] = surgeryList[i].anaesthesiaorName
        }
      }
    })

    const title = {
      instituionCode: 'A01',
      instituionName: 'A02',
      bah: 'A48',
      admissTimes: 'A49',
      admissDate: 'B12',
      dismissDate: 'B15',
      healthCardNo: 'A47',
      name: 'A11',
      sex: 'A12C',
      birthDate: 'A13',
      age: 'A14',
      country: 'A15C',
      marriage: 'A21C',
      occupation: 'A38C',
      nation: 'A19C',
      certtype: 'A20N',
      socialNo: 'A20',
      birthPlaceName: 'A22',
      nativePlace: 'A23C',
      hkPlaceName: 'A24',
      hkZipCode: 'A25C',
      livePlace: 'A26',
      phone: 'A27',
      addrZipCode: 'A28C',
      unitinfo: 'A29',
      unitPhone: 'A30',
      unitZipCode: 'A31C',
      contactName: 'A32',
      contactRelation: 'A33C',
      contactAddrName: 'A34',
      contactPhone: 'A35',
      daytimeSurgery: 'B38',
      zyAdmissWay: 'B11C',
      admissDeptCode: 'B13C',
      admissWard: 'B14',
      zkWard: 'B21C',
      dismissDeptCode: 'B16C',
      dismissWard: 'B17',
      admissDays: 'B20',
      clinicDiagCode: 'C01C',
      clinicDiagStr: 'C02N',
      disdiagCode0: 'C03C',
      disdiagName0: 'C04N',
      disdiagAdms0: 'C05C',
      disdiagDiss0: 'F05',
      // 理想情况是在这里做其他诊断的插入
      pathologicDiagCode: 'C09C',
      pathologicDiagStr: 'C10N',
      blh: 'C11',
      hurtReasonCode: 'C12C',
      hurtReasonName: 'C13N',
      allergy: 'C24C',
      allergicMedicine: 'C25',
      hbsAg: 'F10',
      hcvAb: 'F11',
      hivAb: 'F12',
      deptLeader: 'B22C',
      deptLeaderName: 'B22',
      leaderDoctor: 'B23C',
      leaderDoctorName: 'B23',
      mainDoctor: 'B24C',
      mainDoctorName: 'B24',
      admissDoctor: 'B25C',
      admissDoctorName: 'B25',
      dutyNurse: 'B26C',
      dutyNurseName: 'B26',
      studyDoctorName: 'B27',
      internshipDoctorName: 'B28',
      coderName: 'B29',
      qualityControlLevel: 'B30C',
      qualityControlDoctorName: 'B31',
      qualityControlNurseName: 'B32',
      qualityControlDate: 'B33',
      autopsy: 'C34C',
      bloodType: 'C26C',
      rh: 'C27C',
      surgeryCode0: 'C14x01C',
      surgeryName0: 'C15x01N',
      surgeryDate0: 'C16x01',
      surgeryLevel0: 'C17x01',
      surgeryLasttime0: 'F13',
      surgeryOptor0: 'C18x01',
      surgeryAst10: 'C19x01',
      surgeryAst20: 'C20x01',
      surgeryWjwCh0: 'C21x01C',
      surgeryAnae0: 'C22x01C',
      surgeryAnaeLevel0: 'F15',
      surgeryAnaeName0: 'C23x01',
      // 理想情况是在这里做其他手术的插入
      ageDays: 'A16',
      newBornWeight: 'A18x01',
      newBornAdmissWeight: 'A17',
      comaDaysBeforeAdmiss: 'C28',
      comaHoursBeforeAdmiss: 'C29',
      comaMinutesBeforeAdmiss: 'C30',
      comaDaysAfterAdmiss: 'C31',
      comaHoursAfterAdmiss: 'C32',
      comaMinutesAfterAdmiss: 'C33',
      admissAgainInOneMonth: 'B36C',
      admissAgainPurpose: 'B37',
      zyDismissWay: 'B34C',
      dismissDestination: 'B35',
      totalCost: 'D01',
      selfPay: 'D09',
    }

    for (let i = 1; i < 41; i++) {
      let mark = ('0' + i).slice(-2)
      title['disdiagCode' + i] = `C06x${mark}C`
      title['disdiagName' + i] = `C07x${mark}N`
      title['disdiagAdms' + i] = `C08x${mark}C`
      title['disdiagDiss' + i] = `F06x${mark}`
    }

    for (let i = 1; i < 41; i++) {
      let mark = ('0' + i).slice(-2)
      title['surgeryCode' + i] = `C35x${mark}C`
      title['surgeryName' + i] = `C36x${mark}N`
      title['surgeryDate' + i] = `C37x${mark}`
      title['surgeryLevel' + i] = `C38x${mark}`
      title['surgeryLasttime' + i] = `F14x${mark}`
      title['surgeryOptor' + i] = `C39x${mark}`
      title['surgeryAst1' + i] = `C40x${mark}`
      title['surgeryAst2' + i] = `C41x${mark}`
      title['surgeryWjwCh' + i] = `C42x${mark}C`
      title['surgeryAnae' + i] = `C43x${mark}C`
      title['surgeryAnaeLevel' + i] = `F16x${mark}`
      title['surgeryAnaeName' + i] = `C44x${mark}`
    }

    Export(sheets, title, `【${month}】出院数据`)
  }
}
</script>
