<template>
	<div class="plan-index-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="examType">
                    <el-select v-model="state.queryParams.examType" clearable placeholder="考试类型" @change="handleQuery" :disabled="state.tableLoading">
                        <el-option key='0' label="正式考试" :value="0" />
                        <el-option key='1' label="模拟考试" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="考核方式" prop="mode">
                    <el-select v-model="state.queryParams.mode" clearable placeholder="考核方式" @change="handleQuery" :disabled="state.tableLoading">
                        <el-option key='0' label="统一试卷" :value="0" />
                        <el-option key='1' label="随机抽题" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="考试编号" prop="code">
                    <el-input v-model="state.queryParams.code" placeholder="考试编号" clearable @keyup.enter="handleQuery" class="keyword-input" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleQuery" :loading="state.tableLoading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
                    <el-button @click="drawer = true" class="default-button"> <SvgIcon name="ele-Search" class="mr5" />更多查询</el-button>
                </el-form-item>
            </el-form>
            <vxe-toolbar
                ref="toolbarRef"
                :loading="state.tableLoading"
                custom
                :refresh="{
                    queryMethod: handleQuery,
                }"
            >
                <template #buttons>
                    <el-button type="primary" @click="onAdd" v-auth="'examManage:index:add'" :loading="state.tableLoading">
                        <SvgIcon name="ele-Plus" class="mr10" />新增
                    </el-button>
                </template>
            </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="examManage"
                    :custom-config="{ storage: true }"
                    showHeaderOverflow
                >
                    <vxe-column field="code" title="考试编号" width="150"></vxe-column>
                    <vxe-column field="examTypeDes" title="考试类型" width="100"></vxe-column>
                    <vxe-column field="modeDes" title="考核方式" width="100"></vxe-column>
                    <vxe-column field="name" title="考试标题" min-width="150"></vxe-column>
                    <vxe-column field="totalScore" title="考试总分" width="100"></vxe-column>
                    <vxe-column field="cutoffScore" title="合格分数" width="100"></vxe-column>
                    <vxe-column field="count" title="可考次数" width="100">
                        <template #default="{ row }">
                            {{ row.examType == 0 ? row.count : '' }}
						</template>
                    </vxe-column>
                    <vxe-column field="startTime" title="可考开始时间" width="160">
						<template #default="{ row }">
                            <span v-if="row.examType == 0">{{formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS')}}</span>
                            <span v-else></span>
						</template>
					</vxe-column>
                    <vxe-column field="endTime" title="可考结束时间" width="160">
						<template #default="{ row }">
                            <span v-if="row.examType == 0">{{formatDate(row.endTime, 'YYYY-mm-dd HH:MM:SS')}}</span>
                            <span v-else></span>
						</template>
					</vxe-column>
                    <vxe-column field="timeSpan" title="考试时长" width="100">
                        <template #default="{ row }">
                            {{ row.examType == 0 ? row.timeSpan : '' }}
						</template>
                    </vxe-column>
                    <vxe-column field="examStatus" title="考试状态" width="100">
                        <template #default="{ row }">
                            <span v-if="row.examStatus == 0">未开始</span>
                            <span v-else-if="row.examStatus == 1">考试中</span>
                            <span v-else-if="row.examStatus == 2">考试结束</span>
                        </template>
                    </vxe-column>
                    <vxe-column field="status" title="状态" width="160">
                        <template #default="{ row }">
                            <el-switch v-model="row.status" />
                        </template>
                    </vxe-column>
                    <vxe-column title="操作" fixed="right" width="120" align="center" :show-overflow="false">
                        <template #default="{ row }">
                            <el-button link type="primary" @click="onEdit(row)" title="编辑" v-auth="'examManage:index:edit'">
                                编辑
                            </el-button>
                            <el-button link type="danger" @click="onRowDel(row)" title="删除" v-auth="'examManage:index:delete'">
                                删除
                            </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>
        <!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px">
				<el-form-item label="考试标题" prop="name">
                    <el-input v-model="state.queryParams.name" placeholder="考试标题" clearable @keyup.enter="handleQuery" class="keyword-input" />
                </el-form-item>
			</el-form>
			<template #footer>
				<el-button type="primary" @click="handleQuery" :loading="state.tableLoading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(drawerRuleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</template>
		</el-drawer>
	</div>
</template>

<script lang="tsx" setup name="examManage">
import { ref, reactive, onMounted, defineAsyncComponent, computed } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import type { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import 'splitpanes/dist/splitpanes.css';
import Other from '@/utils/other';
import { downloadFileByStream } from '@/utils/tools';
import {deleteExamManage, getExamManageData} from '@/api/examTrain/examManage';

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

const router = useRouter(); //路由

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, //页码
		PageSize: 20, //每页条数
        code: '', // 编号
        name: '', // 标题
		examType: null, // 考试类型
		mode: 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);
	getExamManageData(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.pagination.totalCount;
			state.tableLoading = false;
		})
		.catch(() => {
			state.tableLoading = false;
		});
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	handleQuery();
};
// 新增
const onAdd = () => {
	router.push({
		name: 'examManageEdit',
		params: {
			tagsViewName: '新增考试',
		},
	});
};
// 编辑
const onEdit  = (row: any) => {
	router.push({
		name: 'examManageEdit',
		params: {
			id: row.id,
			tagsViewName: '编辑考试',
		},
	});
};
// 删除当前行
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`是否确认删除当前试卷？删除后不可恢复`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
        .then(() => {
			deleteExamManage({id: row.id}).then(() => {
				ElMessage.success('删除成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 表格选中状态
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
