<template>
	<div class="exam-train-exam-marking-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent :disabled="state.tableLoading">
				<el-form-item label="是否批改" prop="isCheck">
					<el-select v-model="state.queryParams.isCheck" clearable placeholder="请选择是否批改" @change="handleQuery">
						<el-option label="已批改完成" :value="true" />
						<el-option label="未批改完成" :value="false" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.tableLoading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.tableLoading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="examTrainExamMarking"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="examCode" title="考试编号" width="200"></vxe-column>
					<vxe-column field="examName" title="考试标题" width="150"></vxe-column>
					<vxe-column field="totalScore" title="考试总分" width="150"></vxe-column>
					<vxe-column field="cutoffScore" title="合格分数" width="150"></vxe-column>
					<vxe-column field="isCheck" title="是否批改" width="150">
						<template #default="{ row }">
							{{ row.isCheck ? '已批改完成' : '未批改完成' }}
						</template>
					</vxe-column>
					<vxe-column field="remark" title="备注" min-width="200"></vxe-column>
					<vxe-column title="操作" fixed="right" width="80" align="center" :show-overflow="false">
						<template #default="{ row }">
							<el-button link type="primary" @click="onView(row)" title="查看" v-if="row.isCheck"> 查看 </el-button>
							<el-button link type="primary" @click="onMarking(row)" title="阅卷" v-else> 阅卷 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.tableLoading"
			/>
		</div>
	</div>
</template>

<script lang="tsx" setup name="examTrainExamMarking">
import { ref, reactive, onMounted, defineAsyncComponent } from 'vue';
import 'splitpanes/dist/splitpanes.css';
import Other from '@/utils/other';
import { FormInstance } from 'element-plus';
import { getMarkingList } from '@/api/examTrain/marking';
import { useRouter } from 'vue-router';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, //页码
		PageSize: 20, //每页条数
		isCheck: null, // 是否批改
	},
	tableLoading: false, //表格loading
	tableData: [], //表格数据
	total: 0, //总条数
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取试题列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.tableLoading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	getMarkingList(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.pagination.totalCount ?? 0;
			state.tableLoading = false;
		})
		.catch(() => {
			state.tableLoading = false;
		});
};
/** 重置按钮操作 */
const ruleFormRef = ref<RefType>(); // 表单ref
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 阅卷
const router = useRouter(); //路由
const onMarking = (row: any) => {
	router.push({
		name: 'examTrainExamMarkingEdit',
		params: {
			tagsViewName: `阅卷 ${row.examName}`,
			examId: row.id,
		},
	});
};
// 阅卷查看
const onView = (row: any) => {
	router.push({
		name: 'examTrainExamMarkingEdit',
		params: {
			tagsViewName: `阅卷 ${row.examName}`,
			examId: row.id,
			isView: 'true',
		},
	});
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
