<template>
  <el-container>
    <el-aside width="260px">
      <Overview ref="overview" :showSelection="isUploadPage" />
    </el-aside>
    <el-main>
      <el-container>
        <el-header height="36px" style="margin-top: 6px">
          <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">
            <el-input v-model="search.zyh" style="width: 100px" clearable @keyup.enter="searchPatient" placeholder="住院号"></el-input>
            <el-button type="primary" icon="el-icon-search" @click="searchPatient">检索</el-button>
          </span>
          &nbsp;&nbsp;
          <el-button type="primary" icon="el-icon-postcard" @click="checkIdCard">身份信息</el-button>
          <el-button type="primary" icon="el-icon-download" @click="downloadReadCard" style="margin-left: 10px"> 下载读卡程序 </el-button>
          <el-button style="margin-left: 10px" type="success" icon="el-icon-document" @click="getRegInfo">登记信息 </el-button>
          <el-button style="margin-left: 10px" type="danger" icon="el-icon-magic-stick" @click="toEmpiView">360视图 </el-button>
        </el-header>
        <el-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>
        </el-main>
      </el-container>
    </el-main>
  </el-container>
</template>

<script>
import { computed, reactive, ref } from 'vue'
import store from '@/store'
import dismissIcon from '@/assets/dismiss-icon.png'
import { getPatientInfo, getEmpiViewUrl } from '@/api/yibao/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'
export default {
  components: {
    Overview,
    IdentifyImage,
    Registinfo,
  },
  setup() {
    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'
    })

    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
    }

    return {
      patient,
      permission,
      search,
      showRegisterInfo,
      isUploadPage,
      dismissIcon,
      showIdCardImg,
      overview,
      checkIdCard,
      getRegInfo,
      toEmpiView,
      searchPatient,
      downloadReadCard,
      handleSelectSearchMehtod,
    }
  },
}

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

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,
  }
}

function downloadReadCard() {
  window.open('http://172.16.32.160:8888/readcard/ReadCard（解压到D盘根目录）.zip', '_blank')
}
</script>
