<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="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" />
                </el-form-item>
                <el-form-item label="身份证号" prop="idCard">
                    <el-input class="w-[280px]" v-model="queryParams.idCard" />
                </el-form-item>
                <el-form-item label="民族" prop="nationality">
                    <el-input class="w-[280px]" v-model="queryParams.nationality" />
                </el-form-item>
                <el-form-item label="政治面貌" prop="politicalStatus">
                    <el-select v-model="queryParams.politicalStatus" class="w-[280px]" clearable>
                        <el-option label="群众" :value="0" />
                        <el-option label="团员" :value="1" />
                        <el-option label="党员" :value="2" />
                        <el-option label="其他" :value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input class="w-[280px]" v-model="queryParams.phone" />
                </el-form-item>
                <el-form-item label="籍贯" prop="nativePlace">
                    <el-input class="w-[280px]" v-model="queryParams.nativePlace" />
                </el-form-item>
                <el-form-item label="学校类型" prop="schoolType">
                    <el-select v-model="queryParams.schoolType" class="w-[280px]" clearable>
                        <el-option label="普通高中" :value="1" />
                        <el-option label="职业高中" :value="2" />
                        <el-option label="中专" :value="3" />
                        <el-option label="大专" :value="4" />
                        <el-option label="本科" :value="5" />
                        <el-option label="其他" :value="6" />
                    </el-select>
                </el-form-item>
                <el-form-item label="学历" prop="academicQualification">
                    <el-select
                        v-model="queryParams.academicQualification"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="初中" :value="1" />
                        <el-option label="高中" :value="2" />
                        <el-option label="中专" :value="3" />
                        <el-option label="大专" :value="4" />
                        <el-option label="本科" :value="5" />
                        <el-option label="硕士" :value="6" />
                        <el-option label="博士" :value="7" />
                    </el-select>
                </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="['stuBaseInfo:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="关联用户ID" prop="userId" min-width="100" />
                <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="idCard" min-width="100" />
                <el-table-column label="出生日期" prop="birthday" min-width="100" />
                <el-table-column label="民族" prop="nationality" min-width="100" />
                <el-table-column
                    label="政治面貌"
                    prop="politicalStatus"
                    min-width="100"
                    :formatter="formatPoliticalStatus"
                />
                <el-table-column label="手机号码" prop="phone" min-width="100" />
                <el-table-column label="邮箱" prop="email" min-width="100" />
                <el-table-column label="紧急联系人" prop="emergencyContact" min-width="100" />
                <el-table-column label="紧急联系电话" prop="emergencyPhone" min-width="100" />
                <el-table-column label="与紧急联系人关系" prop="relationship" min-width="100" />
                <el-table-column label="家庭住址" prop="homeAddress" min-width="100" />
                <el-table-column label="籍贯" prop="nativePlace" min-width="100" />
                <el-table-column label="邮政编码" prop="postalCode" min-width="100" />
                <el-table-column label="毕业学校" prop="previousSchool" min-width="100" />
                <el-table-column
                    label="学校类型"
                    prop="schoolType"
                    min-width="100"
                    :formatter="formatSchoolType"
                />
                <el-table-column label="毕业年份" prop="graduationYear" min-width="100" />
                <el-table-column
                    label="学历"
                    prop="academicQualification"
                    min-width="100"
                    :formatter="formatAcademicQualification"
                />
                <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="['stuBaseInfo:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['stuBaseInfo: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="stuBaseInfo">
import { stuBaseInfoDelete, stuBaseInfoLists } from '@/api/stuBaseInfo'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'

import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    userId: '',
    name: '',
    gender: '',
    idCard: '',
    nationality: '',
    politicalStatus: '',
    phone: '',
    nativePlace: '',
    schoolType: '',
    graduationYear: '',
    academicQualification: ''
})

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

const formatGender = (row: any, column: any, cellValue: any) => {
    const genderMap: Record<string | number, string> = {
        '0': '未知',
        '1': '男',
        '2': '女'
    }
    return genderMap[String(cellValue)] || cellValue
}

const formatPoliticalStatus = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '0': '群众',
        '1': '团员',
        '2': '党员',
        '3': '其他'
    }
    return statusMap[String(cellValue)] || cellValue
}

const formatSchoolType = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '1': '普通高中',
        '2': '职业高中',
        '3': '中专',
        '4': '大专',
        '5': '本科',
        '6': '其他'
    }
    return statusMap[String(cellValue)] || cellValue
}

const formatAcademicQualification = (row: any, column: any, cellValue: any) => {
    const statusMap: Record<string | number, string> = {
        '1': '初中',
        '2': '高中',
        '3': '中专',
        '4': '大专',
        '5': '本科',
        '6': '硕士',
        '7': '博士'
    }
    return statusMap[String(cellValue)] || cellValue
}

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 stuBaseInfoDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
