<template>
	<div class="knowledge-question-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<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="handleQuery" 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"
						@change="handleQuery"
					>
					</el-cascader>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :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>
			<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
					:scrollY="{ enabled: true, gt: 100 }"
					id="knowledgeQuestion"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="knowledge.title" title="标题" min-width="200">
						<template #default="{ row }">
							<el-button link type="primary" @click="onPreview(row)">{{ row.knowledge?.title }}</el-button>
						</template>
					</vxe-column>
					<vxe-column field="knowledgeType" title="知识分类" width="150"></vxe-column>
					<vxe-column field="knowledge.statusText" title="知识状态" width="100"></vxe-column>
					<vxe-column field="stateText" title="答复状态" width="120"></vxe-column>
					<vxe-column field="creatorName" title="提问人" width="120"></vxe-column>
					<vxe-column field="creationTime" title="提问时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="content" title="提问内容" width="150"></vxe-column>
					<vxe-column field="replyContent" title="答复内容" width="150"></vxe-column>
					<vxe-column field="replyTime" title="答复时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.replyTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="180" align="center" :show-overflow="false">
						<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:question: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)"> 编辑 </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>
		<!-- 知识提问 -->
		<question-add ref="questionRef" @submitSuccess="queryList" />
	</div>
</template>

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

// 引入组件
const QuestionAdd = defineAsyncComponent(() => import('@/views/knowledge/question/components/Question-add.vue')); // 知识提问
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 2,
		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 handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	questionList(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 questionRef = ref<RefType>();
const onReply = (row: any) => {
	questionRef.value.openDialog(row, true);
};
// 知识详情
const router = useRouter();
const onPreview = (row: any) => {
	router.push({
		name: 'knowledgePreview',
		params: {
			id: row.knowledge.id,
			tagsViewName: row.knowledge.title,
		},
	});
};
// 编辑
const onEdit = (row: any) => {
	router.push({
		name: 'knowledgeEdit',
		params: {
			id: row.knowledge.id,
			tagsViewName: '编辑知识',
		},
	});
};
// 页面加载时
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
