<template>
    <div style="display: flex">
        <button @click="queryYz" title="重新查询患者的医嘱">重置</button>
        <label>
            显示范围：
            <select v-model="queryParam.displayRange" title="显示范围">
                <option :value="0" title="全部的医嘱">全部</option>
                <option :value="1" title="已经停止的医嘱">停止</option>
                <option :value="2" title="正在执行的长期医嘱">当前</option>
                <option :value="3" title="今天开的医嘱">今天</option>
            </select>
        </label>

        <label>
            状态：
            <select v-model.number="queryParam.zhuangTai" title="医嘱的状态">
                <option :value="0">全部</option>
                <option :value="1">录入</option>
                <option :value="2">确认</option>
                <option :value="3">执行</option>
                <option :value="5">停止</option>
            </select>
        </label>
        <label>
            药房：
            <select v-model="queryParam.groupNo" title="在新增医嘱的时候，去指定的药房查询药品。">
                <option value="73">住院</option>
                <option value="71">门诊</option>
            </select>
        </label>

        <button style="margin-left: 5px" @click="props.addYiZhuClick" title="ALT+A">添加</button>
        <button style="margin-left: 5px" @click="deleteAnOrderByOrderNo"
                title="在表格中选中了一条医嘱后这个方法才能有效果。（这个医嘱需要在工作台显示）">
            删除
        </button>
        <button style="margin-left: 5px" @click="batchDeleteOrdersClick"
                title="在表格中，点击前面的选择框，选中医嘱（多条或单挑），才能点击此按钮，限制：只能删除自己开的，只能删除录入状态的医嘱。">
            批量删除
        </button>
        <button style="margin-left: 5px" @click="currentConfirmOrdersClick" v-if="!isCydy()"
                title="把录入状态的医嘱，变为确认状态。">确认
        </button>
        <button style="margin-left: 5px" @click="confirmTheDoctorSOrderWithMedicineClick"
                v-else
                title="把录入状态的医嘱，变为确认状态。">确认带药
        </button>
        <button style="margin-left: 5px" @click="props.clickOnTheOrderTemplate" title="获取医嘱的模板">模板</button>

        <div style="display: flex;">
            <status-color color="#05ff00" label="R:录入"/>
            <status-color color="#0000fb" label="Q:确认"/>
            <status-color color="#ff07f3" label="Z:执行"/>
            <status-color color="red" label="T:停止"/>
            <status-color color="#00ffe0" label="D:作废"/>
        </div>
    </div>

</template>

<script setup name='YzQueryCondition'>
import {
    confirmTheDoctorSOrderWithMedicine,
    huoQuYiZhuShuJu
} from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
import StatusColor from "@/components/zhu-yuan-yi-sheng/yi-zhu-lu-ru/yz-header/StatusColor";
import {
    errorMsg,
    isCydy,
    queryParam, tempYzData, yiZhuData, yiZhuDataInit,
    yzData, yzMitt
} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";
import {BizException, ExceptionEnum} from "@/utils/BizException";

const props = defineProps({
    patientInfo: {
        type: Object
    },
    returnData: {
        type: Function
    },
    addYiZhuClick: Function,
    frequCodeChange: Function,
    confirmOrdersClick: Function,
    clickOnTheOrderTemplate: Function
})

const emits = defineEmits(['batchDeleteOrdersClick'])

const queryYz = async (isScroll = true) => {
    if (!props.patientInfo.inpatientNo) {
        BizException(ExceptionEnum.MESSAGE_ERROR, '请先选择患者')
    }

    try {
        yiZhuDataInit()
        yzMitt.emit('clearSelected')
        if (isScroll) {
            yzMitt.emit('tableScroll', 0);
        }
        yzData.value = await huoQuYiZhuShuJu({
            patNo: props.patientInfo.inpatientNo,
            times: props.patientInfo.admissTimes
        })
    } catch (e) {
        yzData.value = []
    }

}

const currentConfirmOrdersClick = () => {
    if (!props.patientInfo.inpatientNo) {
        BizException(ExceptionEnum.MESSAGE_ERROR, '请先选择患者')
    }
    props.confirmOrdersClick()
}

const confirmTheDoctorSOrderWithMedicineClick = () => {
    if (!props.patientInfo.inpatientNo) {
        BizException(ExceptionEnum.MESSAGE_ERROR, '请先选择患者')
    }
    confirmTheDoctorSOrderWithMedicine(props.patientInfo.inpatientNo, props.patientInfo.admissTimes).then((res) => {
        if (res?.error) {
            errorMsg.value.type = 3
            errorMsg.value.dialog = true
            errorMsg.value.data = res.data
        } else {
            queryYz()
        }
    })
}

const deleteAnOrderByOrderNo = () => {
    yzMitt.emit('deleteAnOrderByOrderNo', yiZhuData.value)
}

/**
 * 点击批量删除数据
 */
const batchDeleteOrdersClick = () => {
    emits('batchDeleteOrdersClick')
}

watch(() => props.patientInfo, () => {
    queryYz()
})

onMounted(() => {
    yzMitt.on('queryYz', (val) => {
        return queryYz(val)
    })
})


</script>

<style scoped lang="scss">
.status_color {
  width: 10px;
  height: 10px;
}

</style>
