<template>
	<div class="knowledge-collect-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="关键词" prop="Keyword">
					<el-input v-model="state.queryParams.Keyword" placeholder="关键词" clearable @keyup.enter="queryList" class="keyword-input" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :loading="state.loading"> <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>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			>
				<template #title="{ row }">
					<el-button link type="primary" @click="onPreview(row)">{{ row.knowledge?.title }}</el-button>
				</template>
				<template #knowledgeType="{ row }">
					<span>{{ row.knowledge?.knowledgeType?.map((item) => item.value).join(',') }}</span>
				</template>
				<template #operation="{ row }">
					<el-button link type="primary" @click="onPreview(row)" title="知识详情"> 知识详情 </el-button>
				</template>
			</ProTable>
		</el-card>
	</div>
</template>

<script lang="tsx" setup name="knowledgeCollect">
import { onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { collectList } from '@/api/knowledge/collect';

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'knowledge.title', label: '知识标题', width: 300 },
	{ prop: 'knowledgeType', label: '知识分类', width: 200 },
	{ prop: 'knowledge.statusText', label: '知识状态' },
	{ prop: 'score', label: '知识评分' },
	{ prop: 'creatorName', label: '收藏人' },
	{
		prop: 'creationTime',
		label: '收藏时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 170, align: 'center' },
]);
// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 知识分类
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
	typeData: [], // 知识分类
});
const ruleFormRef = ref<RefType>(null); // 表单ref
// 获取参数列表
const queryList = () => {
	state.loading = true;
	collectList(state.queryParams)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 知识详情
const router = useRouter();
const onPreview = (row: any) => {
	router.push({
		name: 'knowledgePreview',
		params: {
			id: row.knowledgeId,
			tagsViewName: '知识详情',
		},
	});
};

// 页面加载时
onMounted(() => {
	queryList();
});
</script>
