<template>
	<div class="app-container">
		<!-- 搜索 -->
		<el-card shadow="never" class="mb12">
			<el-form :inline="true" :model="searchForm" ref="searchFormRef" label-width="90px">
				<el-form-item label="学号" prop="stuNo">
					<el-input v-model="searchForm.stuNo" placeholder="请输入学号" clearable />
				</el-form-item>
				<el-form-item label="姓名" prop="stuName">
					<el-input v-model="searchForm.stuName" placeholder="请输入姓名" clearable />
				</el-form-item>
				<el-form-item label="身份证" prop="idCard">
					<el-input v-model="searchForm.idCard" placeholder="请输入身份证" clearable />
				</el-form-item>
				<el-form-item label="电话" prop="phone">
					<el-input v-model="searchForm.phone" placeholder="请输入电话" clearable />
				</el-form-item>
				<el-form-item label="家长电话" prop="parentPhone">
					<el-input v-model="searchForm.parentPhone" placeholder="请输入家长电话" clearable />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleSearch">
						<el-icon><Search /></el-icon>
						查询
					</el-button>
					<el-button @click="handleReset">
						<el-icon><Refresh /></el-icon>
						重置
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<!-- 表格 -->
		<el-card shadow="never">
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@sort-change="sortChangeHandle"
				style="width: 100%"
			>
				<el-table-column type="index" label="序号" width="70" align="center">
					<template #header>
						<el-icon><List /></el-icon>
						<span style="margin-left: 4px">序号</span>
					</template>
				</el-table-column>
				<el-table-column prop="deptName" label="学院" show-overflow-tooltip>
					<template #header>
						<el-icon><OfficeBuilding /></el-icon>
						<span style="margin-left: 4px">学院</span>
					</template>
				</el-table-column>
				<el-table-column prop="majorName" label="专业" show-overflow-tooltip>
					<template #header>
						<el-icon><Briefcase /></el-icon>
						<span style="margin-left: 4px">专业</span>
					</template>
				</el-table-column>
				<el-table-column prop="className" label="班级" show-overflow-tooltip>
					<template #header>
						<el-icon><Grid /></el-icon>
						<span style="margin-left: 4px">班级</span>
					</template>
				</el-table-column>
				<el-table-column prop="stuNo" label="学号" show-overflow-tooltip width="130">
					<template #header>
						<el-icon><Document /></el-icon>
						<span style="margin-left: 4px">学号</span>
					</template>
				</el-table-column>
				<el-table-column prop="stuName" label="姓名" show-overflow-tooltip>
					<template #header>
						<el-icon><UserFilled /></el-icon>
						<span style="margin-left: 4px">姓名</span>
					</template>
				</el-table-column>
				<el-table-column prop="idCard" label="身份证" show-overflow-tooltip width="180">
					<template #header>
						<el-icon><CreditCard /></el-icon>
						<span style="margin-left: 4px">身份证</span>
					</template>
				</el-table-column>
				<el-table-column prop="phone" label="电话" show-overflow-tooltip width="130">
					<template #header>
						<el-icon><Phone /></el-icon>
						<span style="margin-left: 4px">电话</span>
					</template>
				</el-table-column>
				<el-table-column prop="parentPhone" label="家长电话" show-overflow-tooltip width="140">
					<template #header>
						<el-icon><Phone /></el-icon>
						<span style="margin-left: 4px">家长电话</span>
					</template>
				</el-table-column>
				<el-table-column prop="gender" label="性别" width="90" align="center">
					<template #header>
						<el-icon><User /></el-icon>
						<span style="margin-left: 4px">性别</span>
					</template>
					<template #default="scope">
						<GenderTag :gender="scope.row.gender" />
					</template>
				</el-table-column>
				<el-table-column prop="dormNo" label="宿舍号" show-overflow-tooltip width="120">
					<template #header>
						<el-icon><HomeFilled /></el-icon>
						<span style="margin-left: 4px">宿舍号</span>
					</template>
				</el-table-column>
				<el-table-column prop="teacherName" label="班主任" show-overflow-tooltip>
					<template #header>
						<el-icon><UserFilled /></el-icon>
						<span style="margin-left: 4px">班主任</span>
					</template>
				</el-table-column>
				<el-table-column prop="teacherPhone" label="班主任电话" show-overflow-tooltip width="140">
					<template #header>
						<el-icon><Phone /></el-icon>
						<span style="margin-left: 4px">班主任电话</span>
					</template>
				</el-table-column>
				<el-table-column prop="schoolRollStatus" label="学籍状态" show-overflow-tooltip width="110" align="center">
					<template #header>
						<el-icon><CircleCheck /></el-icon>
						<span style="margin-left: 4px">学籍状态</span>
					</template>
					<template #default="scope">
						<StatusTag :status="scope.row.schoolRollStatus" :options="rollStatusOptions" />
					</template>
				</el-table-column>
				<el-table-column prop="stuStatus" label="学生状态" show-overflow-tooltip width="110" align="center">
					<template #header>
						<el-icon><Tickets /></el-icon>
						<span style="margin-left: 4px">学生状态</span>
					</template>
					<template #default="scope">
						<StatusTag :status="scope.row.stuStatus" :options="stuStatusOptions" />
					</template>
				</el-table-column>
				<el-table-column prop="avatarUrl" label="头像" width="120" align="center">
					<template #header>
						<el-icon><Picture /></el-icon>
						<span style="margin-left: 4px">头像</span>
					</template>
					<template #default="scope">
						<el-image
							v-if="scope.row.avatarUrl"
							:src="scope.row.avatarUrl"
							:preview-src-list="[scope.row.avatarUrl]"
							fit="cover"
							style="width: 60px; height: 60px; border-radius: 6px"
						/>
						<el-tag v-else type="info" effect="plain">无</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120" fixed="right" align="center">
					<template #header>
						<el-icon><Setting /></el-icon>
						<span style="margin-left: 4px">操作</span>
					</template>
					<template #default>
						<span style="color: #999">—</span>
					</template>
				</el-table-column>
			</el-table>

			<!-- 分页 -->
			<div class="pagination-wrapper">
				<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
			</div>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="QueryStuindex">
import { ref, reactive } from 'vue';
import {
	Search,
	Refresh,
	List,
	OfficeBuilding,
	Grid,
	Document,
	UserFilled,
	CreditCard,
	Phone,
	User,
	HomeFilled,
	CircleCheck,
	Tickets,
	Setting,
	Picture,
	Briefcase,
} from '@element-plus/icons-vue';
import { BasicTableProps, useTable } from '/@/hooks/table';
import { queryStuindex } from '/@/api/basic/basicstudent';
import GenderTag from '/@/components/GenderTag/index.vue';
import StatusTag from '/@/components/StatusTag/index.vue';

// 搜索表单
const searchFormRef = ref();
const searchForm = reactive({
	stuNo: '',
	stuName: '',
	idCard: '',
	phone: '',
	parentPhone: '',
});

// 状态映射（可根据实际字典调整）
const rollStatusOptions = [
	{ value: '0', label: '在籍', type: 'success' },
	{ value: '1', label: '离校', type: 'warning' },
	{ value: '2', label: '休学', type: 'info' },
];
const stuStatusOptions = [
	{ value: '0', label: '正常', type: 'success' },
	{ value: '1', label: '预警', type: 'warning' },
	{ value: '2', label: '异常', type: 'danger' },
];

// 表格配置
const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: searchForm,
	pageList: queryStuindex,
	props: {
		item: 'records',
		totalCount: 'total',
	},
	createdIsNeed: true,
});

const { getDataList, sortChangeHandle, sizeChangeHandle, currentChangeHandle, tableStyle } = useTable(state);

const handleSearch = () => {
	getDataList();
};

const handleReset = () => {
	searchFormRef.value?.resetFields();
	getDataList();
};
</script>

<style scoped>
.app-container {
	padding: 16px;
}
.mb12 {
	margin-bottom: 12px;
}
.pagination-wrapper {
	margin-top: 20px;
	display: flex;
	justify-content: flex-end;
}
</style>
