<template>
    <div style="height: 5%">
        医嘱
        <el-radio-group v-model="radio">
            <el-radio :label="0">临时</el-radio>
            <el-radio :label="1">长期</el-radio>
        </el-radio-group>
        <el-divider direction="vertical"/>
        <el-select v-model="orderType" style="width: 90px">
            <el-option :value="1" label="全部"/>
            <el-option :value="2" label="已停止"/>
            <el-option :value="3" label="当前"/>
            <el-option :value="4" label="今日"/>
        </el-select>
        <el-button @click="copyClick">复制值</el-button>
    </div>

    <div style="height: 95%">
        <el-auto-resizer>
            <template #default="{ height, width }">
                <vxe-table :data="tempList"
                           :height="height"
                           align="left"
                           border
                           :row-config="{isHover : true, isCurrent: true,height: 24,useKey :  'actOrderNo'}"
                           show-overflow
                           ref="tableRef">
                    <vxe-column type="checkbox" min-width="25"/>
                    <vxe-column type="seq" min-width="30"/>
                    <vxe-column title="开始时间" field="startTime" min-width="150"/>
                    <vxe-column title="医嘱号" field="actOrderNo" min-width="90"/>
                    <vxe-column title="组" field="orderGroup" width="35">
                        <template #default="{row}">
                            <span style="color: red">{{ row.orderGroup }}</span>
                        </template>
                    </vxe-column>
                    <vxe-column title="医嘱名称" field="orderName" min-width="220"/>
                    <vxe-column title="嘱托" field="instruction" min-width="90"/>
                    <vxe-column title="频率" field="frequCode" min-width="90"/>
                    <vxe-column title="给药方式" field="supplyCodeName" min-width="90"/>
                    <vxe-column title="申请科室" field="deptName" min-width="90"/>
                    <vxe-column title="执行科室" field="execUnitName" min-width="90"/>
                    <vxe-colgroup title="确认">
                        <vxe-column title="医生" field="signerName" min-width="60"/>
                        <vxe-column title="护士" field="executer1" min-width="60"/>
                    </vxe-colgroup>
                    <vxe-colgroup title="停止">
                        <vxe-column title="时间" field="endTime" min-width="60"/>
                        <vxe-column title="医生" field="modifierName" min-width="60"/>
                        <vxe-column title="护士" field="executer1" min-width="60"/>
                    </vxe-colgroup>
                </vxe-table>
            </template>
        </el-auto-resizer>
    </div>
</template>

<script setup name='EmrOrderList' lang="tsx">
import {computed, Ref, ref, onMounted} from "vue";
import {
    huoQuYiZhuShuJu
} from "../../../../api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
import {getFormatDatetime} from "@/utils/date";
import {getServerDateApi} from "@/api/public-api";
import XcTableV3 from "@/components/xiao-chan/xc-table-v3/XcTableV3.vue";
import {XcColumn} from "@/components/xiao-chan/xc-table-v3/XcColumn";
import {xcMessage} from "@/utils/xiaochan-element-plus";
import {stringNotBlank} from "@/utils/blank-utils";
import {emrCopyFunc} from '@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init'

const props = defineProps({
    patNo: String,
    times: Number,
    inTheHospital: Boolean,
})

const list: Ref<Array<any>> = ref([])
const radio = ref(1)
const orderType = ref(3)
const tableRef = ref(null)

const columns: XcColumn[] = [
    {
        type: 'selection',
        title: '选择',
    },
    {
        type: 'index',
        title: '序号',
        width: 30
    },
    {key: 'startTime', title: '开始时间', width: 150},
    {key: 'actOrderNo', title: '医嘱号', width: 90},
    {
        key: 'orderGroup', title: '组', width: 20,
        cellRenderer: ({row}) => (
            <span style="color: red"> {row.orderGroup}</span>
        )
    },
    {
        key: 'orderName', title: '医嘱名称',
    },
    {
        key: 'instruction', title: '嘱托', width: 90
    },
    {
        key: 'frequCode', title: '频率', width: 90
    },
    {
        key: 'supplyCodeName', title: '给药方式', width: 90
    },
    {
        key: 'deptName', title: '申请科室', width: 90,
    },
    {
        key: 'execUnitName', title: '执行科室', width: 90,
    },
    {
        title: '确认',
        children: [
            {key: 'signerName', title: '确认医生', width: 120},
            {key: 'executer1', title: '确认护士', width: 120}
        ]
    },
    {
        title: '停止',
        children: [
            {key: 'endTime', title: '停止时间', width: 220},
            {key: 'modifierName', title: '停止医生', width: 120},
            {key: 'executer1', title: '停止护士', width: 120}
        ]
    },
]

let newDate = ''
getServerDateApi().then(res => {
    newDate = res as any
})

const tempList = computed(() => {
    return list.value.filter(item => {
        let onceFlag = false
        let type = false
        if (radio.value === 0 && item.frequCode === 'ONCE') {
            onceFlag = true
        } else if (radio.value === 1 && item.frequCode !== 'ONCE') {
            onceFlag = true
        }
        if (orderType.value === 1) {
            type = true
        }
        if (orderType.value === 2) {
            type = item.statusFlag === '5'
        }
        if (orderType.value === 3) {
            type = item.statusFlag === '3' || item.statusFlag === '4'
        }
        if (orderType.value === 4) {
            type = getFormatDatetime(newDate, 'YYYY-MM-DD') === getFormatDatetime(item.startTime, 'YYYY-MM-DD')
        }
        return onceFlag && type
    })
})

const open = () => {
    huoQuYiZhuShuJu({
        patNo: props.patNo,
        times: props.times,
        inTheHospital: props.inTheHospital
    }).then(res => {
        list.value = res as any
    })
}

const copyClick = () => {
    let tempData = tableRef.value.getCheckboxRecords(true)
    let str = ''
    if (tempData.length > 0) {
        for (let i = 0; i < tempData.length; i++) {
            let item = tempData[i]
            str += item.orderName
            if (stringNotBlank(item.instruction)) {
                str += "，" + item.instruction
            }
            if (stringNotBlank(item.instruction)) {
                str += "，" + item.instruction
            }
            if (stringNotBlank(item.supplyCodeName)) {
                str += "，" + item.supplyCodeName
            }
            if (stringNotBlank(item.drugQuan)) {
                str += "，" + item.drugQuan
                if (stringNotBlank(item.drugQuanName)) {
                    str += item.drugQuanName
                } else {
                    str += '次'
                }
            }
            str += i + 1 === tempData.length ? '。' : '、';
        }
        emrCopyFunc(str)

    } else {
        xcMessage.error('请先选择医嘱。')
    }
}

onMounted(() => {
    open()
})

</script>

<style scoped lang="scss">


</style>
