<template>
    <el-popover width="220">
        <template #reference>
            <el-button>
                辅助工具
            </el-button>
        </template>
        <template #default>
            <el-button @click="open(0)" type="success">医嘱</el-button>
            <el-button @click="open(1)" type="primary">护理</el-button>
            <el-button @click="open(2)" type="warning">检验</el-button>
            <el-button @click="open(3)" type="info">检查</el-button>
            <hr/>
            <el-button @click="open(4)" text type="success" plain>会诊</el-button>
            <el-button @click="open(5)" text type="primary" plain>手术</el-button>
            <el-button @click="open(6)" text type="warning" plain>检验</el-button>
            <el-button @click="open(7)" text type="info" plain>草药</el-button>
            <hr/>
            <el-button @click="emrChannelClick(emrChannelEnum.firstPageOfMedicalRecord)" type="success" round>首页
            </el-button>
            <el-button @click="emrChannelClick(emrChannelEnum.medicalAdvice)" type="primary" round>医嘱</el-button>
            <el-button @click="open(8)" type="warning" round>心电</el-button>

        </template>
    </el-popover>
    <el-drawer v-model="drawer"
               direction="btt"
               append-to-body
               destroy-on-close
               size="100%"
               :with-header="false">
        <el-button class="close_but"
                   type="danger"
                   icon="Close"
                   @click="drawer =false">
            关闭
        </el-button>
        <emr-order-list v-if="index === 0"
                        :pat-no="patInfo.inpatientNo"
                        :times="patInfo.admissTimes"/>
        <emr-yz-temperature v-if="index === 1"/>
        <inspection-report-index
                style="font-size: 12px;height: 100%"
                v-if="index === 2"
                :pat-no="patInfo.inpatientNo"
                :start="start"
                :end="end"/>
        <emr-inspect v-if="index === 3"
                     :pat-no="patInfo.inpatientNo"
                     :times="patInfo.admissTimes"/>
        <emr-group-consultation v-if="index === 4"/>
        <emr-operation v-if="index === 5"
                       :emr-data="props.emrData"
                       @to-fill-in-data="toFillInData"/>
        <emr-test @close="close" v-if="index === 6"/>
        <emr-herbs v-if="index === 7"
                   @close="close"
                   :pat-no="patInfo.inpatientNo"
                   :times="patInfo.admissTimes"/>
        <emr-electrocardiogram v-if="index === 8"
                               :pat-no="patInfo.inpatientNo"
                               :times="patInfo.admissTimes"/>
    </el-drawer>

</template>

<script setup name='EmrAuxiliaryTools'>
import EmrOrderList from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrOrderList.vue";
import {defineProps, onMounted, ref} from 'vue'
import EmrYzTemperature from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrYzTemperature.vue";
import {patInfo} from './emr-tools-store'
import EmrInspect from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrInspect.vue";
import {getServerDateApi} from "@/api/public-api";
import {formatDate} from "@/utils/date";
import InspectionReportIndex from "@/views/examination/InspectionReportIndex.vue";
import EmrGroupConsultation from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrGroupConsultation.vue";
import EmrOperation from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrOperation.vue";
import EmrTest from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrTest.vue";
import EmrHerbs from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrHerbs.vue";
import {
    emrChannelEnum,
    emrMitt
} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";
import {xcMessage} from "@/utils/xiaochan-element-plus";
import EmrElectrocardiogram from "@/components/zhu-yuan-yi-sheng/emr/auxiliary-tools/EmrElectrocardiogram.vue";

const props = defineProps({
    patInfo: {
        type: Object
    },
    emrData: {
        type: Object
    }
})

const emits = defineEmits(['toFillInData'])

const end = ref('')
const start = ref('')

const drawer = ref(false)

onMounted(async () => {
    patInfo.value = props.patInfo
    end.value = formatDate(await getServerDateApi())
    start.value = formatDate(props.patInfo.admissDate)
})

const toFillInData = (data) => {
    emits('toFillInData', data)
    close()
}

const index = ref(-1)
const open = (val) => {
    localStorage.removeItem('clipBoardData');
    drawer.value = true
    index.value = val
}

const close = () => {
    drawer.value = false
    index.value = -1
}

let emrChannel = new BroadcastChannel('emrChannel')
const emrChannelClick = (name) => {
    let str = {
        name,
        data: props.patInfo
    }
    let patInfoStr = JSON.stringify(str)
    if (emrMitt.emit('患者病区判断')) {
        emrChannel.postMessage(patInfoStr)
    } else {
        xcMessage.error('当前患者不存在您的病区，无法进行该操作。')
    }
}

</script>

<style scoped lang="scss">
.close_but {
  position: absolute;
  top: 10px;
  right: 5px;
}
</style>
