<template>
	<div class="task-container">
		<a-form layout="inline" :model="queryParam">
			<a-form-item label="年级：">
				<a-select style="min-width: 150px" v-model:value="queryParam.gradeLevel" allowClear placeholder="年级">
					<a-select-option v-for="item in levelEnum" :key="item.key" :value="item.key">
						{{ item.value }}
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item>
				<a-button type="primary" @click="submitForm">查询</a-button>
				<a-button style="margin-left: 20px" type="primary" @click="createTask">创建任务</a-button>
			</a-form-item>
		</a-form>

		<a-table
			:loading="listLoading"
			:data-source="tableData"
			:pagination="false"
			row-key="id"
			bordered
			style="margin-top: 16px"
		>
			<a-table-column title="Id" dataIndex="id" key="id" width="100" />
			<a-table-column title="标题" dataIndex="title" key="title" />
			<!-- <a-table-column title="学级" dataIndex="gradeLevel" key="gradeLevel" :customRender="levelFormatter" /> -->
			<a-table-column title="发送人" dataIndex="createUserName" key="createUserName" width="100" />
			<a-table-column title="创建时间" dataIndex="createTime" key="createTime" width="160">
				<template #default="{ record }">
					{{ formatDateTime(record.createTime) }}
				</template>
			</a-table-column>
			<a-table-column title="操作" key="action" align="center" width="160">
				<template #default="{ record }">
					<a-button size="small" @click="editTask(record)">编辑</a-button>
					<a-button size="small" danger style="margin-left: 8px" @click="deleteTask(record)">删除</a-button>
				</template>
			</a-table-column>
		</a-table>
		<a-pagination
			v-show="total > 0"
			:total="total"
			:current="queryParam.pageIndex"
			:pageSize="queryParam.pageSize"
			@change="onPageChange"
			@showSizeChange="onPageSizeChange"
			:showSizeChanger="true"
			:pageSizeOptions="['10', '20', '50', '100']"
			style="margin-top: 16px; text-align: right"
		/>
		<a-drawer
			:visible="drawerVisible"
			:title="drawerTitle"
			placement="right"
			width="900"
			@close="closeDrawer"
			destroyOnClose
		>
			<TaskEdit v-if="drawerVisible" :id="editId" @success="onEditSuccess" />
		</a-drawer>
	</div>
</template>

<script setup>
	import { ref, reactive, onMounted } from 'vue'
	import { message, Modal } from 'ant-design-vue'
	import taskApi from '@/api/exam/paper/task.js'
	import TaskEdit from './form.vue'
	import { useExamStore } from '@/store/exam.js'
	import { storeToRefs } from 'pinia'
	import { parseTime } from '@/utils/exam'
	const examStore = useExamStore()
	const { levelEnum, enumFormat } = storeToRefs(examStore)
	const drawerVisible = ref(false)
	const drawerTitle = ref('')
	const editId = ref(null)

	const queryParam = reactive({
		gradeLevel: null,
		pageIndex: 1,
		pageSize: 10
	})
	const listLoading = ref(false)
	const tableData = ref([])
	const total = ref(0)

	const fetchList = async () => {
		listLoading.value = true
		try {
			const params = {
				...queryParam,
				current: queryParam.pageIndex,
				size: queryParam.pageSize
			}
			delete params.pageIndex
			delete params.pageSize
			const data = await taskApi.pageList(params)
			tableData.value = data.records || []
			total.value = data.total || 0
			queryParam.pageIndex = data.current || 1
		} finally {
			listLoading.value = false
		}
	}

	onMounted(() => {
		fetchList()
	})

	const submitForm = () => {
		queryParam.pageIndex = 1
		fetchList()
	}

	const onPageChange = (page, pageSize) => {
		queryParam.pageIndex = page
		queryParam.pageSize = pageSize
		fetchList()
	}
	const onPageSizeChange = (current, size) => {
		queryParam.pageIndex = 1
		queryParam.pageSize = size
		fetchList()
	}

	const editTask = (record) => {
		drawerVisible.value = true
		drawerTitle.value = '编辑任务'
		editId.value = record.id
	}

	const deleteTask = (record) => {
		Modal.confirm({
			title: '确认删除该任务吗？',
			onOk: async () => {
				try {
					await taskApi.deleteTask(record.id)
					message.success('删除成功')
					fetchList()
				} catch (e) {
					message.error(e.msg || '删除失败')
				}
			}
		})
	}

	const levelFormatter = ({ text }) => {
		return enumFormat.value(levelEnum.value, text)
	}
	const createTask = () => {
		drawerVisible.value = true
		drawerTitle.value = '创建任务'
		editId.value = null
	}
	const onEditSuccess = () => {
		editId.value = null
		drawerVisible.value = false
		fetchList()
	}
	const closeDrawer = () => {
		drawerVisible.value = false
	}
	const formatDateTime = (val) => {
		if (!val) return ''
		return parseTime(val, '{y}-{m}-{d} {h}:{i}:{s}')
	}
</script>

<style lang="less" scoped>
	.task-container {
		background: #fff;
		padding: 24px;
		border-radius: 8px;
	}
</style>
