<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="code">
                    <el-input v-model="state.queryParams.code" placeholder="试卷编号" clearable @keyup.enter="handleQuery" class="keyword-input" />
                </el-form-item>
                <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-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>
                    <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="'testPaper: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="examTrainExamTestPaper"
                    :custom-config="{ storage: true }"
                    showHeaderOverflow
                >
                    <vxe-column field="code" title="试卷编号" width="150"></vxe-column>
                    <vxe-column field="name" title="试卷标题" width="200"></vxe-column>
                    <vxe-column field="examTypeDes" title="试卷类型" width="100"></vxe-column>
                    <vxe-column field="modeDes" title="组卷方式" width="100"></vxe-column>
                    <!-- <vxe-column field="status" title="状态" width="160">
                        <template #default="{ row }">
                            <el-switch v-model="row.status" />
                        </template>
                    </vxe-column> -->
                    <vxe-column field="remark" title="备注" min-width="150"></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="'testPaper:index:edit'">
                                编辑
                            </el-button>
                            <el-button link type="danger" @click="onRowDel(row)" title="删除" v-auth="'testPaper: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="examMode">
                    <el-select v-model="state.queryParams.examMode" 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="status">
                    <el-select v-model="state.queryParams.status" 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>
			<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="examTrainExamTestPaper">
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 {deleteTestPaper, getTestPaperData} from '@/api/examTrain/testPaper';

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

const router = useRouter(); //路由

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, //页码
		PageSize: 20, //每页条数
        code: null, // 编号
        name: null, // 标题
		examType: null, // 试卷类型
		examMode: null, // 组卷方式
		status: null, // 状态
	},
	tableData: [], //表格数据
	total: 0, //总条数
	tableLoading: false, //表格loading
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取试题列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.tableLoading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	getTestPaperData(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 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: 'testPaperEdit',
		params: {
			tagsViewName: '新增试卷',
		},
	});
};
// 编辑
const onEdit  = (row: any) => {
	router.push({
		name: 'testPaperEdit',
		params: {
			id: row.id,
			tagsViewName: '编辑试卷',
		},
	});
};
// 删除当前行
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`是否确认删除当前试卷？删除后不可恢复`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
        .then(() => {
			deleteTestPaper({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>
