<template>
  <el-container>
    <el-header>
      <span>统计时间：</span>
      <el-select v-model="dateRangeType" style="width: 80px">
        <el-option value="monthrange" label="按月份"></el-option>
        <el-option value="year" label="按年度"></el-option>
      </el-select>
      <el-date-picker
        v-if="dateRangeType === 'monthrange'"
        v-model="monthrange"
        type="monthrange"
        style="width: 170px"
        range-separator="至"
        start-placeholder="开始月份"
        end-placeholder="结束月份"
      >
      </el-date-picker>
      <el-date-picker v-else v-model="year" type="year" style="width: 100px"> </el-date-picker>
      <el-divider direction="vertical"></el-divider>
      <span>统计方式：</span>
      <el-select v-model="statisticsType" style="width: 100px">
        <el-option :value="1" label="按入院科室"></el-option>
        <el-option :value="2" label="按出院科室"></el-option>
      </el-select>
      <el-divider direction="vertical"></el-divider>
      <el-button type="primary" icon="Search" @click="excuteQuery">查询</el-button>
      <el-button type="primary" icon="Download" @click="exportExcel">导出Excel</el-button>
    </el-header>
    <el-main>
      <el-table :data="statistics" stripe height="360px">
        <el-table-column prop="departmentName" label="科室"></el-table-column>
        <el-table-column prop="diagConformInAdmAndDischarge" label="入院诊断与出院诊断符合率"></el-table-column>
        <el-table-column prop="diagConformInPreAndAfterOperation" label="术前与术后诊断符合率"></el-table-column>
        <el-table-column prop="diagConformInMainDiagAndPathology" label=" 临床主要诊断与病理符合率"></el-table-column>
        <el-table-column prop="rescueSuccessRate" label="抢救成功率"></el-table-column>
        <el-table-column prop="surgicalIncisionBestHealingRate" label="手术切口甲级愈合率"></el-table-column>
        <el-table-column prop="medicalRecordRateAboveGradeFour" label="三级及以上病历率"></el-table-column>
        <el-table-column prop="levelFiveMedicalRecordRate" label="五级病案率"></el-table-column>
        <el-table-column prop="averageLengthOfStay" label="平均住院日"></el-table-column>
        <el-table-column prop="bedTurnoverTimes" label="病床周转次数"></el-table-column>
      </el-table>
    </el-main>
  </el-container>
</template>

<script>
import { ref, onMounted } from 'vue'
import { getRawMonthOffset, formatMonth, formatYear } from '@/utils/date'
import { analyzeQualityControl } from '@/api/case-front-sheet/index'
import { Export } from '@/utils/ExportExcel'
import { ElMessage } from 'element-plus'
export default {
  setup() {
    const dateRangeType = ref('monthrange')
    const year = ref(null)
    const monthrange = ref([])
    const statisticsType = ref(1)
    const statistics = ref([])

    const excuteQuery = () => {
      const params = {
        type: statisticsType.value,
      }
      if (dateRangeType.value === 'monthrange') {
        params.beginMonth = formatMonth(monthrange.value[0])
        params.endMonth = formatMonth(monthrange.value[1])
      } else {
        params.year = formatYear(year.value)
      }
      analyzeQualityControl(params).then((res) => {
        statistics.value = res
      })
    }

    const exportExcel = () => {
      if (statistics.value.length === 0) {
        ElMessage({
          message: '没有可以导出的数据。',
          type: 'warning',
          duration: 2500,
          showClose: true,
        })
        return
      }

      const title = {
        departmentCode: '科室编码',
        departmentName: '科室名称',
        diagConformInAdmAndDischarge: '入院诊断与出院诊断符合率',
        diagConformInPreAndAfterOperation: '术前与术后诊断符合率',
        diagConformInMainDiagAndPathology: '临床主要诊断与病理符合率',
        rescueSuccessRate: '病房危重病人抢救成功率',
        surgicalIncisionBestHealingRate: '手术切口甲级愈合率',
        medicalRecordRateAboveGradeFour: '三级及以上病历率',
        levelFiveMedicalRecordRate: '五级病案率',
        averageLengthOfStay: '平均住院日',
        bedTurnoverTimes: '病床周转次数',
      }

      const preffix = dateRangeType.value === 'monthrange' ? formatMonth(monthrange.value[0]) + '至' + formatMonth(monthrange.value[1]) : formatYear(year.value)
      Export(statistics.value, title, `【${preffix}】各科室质控数据`)
    }

    onMounted(() => {
      const offset = getRawMonthOffset()
      monthrange.value[0] = offset.start
      monthrange.value[1] = offset.end
      year.value = new Date()
    })

    return {
      dateRangeType,
      year,
      monthrange,
      statisticsType,
      statistics,
      excuteQuery,
      exportExcel,
    }
  },
}

// 入院诊断与出院诊断符合率≥95 %;
// 术前与术后诊断符合率≥90 %;
// 临床主要诊断与病理符合率≥50 % (以上三项每低于1个百分点扣1分)﹔
// 病房危重病人抢救成功率≥84 %;
// 无菌手术切口甲级愈合率≥97 %;
// 三级及以上病历率≥90 %，
// 无五级病案（低于1个百分点扣1分，出现1份v级病历扣3分);
// 平均住院日s12天;
// 病床周转次数≥18次 / 年;(床位周转次数=出院人数/平均开放床位数)

// 771张床
</script>
