<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div v-show="showSearch" class="mb-[10px]">
                <el-card shadow="hover">
                    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                        <el-form-item label="筛查类型" prop="configId">
                            <div>
                                <el-select v-model="queryParams.configId" placeholder="请选择" clearable>
                                    <el-option v-for="item in screeningAssessmentConfigList" :key="item.configId" :label="item.name" :value="item.configId" />
                                </el-select>
                            </div>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </transition>

        <el-card shadow="never">
            <template #header>
                <el-row :gutter="10" class="mb8">
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </template>
            <el-table v-loading="loading" border :data="screeningList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="营养筛查时间" align="center" prop="screeningTime" />
                <el-table-column label="营养筛查类型" align="center" prop="configName" />
                <el-table-column label="营养筛查分数" align="center" prop="screeningScore" />
                <el-table-column label="营养筛查结论" align="center" prop="screeningConclusion" />
                <el-table-column label="筛查医生/护士" align="center" prop="createByName" />
                <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-tooltip content="详情" placement="top">
                            <el-button link type="primary" icon="View" @click="handleView(scope.row)">详情</el-button>
                        </el-tooltip>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>
        <!-- 添加或修改营养筛查对话框 -->

    </div>
</template>

<script setup name="NutritionScreening" lang="ts">
    import { listScreening, } from '@/api/patients/screening';
    import { ScreeningVO, ScreeningQuery, ScreeningForm } from '@/api/patients/screening/types';
    import { listScreeningAssessmentConfig } from '@/api/system/screeningAssessmentConfig';
    import { ScreeningAssessmentConfigQuery, ScreeningAssessmentConfigVO } from '@/api/system/screeningAssessmentConfig/types';

    const { proxy } = getCurrentInstance() as ComponentInternalInstance;
    const { treatment_user_type } = toRefs < any > (proxy ?.useDict('treatment_user_type'));

    const screeningTime = ref < [DateModelType, DateModelType] > (['', '']);
    const screeningList = ref < ScreeningVO[] > ([]);
    const buttonLoading = ref(false);
    const loading = ref(true);
    const showSearch = ref(true);
    const ids = ref < Array < string | number >> ([]);
    const single = ref(true);
    const multiple = ref(true);
    const total = ref(0);
    const screeningAssessmentConfigList = ref < ScreeningAssessmentConfigVO[] > ([]);
    const router = useRouter();
    const queryFormRef = ref < ElFormInstance > ();
    const screeningFormRef = ref < ElFormInstance > ();
    const { patientInfo } = defineProps < { patientInfo: any } > ()
    const emit = defineEmits(['change'])

    const dialog = reactive < DialogOption > ({
        visible: false,
        title: ''
    });

    const initFormData: ScreeningForm = {
        id: undefined,
        patientId: undefined,
        configId: undefined,
        screeningTime: undefined,
        visitType: undefined,
        patientNo: undefined,
        screeningScore: undefined,
        screeningConclusion: undefined,
        paymentStatus: undefined,
        status: undefined,
    }
    const data = reactive < PageData < ScreeningForm,
        ScreeningQuery >> ({
            form: { ...initFormData },
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                patientId: undefined,
                configId: undefined,
                screeningTime: undefined,
                visitType: undefined,
                patientNo: undefined,
                screeningScore: undefined,
                screeningConclusion: undefined,
                paymentStatus: undefined,
                status: undefined,
                params: {}
            },
            rules: {
               
            }
        });

    const { queryParams, form, rules } = toRefs(data);

    /** 查询筛查/评估配置列表 */
    const getScreeningConfigList = async () => {
        let param = ref < ScreeningAssessmentConfigQuery > ({
            pageNum: 1,
            type: '1',
            pageSize: -1,
        });
        const res = await listScreeningAssessmentConfig(param.value);
        screeningAssessmentConfigList.value = res.rows;
    }

    /** 查询营养筛查列表 */
    const getList = async () => {
        loading.value = true;
        queryParams.value.patientId = patientInfo.id;
        const res = await listScreening(proxy ?.addDateRange(queryParams.value, screeningTime.value));
        screeningList.value = res.rows;
        total.value = res.total;
        loading.value = false;
    }
    const handleView = async (row ? : ScreeningVO) => {
        emit('change', 'nutritionScreeningAdd', window.btoa(row.id.toString()))
    }
    /** 取消按钮 */
    const cancel = () => {
        reset();
        dialog.visible = false;
    }

    /** 表单重置 */
    const reset = () => {
        form.value = { ...initFormData };
        screeningFormRef.value ?.resetFields();
    }

    /** 搜索按钮操作 */
    const handleQuery = () => {
        queryParams.value.pageNum = 1;
        getList();
    }


    /** 多选框选中数据 */
    const handleSelectionChange = (selection: ScreeningVO[]) => {
        ids.value = selection.map(item => item.id);
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }


    function formatDate(date) {
        const pad = n => n < 10 ? '0' + n : n;
        return date.getFullYear() + '-' +
            pad(date.getMonth() + 1) + '-' +
            pad(date.getDate());
    }

    onMounted(() => {
        getList();
        getScreeningConfigList();

        let now = new Date();
        let start = formatDate(now);
        let end = formatDate(new Date(now.getTime() - 24 * 60 * 60 * 1000 * 30));
        screeningTime.value = [end + ' 00:00:00', start + ' 23:59:59'];
    });
</script>

<style scoped>
    .screening-type-grid {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .screening-type-btn {
        font-weight: normal;
        font-size: 16px;
        height: 48px;
        border-radius: 8px;
    }
</style>