<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="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" />
                </el-form-item>
                <el-form-item label="审核状态" prop="approvalStatus">
                    <el-select
                        v-model="queryParams.approvalStatus"
                        class="w-[280px]"
                        clearable
                        @change="getLists()"
                    >
                        <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>
                    <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">
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
                border
            >
                <el-table-column label="报名编号" min-width="100">
                    <template #default="{ row }">
                        <el-button
                            type="primary"
                            link
                            @click="handleShowDetail(row.baseInfoId)"
                            style="text-decoration: underline"
                        >
                            {{ row.applicationNumber }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="名称" prop="name" min-width="100" />
                <el-table-column
                    label="性别"
                    prop="gender"
                    min-width="100"
                    :formatter="formatGender"
                />
                <el-table-column label="籍贯" prop="nativePlace" min-width="100" />
                <el-table-column
                    label="审核状态"
                    prop="approvalStatus"
                    min-width="100"
                    :formatter="formatRegistrationStatus"
                />
                <el-table-column label="报名时间" prop="applicationTime" min-width="100" />
                <el-table-column label="操作" width="240" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['stuRegistration/edit']"
                            @click="handleRoEdit(row)"
                            :disabled="row.approvalStatus == 0"
                        >
                            撤销
                        </el-button>
                        <el-button
                            v-perms="['stuRegistration/edit']"
                            type="success"
                            @click="handleYesEdit(row)"
                            :disabled="row.approvalStatus != 0"
                        >
                            通过
                        </el-button>
                        <el-button
                            v-perms="['stuRegistration/edit']"
                            type="danger"
                            @click="handleNoEdit(row)"
                            :disabled="row.approvalStatus != 0"
                        >
                            拒绝
                        </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" />
        <base-popup ref="detailRef" />
    </div>
</template>
<script lang="ts" setup name="stuRegistration">
import {
    stuRegistrationRegistration,
    stuRegistrationRegistrationLists
} from '@/api/stuRegistration'
import { usePaging } from '@/hooks/usePaging'

import BasePopup from '../Base.vue'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const detailRef = shallowRef<InstanceType<typeof BasePopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    name: '',
    baseInfoId: '',
    approvalStatus: '',
    applicationNumber: '',
    rejectionReason: '',
    admissionStatus: '',
    paymentStatus: '',
    registrationStatus: 0,
    applicationTimeStart: '',
    applicationTimeEnd: ''
})

const formData = reactive({
    id: '',
    operation: 0,
    reason: ''
})

const formatRegistrationStatus = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '0': '待审核',
        '1': '已审核',
        '2': '审核不通过'
    }
    const statusText = statusMap[String(cellValue)] || cellValue
    // 当审核状态为2（不通过）时，显示拒绝原因
    if (cellValue === 2 && row.rejectionReason) {
        return `${statusText} ： ${row.rejectionReason}`
    }
    return statusText
}

const formatGender = (value: number) => {
    const map: Record<number, string> = {
        0: '未知',
        1: '男',
        2: '女'
    }
    return map[value] || '未知'
}

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: stuRegistrationRegistrationLists,
    params: queryParams
})

const handleShowDetail = async (id: number) => {
    await nextTick()
    detailRef.value?.open(id)
}

const handleRoEdit = async (data: any) => {
    formData.id = data.id
    formData.operation = 0
    const editData: any = { ...formData }
    await stuRegistrationRegistration(editData)
    getLists()
}

const handleYesEdit = async (data: any) => {
    formData.id = data.id
    formData.operation = 1
    const editData: any = { ...formData }
    await stuRegistrationRegistration(editData)
    getLists()
}

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

getLists()
</script>
