<template>
	<div class="plan-index-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
            <vxe-toolbar
                ref="toolbarRef"
                :loading="state.tableLoading"
                custom
                :refresh="{
                    queryMethod: handleQuery,
                }"
            >
                <template #buttons>
                    <el-button type="primary" @click="onAdd" v-auth="'practice: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="examTrainExamPractice"
                    :custom-config="{ storage: true }"
                    showHeaderOverflow
                >
                    <vxe-column field="code" title="练习流水号" width="200"></vxe-column>
                    <vxe-column field="count" title="出题数量" width="150"></vxe-column>
                    <vxe-column field="practiceTime" title="练习时间" min-width="160">
						<template #default="{ row }">
							{{ formatDate(row.practiceTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
                    <vxe-column title="操作" fixed="right" width="120" align="center" :show-overflow="false">
                        <template #default="{ row }">
                            <el-button link type="primary" @click="onView(row)" title="查看" v-auth="'practice: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>
        <practice-add ref="practiceAddRef" @choose="onOpenExam" />
		<practice-exam ref="practiceExamRef" @updateList="queryList" />
		<practice-view ref="practiceViewRef" />
	</div>
</template>

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

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const PracticeAdd = defineAsyncComponent(() => import('@/views/examTrain/exam/practice/components/Practice-add.vue')); // 新增练习
const PracticeExam = defineAsyncComponent(() => import('@/views/examTrain/exam/practice/components/Practice-exam.vue')); // 练习
const PracticeView = defineAsyncComponent(() => import('@/views/examTrain/exam/practice/components/Practice-view.vue')); // 查看

const router = useRouter(); //路由

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, //页码
		PageSize: 20, //每页条数
	},
	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);
	getPracticeData(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 practiceAddRef = ref<RefType>(); // 新增ref
const onAdd = () => {
	practiceAddRef.value.openDialog(state.tableData.value);
};
const practiceExamRef = ref<RefType>(); // 练习ref
const onOpenExam = (practiceId) => {
    console.log(practiceId);
	practiceExamRef.value.openDialog(practiceId);
}
// 打开查看弹窗
const practiceViewRef = ref<RefType>(); // 修改ref
const onView = (row: any) => {
	practiceViewRef.value.openDialog(row);
};
// 表格选中状态
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
