<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>
                    <el-button type="primary" @click="handleQuery" :loading="state.tableLoading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				    <el-button @click="resetQuery(ruleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" 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="examTrainTrainUserTrain"
                    :custom-config="{ storage: true }"
                    showHeaderOverflow
                >
                    <vxe-column field="trainCode" title="培训编号" width="150"></vxe-column>
                    <vxe-column field="trainName" title="培训标题" min-width="150"></vxe-column>
                    <vxe-column field="trainStartTime" title="培训开始时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.trainStartTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
                    <vxe-column field="trainEndTime" title="培训结束时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.trainEndTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
                    <vxe-column field="isCompleteDes" title="是否完成培训" align="center" width="120"></vxe-column>
                    <!-- <vxe-column field="" title="创建人" width="100"></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 title="操作" fixed="right" width="160" align="center" :show-overflow="false">
                        <template #default="{ row }">
                            <el-button type="primary" v-if="!row.isComplete" @click="onStudy(row)" size="small" title="学习" v-auth="'userTrain:index:study'">
                                学习
                            </el-button>
                            <el-button link type="primary" v-else @click="onView(row)" title="查看小测" v-auth="'userTrain: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>

        <!-- 查看小测 -->
		<userTrain-view ref="userTrainViewRef" @updateList="queryList" />
	</div>
</template>

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

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const UserTrainView = defineAsyncComponent(() => import('@/views/examTrain/train/userTrain/components/UserTrain-view.vue')); // 查看组件

const router = useRouter(); //路由

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, //页码
		PageSize: 20, //每页条数
        code: '', // 编号
        name: '', // 标题
	},
	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);
	getUserTrainData(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 ruleFormRef = ref<RefType>(); // 表单ref
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	handleQuery();
};
// 学习
const onStudy  = (row: any) => {
    console.log(row)
	router.push({
		name: 'trainStudy',
		params: {
			id: row.id,
            trainName: row.trainName,
			tagsViewName: '培训学习',
		},
	});
};
// 打开查看弹窗
const userTrainViewRef = ref<RefType>(); // 修改ref
const onView = (row: any) => {
	userTrainViewRef.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>
