<template>
  <page-layer>
    <template #header>
      <el-select v-model="search.current" style="width: 70px" @change="handleSelectSearchMehtod">
        <el-option v-for="item in search.methods" :key="item.code" :value="item.code" :label="item.name"></el-option>
      </el-select>
      <span v-if="permission < 10" style="margin-left: 8px">
            <el-input v-model="search.zyh" style="width: 100px" clearable @keyup.enter="searchPatient"
                      placeholder="住院号"></el-input>
            <el-button type="primary" icon="Search" @click="searchPatient">检索</el-button>
          </span>
      <el-button type="primary" icon="Postcard" @click="checkIdCard" style="margin-left: 8px">身份信息</el-button>
      <el-button style="margin-left: 10px" type="success" icon="Document" @click="getRegInfo">登记信息</el-button>
      <el-button style="margin-left: 10px" type="danger" icon="MagicStick" @click="toEmpiView">360视图</el-button>
    </template>
    <template #aside>
      <Overview ref="overview" :showSelection="isUploadPage"/>
    </template>
    <template #main>
      <router-view v-slot="{ Component }">
        <transition name="fade-transform" mode="out-in">
          <keep-alive>
            <component :is="Component"/>
          </keep-alive>
        </transition>
      </router-view>
      <Registinfo v-if="showRegisterInfo" :params="patient" @close="showRegisterInfo = false"></Registinfo>
      <IdentifyImage v-if="showIdCardImg" :pat-no="patient.inpatientNo" :times="patient.admissTimes"
                     @close="showIdCardImg = false"></IdentifyImage>
    </template>
  </page-layer>
</template>

<script setup>
import {computed, reactive, ref} from 'vue'
import store from '@/store'
import {getEmpiViewUrl, getPatientInfo} from '@/api/inpatient/patient'
import {nullPatient} from '@/utils/validate'
import {getGreatestRole} from '@/utils/permission'
import {baseinfo, setBaseinfo} from '@/data/inpatient'
import Overview from '../../../components/medical-insurance/patient-overview/Index.vue'
import IdentifyImage from '../../../components/inpatient/IdentifyImage.vue'
import Registinfo from '../../../components/medical-insurance/registinfo/Index.vue'
import PageLayer from "@/layout/PageLayer";


const search = initSearchParam()
const permission = getGreatestRole()
const handleSelectSearchMehtod = (val) => {
  store.commit('ptnt/setSearchMethod', val)
}
const patient = computed(() => {
  return baseinfo()
})
const isUploadPage = computed(() => {
  return store.state.app.currentPageName === 'inHospFeeUpload' && !store.state.ptnt.injuryMode
})

const overview = ref(null)

const searchPatient = () => {
  if (!search.zyh) {
    overview.value.fetchOverviews()
    setBaseinfo({})
  } else {
    if (permission < 10) {
      overview.value.overviews = []
      store.commit('ptnt/setBaseinfo', {totalCharge: '0.00', chargeYb: '0.00'})
      getPatientInfo(search.zyh).then((res) => {
        overview.value.currentWard = res.admissWard
        store.commit('user/wardChange', res.admissWard)
        store.commit('ptnt/setCurrentMedType', res.medType)
        setBaseinfo(res)
        overview.value.overviews.push(makeOverview(res))
      })
    }
  }
}

const showRegisterInfo = ref(false)
const getRegInfo = () => {
  if (nullPatient()) return
  showRegisterInfo.value = true
}

const toEmpiView = () => {
  if (nullPatient()) return
  getEmpiViewUrl(patient.value.inpatientNo).then((res) => {
    window.open(res, '_blank')
  })
}

const showIdCardImg = ref(false)
const checkIdCard = () => {
  if (nullPatient()) return
  showIdCardImg.value = true
}

function initSearchParam() {
  return reactive({
    current: 'alpha',
    methods: [
      {code: 'alpha', name: '拼音'},
      {code: 'code', name: '编码'},
      {code: 'name', name: '名称'},
    ],
    zyh: '',
  })
}

function makeOverview(val) {
  return {
    bedNo: val.bedNo,
    inpatientNo: val.inpatientNo,
    admissTimes: val.admissTimes,
    name: val.name,
    sex: val.sex,
    medType: val.medType,
    dismissOrder: val.dismissOrder,
    mdtrtId: val.mdtrtId,
    injurySerialNo: val.injurySerialNo,
    status: val.mdtrtId || val.injurySerialNo ? 1 : 0,
  }
}
</script>
