<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="报名编号" prop="applicationNumber">
                    <el-input v-model="formData.applicationNumber" placeholder="请输入报名编号" />
                </el-form-item>
                <el-form-item label="邀请码" prop="invitationCode">
                    <el-input v-model="formData.invitationCode" placeholder="请输入邀请码" />
                </el-form-item>
                <el-form-item label="审核状态" prop="approvalStatus">
                    <el-radio-group v-model="formData.approvalStatus" placeholder="请选择审核状态">
                        <el-radio :label="0">待审核</el-radio>
                        <el-radio :label="1">审核通过</el-radio>
                        <el-radio :label="2">审核不通过</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="拒绝原因" prop="rejectionReason">
                    <el-input
                        v-model="formData.rejectionReason"
                        placeholder="请输入拒绝原因"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="报名时间" prop="approvedTime">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.approvedTime"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择报名时间"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import type { PropType } from 'vue'

import {
    stuRegistrationAdd,
    stuRegistrationDetail,
    stuRegistrationEdit
} from '@/api/stuRegistration'
import Popup from '@/components/popup/index.vue'
import useUserStore from '@/stores/modules/user'
import feedback from '@/utils/feedback'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const userStore = useUserStore()
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑学生注册状态' : '新增学生注册状态'
})

const formData = reactive({
    id: '',
    userId: '',
    baseInfoId: '',
    applicationNumber: '',
    invitationCode: '',
    intendedCollegeId: '',
    intendedMajorId: '',
    admissionStatus: '',
    paymentStatus: '',
    registrationStatus: '',
    approvalStatus: '',
    rejectionReason: '',
    applicationTime: '',
    admissionTime: '',
    paymentTime: '',
    approvedTime: '',
    registrationTime: ''
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键ID',
            trigger: ['blur']
        }
    ],
    userId: [
        {
            required: true,
            message: '请输入用户ID',
            trigger: ['blur']
        }
    ],
    baseInfoId: [
        {
            required: true,
            message: '请输入关联基本信息ID',
            trigger: ['blur']
        }
    ],
    applicationNumber: [
        {
            required: true,
            message: '请输入报名编号',
            trigger: ['blur']
        }
    ],
    admissionStatus: [
        {
            required: true,
            message: '请选择录取状态：0-待审核 1-已录取 2-未录取',
            trigger: ['blur']
        }
    ],
    paymentStatus: [
        {
            required: true,
            message: '请选择缴费状态：0-未缴费 1-部分缴费 2-已缴费',
            trigger: ['blur']
        }
    ],
    registrationStatus: [
        {
            required: true,
            message: '请选择入学状态：0-未入学 1-已入学',
            trigger: ['blur']
        }
    ],
    approvalStatus: [
        {
            required: true,
            message: '请选择审核状态：0-待审核 1-审核通过 2-审核不通过',
            trigger: ['blur']
        }
    ],
    applicationTime: [
        {
            required: true,
            message: '请选择报名时间',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await stuRegistrationEdit(data) : await stuRegistrationAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    console.log('当前权限:', userStore.perms)
    console.log('getFetchFun', getFetchFun())
    popupRef.value?.open()
}

const getFetchFun = () => {
    const { perms } = userStore
    if (perms.includes('stuRegistration/list')) {
        return 5
    } else if (perms.includes('stuRegistration/list.enrollment')) {
        return 2
    } else if (perms.includes('stuRegistration/list.payment')) {
        return 3
    } else if (perms.includes('stuRegistration/list.registration')) {
        return 4
    } else if (perms.includes('stuRegistration/list.admission')) {
        return 1
    }
    return 0
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await stuRegistrationDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
