<template>
	<div class="knowledge-error-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="纠错人" prop="CreatorName">
					<el-input v-model="state.queryParams.CreatorName" placeholder="请输入纠错人" clearable @keyup.enter="queryList" class="keyword-input" />
				</el-form-item>
				<el-form-item label="知识分类" prop="KnowledgeTypeId">
					<el-cascader
						:options="state.typeData"
						filterable
						:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false, children: 'children' }"
						placeholder="请选择知识分类"
						v-model="state.queryParams.KnowledgeTypeId"
						ref="cascadeRef"
					>
					</el-cascader>
				</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)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading">
        <el-table-column label="知识标题" show-overflow-tooltip width="130">
          <template #default="{ row }">
            <el-button link type="primary" @click="onPreview(row)">{{ row.knowledge?.title }}</el-button>
          </template>
        </el-table-column>
				<el-table-column prop="knowledgeTypeText" label="知识分类" show-overflow-tooltip width="120"></el-table-column>
				<el-table-column prop="knowledge.statusText" label="知识状态" show-overflow-tooltip></el-table-column>
				<el-table-column prop="stateText" label="答复状态" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creatorName" label="纠错人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationTime" label="纠错时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="content" label="纠错内容" show-overflow-tooltip></el-table-column>
				<el-table-column prop="replyContent" label="答复内容" show-overflow-tooltip></el-table-column>
				<el-table-column label="答复时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.replyTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="200" fixed="right" align="center">
					<template #default="{ row }">
						<el-button link type="primary" @click="onPreview(row)" title="知识详情"> 知识详情 </el-button>
						<el-button link type="primary" @click="onReply(row)" v-auth="'knowledge:error:reply'" title="答复" v-if="[0].includes(row.state)">
							答复
						</el-button>
						<el-button
							link
							type="primary"
							@click="onEdit(row)"
							title="编辑"
							v-if="[0, 4].includes(row.knowledge?.status)"
							v-auth="'knowledge:error:edit'"
						>
							编辑
						</el-button>
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.PageIndex"
				v-model:limit="state.queryParams.PageSize"
				@pagination="queryList"
			/>
		</el-card>
		<!-- 知识纠错 -->
		<error-add ref="errorAddRef" @submitSuccess="queryList" />
	</div>
</template>

<script lang="ts" setup name="knowledgeError">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { auth } from '@/utils/authFunction';
import { correctionList } from '@/api/knowledge/error';
import { useRouter } from 'vue-router';
import { treeList } from '@/api/knowledge/type';

// 引入组件
const ErrorAdd = defineAsyncComponent(() => import('@/views/knowledge/error/components/Error-add.vue')); // 知识纠错

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		KnowledgeTypeId: null, // 知识分类
		CreatorName: null, // 提问人
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
	typeData: [], // 知识分类
});
const ruleFormRef = ref<RefType>(null); // 表单ref
const getBaseData = async () => {
	try {
		const typeDataRes = await treeList({ IsEnable: true });
		state.typeData = typeDataRes.result ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	if (!auth('knowledge:error:query')) ElMessage.error('抱歉，您没有权限获取知识纠错列表！');
	else {
		correctionList(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.knowledge.id,
			tagsViewName: '知识详情',
		},
	});
};
// 答复
const errorAddRef = ref<RefType>();
const onReply = (row: any) => {
	errorAddRef.value.openDialog(row, true);
};
// 编辑
const onEdit = (row: any) => {
	router.push({
		name: 'knowledgeEdit',
		params: {
			id: row.knowledge.id,
			tagsViewName: '编辑知识',
		},
	});
};
// 页面加载时
onMounted(() => {
	getBaseData();
	queryList();
});
</script>
