<template>
	<div class="snapshot-statistics-grid-handle-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<el-form-item prop="crTime">
							<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading" />
						</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>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</div>
	</div>
</template>

<script lang="tsx" setup name="examTrainExamStatisticsMissExamUsers">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import { getMissExamUsersData, getMissExamUsersExport } from '@/api/examTrain/statistics';
import Other from '@/utils/other';

// 引入组件
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	loading: false,
	total: 0, // 总数
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		crTime: defaultDate, // 时间默认今天开始到今天结束
		startTime: null,
		endTime: null,
		anlysisType: 3,
	},
});

const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		tools: [{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }],
	},
	params: {
		exportMethod: getMissExamUsersExport,
		exportParams: requestParams,
	},
	customConfig: {
		storage: true,
	},
	id: 'examTrainExamStatisticsMissExamUsers',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	align: 'center',
	columns: [
		{
			field: 'userName',
			title: '缺考人员',
			width: 200,
			fixed: 'left',
		},
		{
			field: 'orgName',
			title: '部门名称',
			fixed: 'left',
			width: 300,
		},
        {
			field: 'examName',
			title: '考试题目',
			fixed: 'left',
			minWidth: 300,
		},
	],
	data: [],
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.startTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0]; // 受理时间
	requestParams.value.endTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime'); // 删除无用的参数
	getMissExamUsersData(requestParams.value)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result.items ?? [];
			state.total = res?.result.pagination.totalCount ?? 0;
			gridOptions.loading = false;
		})
		.finally(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
// 重置表单
const ruleFormRef = ref<any>(null); // 表单ref
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>
