<template>
  <window-size>
    <van-notice-bar left-icon="warning-o" text="仅供查阅参考，最终结果以医院纸质报告为准。" />
    <van-grid :column-num="3">
      <van-grid-item>
        <template #default>
          <div style="font-size: 12px; color: #333333">就诊人</div>
          <div style="margin-top: 5px; font-size: 14px">
            {{ exam.reportHeader.ptnt_NAME }}
          </div>
        </template>
      </van-grid-item>
      <van-grid-item>
        <template #default>
          <div style="font-size: 12px; color: #333333">性别</div>
          <div style="margin-top: 5px; font-size: 14px">
            {{ computedSex }}
          </div>
        </template>
      </van-grid-item>
      <van-grid-item>
        <template #default>
          <div style="font-size: 12px; color: #333333">年龄</div>
          <div style="margin-top: 5px; font-size: 14px">
            {{ exam.reportHeader.ptnt_AGE }}
          </div>
        </template>
      </van-grid-item>
    </van-grid>
    <div style="height: 5px"></div>
    <van-cell title="送检医生" :value="exam.reportHeader.audt_USR_NAME"></van-cell>
    <van-cell title="开方科室" :value="exam.reportHeader.dept_NAME"></van-cell>
    <van-cell title="报告时间" :value="exam.reportHeader.audt_TIME"></van-cell>
    <van-cell title="报告单号" :value="exam.reportHeader.ordr_ID"></van-cell>
    <van-cell v-if="isMicrobacteria" title="细菌名称" :value="exam.bacteriaResult.bac_NAME_CN"></van-cell>
    <div style="height: 5px"></div>
    <van-grid :column-num="4">
      <van-grid-item text="检查项目"></van-grid-item>
      <van-grid-item text="结果"></van-grid-item>
      <van-grid-item text="单位"></van-grid-item>
      <van-grid-item text="参考值"></van-grid-item>
    </van-grid>
    <div v-for="item in exam.reportItems" :key="item.itm_ID">
      <van-grid :column-num="4">
        <van-grid-item>
          <template #text>
            <span class="van-grid-item__text">
              {{ itemName(item) }}
            </span>
          </template>
        </van-grid-item>
        <van-grid-item>
          <template #text>
            <span class="van-grid-item__text">
              {{ itemValue(item) }}
              <van-tag type="danger" v-if="item.itm_ALERT">
                {{ itemAlert(item.itm_ALERT) }}
              </van-tag>
            </span>
          </template>
        </van-grid-item>
        <van-grid-item :text="item.itm_UNIT"></van-grid-item>
        <van-grid-item>
          <template #text>
            <span class="van-grid-item__text">
              {{ itemRange(item) }}
            </span>
          </template>
        </van-grid-item>
      </van-grid>
    </div>
  </window-size>
</template>

<script>
import { useRouter } from 'vue-router'
import { checkExamDetail } from '../../../api/check-exam'
import { computed, onMounted, reactive } from 'vue'
export default {
  name: 'CheckExamDetail',
  setup() {
    const router = useRouter()
    const exam = reactive({
      reportHeader: {},
      reportItems: [],
      bacteriaResult: {},
    })
    const isMicrobacteria = computed(() => {
      // info_TYPE : 报告类别   1|2|99 -- 普通报告|微生物报告|尚未审核的报告
      return exam.reportHeader.info_TYPE === '2'
    })
    const computedSex = computed(() => {
      return exam.reportHeader.ptnt_SEX === '1' ? '男' : '女'
    })
    const itemName = (item) => {
      return isMicrobacteria.value ? item.anti_NAME_CN : item.itm_NAME
    }
    const itemValue = (item) => {
      if (isMicrobacteria.value) {
        return item.anti_VALUE
      }
      if (item.itm_STR_VALUE) {
        return item.itm_STR_VALUE
      }
      return item.itm_VALUE
    }
    const itemAlert = (val) => {
      return val === 'L' ? '↓' : '↑'
    }
    const itemRange = (item) => {
      if (isMicrobacteria.value) {
        return item.anti_MIC
      }
      if (item.itm_STR_VALUE) {
        return item.range_STR
      }
      return item.range
    }
    onMounted(() => {
      const params = router.currentRoute.value.params
      checkExamDetail(params).then((res) => {
        exam.reportHeader = res.reportHeader
        exam.reportItems = res.reportItems
        if (res.reportItems) {
          res.reportItems.forEach((item) => {
            if (item.bacteriaResults.length === 1) {
              exam.bacteriaResult = item.bacteriaResults[0]
              exam.reportItems = exam.bacteriaResult.antibioticResults
            }
          })
        }
      })
    })
    return {
      exam,
      computedSex,
      isMicrobacteria,
      itemName,
      itemValue,
      itemAlert,
      itemRange,
    }
  },
}
</script>
