<template>
    <div class="tag">
        <div v-for="(item,index) in pathList"
             :style="currentPagePosition(item.path,item?.frequCode)"
             @click="item.func? item.func(index) : handleClick(item.path,item.disable)">
                    <span>
                      {{ item.title }}
                    </span>
        </div>
        <div @click="downloadGuide" style="background-color: seagreen;color: white">
            <span>操作指南 {{ fileName }}</span>
        </div>

        <div @click="closeMedicalAdvice" style="background-color: red;color: white">
            <span v-if="returnToTheConsultation">返回会诊</span>
            <span v-else>退出 </span>
        </div>
    </div>
</template>

<script setup name="YzTag">
import {
    frequCodeEnum,
    queryParam,
    youWuXuanZheHuanZhe
} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";
import {getDrgIntelligentGrouping} from "@/api/zhu-yuan-yi-sheng/emr-patient";
import router from "@/router";
import {isDev} from "@/utils/public";
import {xcMessage} from "@/utils/xiaochan-element-plus";
import {getOperationGuide} from "@/api/public-api";
import {ElMessageBox} from "element-plus";

const props = defineProps({
    patientInfo: Object
})

/**
 * 医嘱筛选
 *
 * @param flag  {@link frequCodeEnum}
 */
const yzSift = (flag) => {
    queryParam.value.frequCode = flag
    router.push('/inpatient/zhuYuanYiSheng/yiZhuLuRu')
}

let pathList = $ref([
    {title: '全部', func: () => yzSift(frequCodeEnum.all), frequCode: frequCodeEnum.all},
    {title: '临时', func: () => yzSift(frequCodeEnum.temporary), frequCode: frequCodeEnum.temporary},
    {title: '长期', func: () => yzSift(frequCodeEnum.longTerm), frequCode: frequCodeEnum.longTerm},
    {title: '出院带药', func: () => yzSift(frequCodeEnum.takeMedicine), frequCode: frequCodeEnum.takeMedicine},
    {path: '/inpatient/zhuYuanYiSheng/huiZhenShenQing', title: '会诊'},
    {path: '/inpatient/zhuYuanYiSheng/jianChaShenQing', title: '检查'},
    {path: '/inpatient/zhuYuanYiSheng/jianYanShenQing', title: '检验'},
    {path: '/inpatient/zhuYuanYiSheng/shouShuShenQing', title: '手术'},
    {path: '/inpatient/zhuYuanYiSheng/caoYaoYiZhu', title: '草药'},
    {
        path: 'emr',
        title: '病历', func: () => {
            if (props.patientInfo.inpatientNo) {
                let query = {
                    patNo: props.patientInfo.inpatientNo,
                    times: props.patientInfo.admissTimes,
                    state: 1
                };
                let te = JSON.stringify(query)
                window.open(window.location.origin + '/myEmrEditor/' + window.btoa(te) + '/refresh', '_blank');
            } else {
                window.open(window.location.origin + '/myEmrEditor/refresh', '_blank');
            }
        }
    },
    {
        title: 'DRG', func: () => {
            if (youWuXuanZheHuanZhe()) return
            getDrgIntelligentGrouping(props.patientInfo.inpatientNo, props.patientInfo.admissTimes).then((res) => {
                window.open(res)
            })
        }
    },
    {
        title: '病历模板', func: () => {
            window.open('http://webhis.thyy.cn:9281/', '_blank')
        },
        disable: true
    }
])
let fileName = $ref('v1.1.0')

const currentPagePosition = (val, frequCode = '') => {
    if (router.currentRoute.value.href === val) {
        return {
            backgroundColor: '#0a84fd',
            color: '#fff'
        }
    }

    if (router.currentRoute.value.href === '/inpatient/zhuYuanYiSheng/yiZhuLuRu' && queryParam.value.frequCode === frequCode) {
        return {
            backgroundColor: '#0a84fd',
            color: '#fff'
        }
    }

    if (val === 'download') {
        return {
            backgroundColor: 'red',
            color: '#fff'
        };
    }

    if (val === 'emr') {
        return {
            backgroundColor: '#E6A23C',
            color: '#fff'
        }
    }
}

const handleClick = (path, disable) => {
    if (disable && !isDev) {
        xcMessage.info('该功能在开发中。。。')
    } else {
        router.push(path);
    }
}

// 点击下载操作指南，先查询一遍最新的。
const downloadGuide = () => {
    getOperationGuide("住院医生教程").then((res) => {
        fileName = res
        ElMessageBox.confirm('请选择线上预览版本还是下载操作指南', '提示', {
            cancelButtonText: '下载',
            confirmButtonText: '线上预览',
            distinguishCancelAndClose: true
        }).then(() => {
            window.open(`http://webhis.thyy.cn:8080/download/操作指南/住院医生教程/住院医生操作指南 ${fileName}.pdf`, '_blank')
        }).catch((e) => {
            if (e === 'cancel') {
                window.open(`http://webhis.thyy.cn:8080/download/操作指南/住院医生教程/住院医生操作指南 ${fileName}.docx`, '_blank')
            }
        })
    })
}

const closeMedicalAdvice = () => {
    if (returnToTheConsultation.value) {
        router.push('/inpatient/casefrntsht/jieShouHuiZhen');
    } else {
        router.push('/');
    }
}

const returnToTheConsultation = ref(false)
onActivated(() => {
    returnToTheConsultation.value = !!router.currentRoute.value.params.consultation;
})

getOperationGuide("住院医生教程").then((res) => {
    fileName = res
})

</script>

<style scoped lang="scss">
.tag {
  display: flex;
  width: 100%;

  div {
    padding: 5px;
    border: 1px solid;
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
