<template>
    <div style="display: flex">
        <div>
            <el-button icon="Search" type="primary" @click="chaKanHuiZhenShenQingClick">会诊查询</el-button>
            <el-button icon="Plus" type="success" @click="dianJiHuiZhenShenQing">会诊申请</el-button>
            <div style="width: 400px">
                <xc-table
                        :final-height="getWindowSize.h  - yzHeaderSize"
                        :local-data="chaKanHuiZhenList"
                        @rowClick="chaKanHuiZhenDan"
                        layout='total, prev,  next'>
                    <el-table-column label="申请时间" prop="inputDate" width="70">
                        <template #default="scope">
                            {{ scope.row.inputDate + '_' + scope.row.reqTimes }}
                        </template>
                    </el-table-column>
                    <el-table-column label="科室" prop="deptName"></el-table-column>
                    <el-table-column label="接受日期" prop="hzDate" show-overflow-tooltip></el-table-column>
                    <el-table-column label="接受医生" prop="hzDoctor2Name"></el-table-column>
                    <el-table-column label="申请人" prop="inputName" width="60"></el-table-column>
                    <el-table-column label="申请科室" prop="reqDeptName"></el-table-column>
                    <el-table-column label="类别" prop="hzLevelName"></el-table-column>
                    <el-table-column label="医生" prop="hzDoctor1Name"></el-table-column>
                    <el-table-column label="级别" prop="hzType">
                        <template #default="scope">
                            <span>{{ hzTypeName(scope.row.hzType) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="接受意见" prop="hzComment" show-overflow-tooltip></el-table-column>
                    <el-table-column fixed="left" label="操作" width="50">
                        <template #default="scope">
                            <el-popconfirm
                                    cancel-button-text="取消"
                                    confirm-button-text="确定"
                                    icon="InfoFilled"
                                    icon-color="red"
                                    title="确定要删除吗？"
                                    @confirm="shanChuHuiZhenShenQingClick(scope.$index, scope.row)"
                            >
                                <template #reference>
                                    <el-button :disabled="scope.row.statusFlag === '2'"
                                               title="删除"
                                               circle
                                               @click.stop.prevent
                                               icon="Delete"
                                               type="danger"/>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </xc-table>
            </div>
        </div>

        <div>
            <el-tabs type="border-card"
                     closable
                     v-model="tabs"
                     @tab-remove="tabRemove">
                <el-tab-pane label="会诊详情"
                             :name="0">
                    <print-the-consultation-form ref="daYingHuiZhenRef" :lai-yuan="2"/>
                </el-tab-pane>
                <el-tab-pane :name="1"
                             label="添加会诊申请"
                             v-if="consultationApplication">
                    <tian-jia-hui-zhen-shen-qing @successfulApplication="successfulApplication"/>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>

</template>
<script setup name="HuiZhenShenQing">
import TianJiaHuiZhenShenQing from '@/components/zhu-yuan-yi-sheng/hui-zhen-shen-qing/TianJiaHuiZhenShenQing.vue'
import {computed, onActivated, ref, watch} from 'vue'
import {chaKanHuiZhenShenQing, shanChuHuiZhenShenQing} from '@/api/case-front-sheet'
import {
    consultationApplication,
    huanZheXinXi,
    youWuXuanZheHuanZhe, yzHeaderSize
} from '@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng'
import store from '@/store'
import {getHuanZheXinXi} from '@/api/case-front-sheet/jie-shou-hui-zhen'
import XcTable from "@/components/xiao-chan/xc-table/XcTable";
import {BizException, ExceptionEnum} from "@/utils/BizException";
import PrintTheConsultationForm from "@/components/hui-zhen-da-ying/PrintTheConsultationForm.vue";
import {getWindowSize} from "@/utils/window-size";

const windowSize = computed(() => {
    return store.state.app.windowSize
})


const chaKanHuiZhenList = ref([])

const tabs = ref(0)

const chaKanHuiZhenShenQingClick = () => {
    if (youWuXuanZheHuanZhe()) return
    chaKanHuiZhenShenQing(huanZheXinXi.value.inpatientNo, huanZheXinXi.value.admissTimes).then((res) => {
        chaKanHuiZhenList.value = res
    })
}

const shanChuHuiZhenShenQingClick = (index, val) => {
    shanChuHuiZhenShenQing(val.inpatientNo, val.admissTimes, val.actOrderNo, val.reqTimes).then((res) => {
        chaKanHuiZhenList.value.splice(index, 1)
    })
}


const daYingHuiZhenRef = ref(null)

const chaKanHuiZhenDan = (val) => {
    getHuanZheXinXi(val.admissTimes, val.reqTimes, val.inpatientNo).then((res) => {
        setTimeout(() => {
            daYingHuiZhenRef.value.huiZhenXinXiXiangQing(res)
        }, 100)
    })
}

const successfulApplication = () => {
    chaKanHuiZhenShenQingClick()
    consultationApplication.value = false
    tabs.value = 0
}

/**
 * 添加会诊申请
 *
 */
const dianJiHuiZhenShenQing = async () => {
    if (youWuXuanZheHuanZhe()) return
    if (consultationApplication.value) {
        BizException(ExceptionEnum.MESSAGE_ERROR, '请先保存正在编辑的会诊申请')
    }
    consultationApplication.value = true
    tabs.value = 1
}

// 移除标签
const tabRemove = () => {
    consultationApplication.value = false
    tabs.value = 0
}

watch(
    () => huanZheXinXi.value,
    () => {
        chaKanHuiZhenShenQingClick()
    }
)

onActivated(() => {
    chaKanHuiZhenShenQingClick()
})

function hzTypeName(val) {
    switch (val) {
        case '1':
            return '主治医师'
        case '2':
            return '副主任医生'
        case '3':
            return '主任医生'
    }
}
</script>

<style scoped lang="scss"></style>


