<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="name">
                    <el-input v-model="state.queryParams.name" placeholder="考试标题" clearable 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 label="开始时间>=" prop="minStartTime">
                    <el-date-picker v-model="state.queryParams.minStartTime" type="datetime" class="w100" value-format="YYYY-MM-DD[T]HH:mm:ss"  placeholder="请选择" popper-class="no-atTheMoment" />
                </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,
                }"
            ></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="examTrainExamUserExam"
                    :custom-config="{ storage: true }"
                    showHeaderOverflow
                >
                    <vxe-column field="examName" title="考试标题" min-width="150"></vxe-column>
                    <vxe-column field="examTypeDesc" title="考试类型" width="100"></vxe-column>
                    <vxe-column field="startTime" title="可考开始时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
                    <vxe-column field="endTime" title="可考结束时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.endTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
                    <vxe-column field="timeSpan" title="考试时长" width="100"></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="totalScore" title="考试总分" width="100"></vxe-column>
                    <vxe-column field="cutoffScore" title="合格分数" width="100"></vxe-column>
                    <vxe-column field="score" title="考试得分" width="100">
                        <template #default="{ row }">
                            <span v-if="row.isSuccess != null">{{ row.score }}</span>
                        </template>
                    </vxe-column>
                    <vxe-column field="isSuccess" title="是否合格" width="100">
                        <template #default="{ row }">
                            <span v-if="row.isSuccess">合格</span>
                            <span v-else-if="row.isSuccess == false">不合格</span>
                        </template>
                    </vxe-column>
                    <vxe-column title="操作" fixed="right" width="150" align="center" :show-overflow="false">
                        <template #default="{ row }">
                            <el-button type="primary" v-if="row.examStatus != 2" @click="onExam(row)" size="small" title="考试" v-auth="'userExam:index:exam'">
                                考试
                            </el-button>
                            <!-- <el-button link type="primary" @click="onView(row)" title="查看" v-auth="'userExam:index:view'">
                                查看
                            </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="maxStartTime">
                    <el-date-picker v-model="state.queryParams.maxStartTime" type="datetime" class="w100" value-format="YYYY-MM-DD[T]HH:mm:ss"  placeholder="请选择" popper-class="no-atTheMoment" />
                </el-form-item>
                <el-form-item label="结束时间>=" prop="minEndTime">
                    <el-date-picker v-model="state.queryParams.minEndTime" type="datetime" class="w100" value-format="YYYY-MM-DD[T]HH:mm:ss"  placeholder="请选择" popper-class="no-atTheMoment" />
                </el-form-item>
                <el-form-item label="结束时间<=" prop="maxEndTime">
                    <el-date-picker v-model="state.queryParams.maxEndTime" type="datetime" class="w100" value-format="YYYY-MM-DD[T]HH:mm:ss"  placeholder="请选择" popper-class="no-atTheMoment" />
                </el-form-item>
                <el-form-item label="考试总分>=" prop="minTotalScore">
                    <el-input type="number" v-model="state.queryParams.minTotalScore" placeholder="请填写" clearable></el-input>
                </el-form-item>
                <el-form-item label="考试总分<=" prop="maxTotalScore">
                    <el-input type="number" v-model="state.queryParams.maxTotalScore" placeholder="请填写" clearable></el-input>
                </el-form-item>
                <el-form-item label="考试得分>=" prop="minScore">
                    <el-input type="number" v-model="state.queryParams.minScore" placeholder="请填写" clearable></el-input>
                </el-form-item>
                <el-form-item label="考试得分<=" prop="maxScore">
                    <el-input type="number" v-model="state.queryParams.maxScore" placeholder="请填写" clearable></el-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>

        <userExam-exam ref="userExamExamRef" @updateList="queryList" />
		<userExam-view ref="userExamViewRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="examTrainExamUserExam">
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 {getUserExamData} from '@/api/examTrain/userExam';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const UserExamExam = defineAsyncComponent(() => import('@/views/examTrain/exam/userExam/components/UserExam-exam.vue')); // 考试组件
const UserExamView = defineAsyncComponent(() => import('@/views/examTrain/exam/userExam/components/UserExam-view.vue')); // 查看组件

const router = useRouter(); //路由

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, //页码
		PageSize: 20, //每页条数
        name: '', // 标题
		examType: null, // 考试类型
        minStartTime: null, // 最小开始时间
        maxStartTime: null, // 最大开始时间
        minEndTime: null, // 最小结束时间
        maxEndTime: null, // 最大结束时间
        minTotalScore: null, // 最小总分
        maxTotalScore: null, // 最大总分
        minScore: null, // 最小分数
        maxScore: 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);
	getUserExamData(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 userExamExamRef = ref<RefType>(); // 新增ref
const onExam = (row: any) => {
	userExamExamRef.value.openDialog(row, state.tableData.value);
};
// 打开查看弹窗
const userExamViewRef = ref<RefType>(); // 修改ref
const onView = (row: any) => {
	userExamViewRef.value.openDialog(row, state.tableData.value);
};
// 表格选中状态
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
