<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="报名编号" prop="applicationNumber">
                    <el-input class="w-[280px]" v-model="queryParams.applicationNumber" />
                </el-form-item>
                <el-form-item label="邀请码" prop="invitationCode">
                    <el-input class="w-[280px]" v-model="queryParams.invitationCode" />
                </el-form-item>
                <el-form-item label="录取状态" prop="admissionStatus">
                    <el-select v-model="queryParams.admissionStatus" class="w-[280px]" clearable>
                        <el-option label="待审核" :value="0" />
                        <el-option label="已录取" :value="1" />
                        <el-option label="未录取" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="缴费状态" prop="paymentStatus">
                    <el-select v-model="queryParams.paymentStatus" class="w-[280px]" clearable>
                        <el-option label="未缴费" :value="0" />
                        <el-option label="已缴费" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="入学状态" prop="registrationStatus">
                    <el-select v-model="queryParams.registrationStatus" class="w-[280px]" clearable>
                        <el-option label="未入学" :value="0" />
                        <el-option label="已入学" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="审核状态" prop="approvalStatus">
                    <el-select v-model="queryParams.approvalStatus" class="w-[280px]" clearable>
                        <el-option label="待审核" :value="0" />
                        <el-option label="审核通过" :value="1" />
                        <el-option label="审核不通过" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="报名时间" prop="applicationTime">
                    <daterange-picker
                        v-model:startTime="queryParams.applicationTimeStart"
                        v-model:endTime="queryParams.applicationTimeEnd"
                    />
                </el-form-item>
                <el-form-item label="录取时间" prop="admissionTime">
                    <daterange-picker
                        v-model:startTime="queryParams.admissionTimeStart"
                        v-model:endTime="queryParams.admissionTimeEnd"
                    />
                </el-form-item>
                <el-form-item label="缴费时间" prop="paymentTime">
                    <daterange-picker
                        v-model:startTime="queryParams.paymentTimeStart"
                        v-model:endTime="queryParams.paymentTimeEnd"
                    />
                </el-form-item>
                <el-form-item label="入学登记时间" prop="registrationTime">
                    <daterange-picker
                        v-model:startTime="queryParams.registrationTimeStart"
                        v-model:endTime="queryParams.registrationTimeEnd"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['stuRegistration/add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
                <el-form-item
                    v-perms="['stuRegistration/list']"
                    class="ml-4"
                    label="当前权限:ALL"
                />
                <el-form-item
                    v-perms="['stuRegistration/list.admission']"
                    class="ml-4"
                    label="当前权限:录取管理"
                />
                <el-form-item
                    v-perms="['stuRegistration/list.enrollment']"
                    class="ml-4"
                    label="当前权限:入学管理"
                />
                <el-form-item
                    v-perms="['stuRegistration/list.payment']"
                    class="ml-4"
                    label="当前权限:缴费管理"
                />
                <el-form-item
                    v-perms="['stuRegistration/list.registration']"
                    class="ml-4"
                    label="当前权限:审核管理"
                />
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="关联基本信息ID" prop="baseInfoId" min-width="100" />
                <el-table-column label="报名编号" prop="applicationNumber" min-width="100" />
                <el-table-column label="邀请码" prop="invitationCode" min-width="100" />
                <el-table-column
                    label="录取状态"
                    prop="admissionStatus"
                    min-width="100"
                    :formatter="formatAdmissionStatus"
                />
                <el-table-column
                    label="缴费状态"
                    prop="paymentStatus"
                    min-width="100"
                    :formatter="formatPaymentStatus"
                />
                <el-table-column
                    label="入学状态"
                    prop="registrationStatus"
                    min-width="100"
                    :formatter="formatRegistrationStatus"
                />
                <el-table-column
                    label="审核状态"
                    prop="approvalStatus"
                    min-width="100"
                    :formatter="formatApprovalStatus"
                />
                <el-table-column label="审核人ID" prop="approvedBy" min-width="100" />
                <el-table-column label="审核时间" prop="approvedTime" min-width="100" />
                <el-table-column label="拒绝原因" prop="rejectionReason" min-width="100" />
                <el-table-column label="报名时间" prop="applicationTime" min-width="100" />
                <el-table-column label="录取时间" prop="admissionTime" min-width="100" />
                <el-table-column label="缴费时间" prop="paymentTime" min-width="100" />
                <el-table-column label="入学登记时间" prop="registrationTime" min-width="100" />
                <el-table-column label="创建时间" prop="createTime" min-width="100" />
                <el-table-column label="更新时间" prop="updateTime" min-width="100" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['stuRegistration/edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['stuRegistration/del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="stuRegistration">
import {
    stuRegistrationAdmissionLists,
    stuRegistrationDelete,
    stuRegistrationEnrollmentLists,
    stuRegistrationLists,
    stuRegistrationPaymentLists,
    stuRegistrationRegistrationLists
} from '@/api/stuRegistration'
import { usePaging } from '@/hooks/usePaging'
import useUserStore from '@/stores/modules/user'
import feedback from '@/utils/feedback'

import EditPopup from './edit.vue'
const userStore = useUserStore()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    baseInfoId: '',
    applicationNumber: '',
    invitationCode: '',
    admissionStatus: '',
    paymentStatus: '',
    registrationStatus: '',
    approvalStatus: '',
    approvedBy: '',
    applicationTimeStart: '',
    applicationTimeEnd: '',
    admissionTimeStart: '',
    admissionTimeEnd: '',
    paymentTimeStart: '',
    paymentTimeEnd: '',
    registrationTimeStart: '',
    registrationTimeEnd: ''
})

const formatAdmissionStatus = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '0': '待审核',
        '1': '已录取',
        '2': '未录取'
    }
    return statusMap[String(cellValue)] || cellValue
}

const formatPaymentStatus = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '0': '未缴费',
        '1': '已缴费'
    }
    return statusMap[String(cellValue)] || cellValue
}

const formatRegistrationStatus = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '0': '未入学',
        '1': '已入学'
    }
    return statusMap[String(cellValue)] || cellValue
}

const formatApprovalStatus = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '0': '待审核',
        '1': '已审核',
        '2': '审核不通过'
    }
    return statusMap[String(cellValue)] || cellValue
}

// 根据权限选择对应的 API
const getFetchFun = () => {
    const { perms } = userStore
    if (perms.includes('stuRegistration/list.admission')) {
        return stuRegistrationAdmissionLists
    } else if (perms.includes('stuRegistration/list.enrollment')) {
        return stuRegistrationEnrollmentLists
    } else if (perms.includes('stuRegistration/list.payment')) {
        return stuRegistrationPaymentLists
    } else if (perms.includes('stuRegistration/list.registration')) {
        return stuRegistrationRegistrationLists
    } else if (perms.includes('stuRegistration/list')) {
        return stuRegistrationLists
    }
    // 默认返回基础列表接口
    return stuRegistrationLists
}

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getFetchFun(), // 使用动态选择的 API
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await stuRegistrationDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
