<template>
	<div class="knowledge-process-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="state.queryParams.EKnowledgeWorkFlowStatus" @tab-change="handleClick">
				<el-tab-pane label="全部" name=" "></el-tab-pane>
				<el-tab-pane :label="v.value" :name="v.key" v-for="(v, i) in state.statusOptions" :key="i"></el-tab-pane>
				<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
					<el-form-item label="审核类型" prop="EKnowledgeApplyType">
						<el-select v-model="state.queryParams.EKnowledgeApplyType" placeholder="请选择审核类型" class="w100">
							<el-option v-for="(item, index) in state.eKnowledgeApplyType" :key="index" :label="item.value" :value="item.key" />
						</el-select>
					</el-form-item>
					<el-form-item label="关键字" prop="Keyword">
						<el-input v-model="state.queryParams.Keyword" placeholder="标题/创建人/创建部门" clearable style="width: 300px"> </el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="queryList" :loading="state.loading" v-waves> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
						<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button" :loading="state.loading"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
					</el-form-item>
				</el-form>
			</el-tabs>
		</el-card>
		<el-card shadow="never">
			<div class="mb20">
				
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableList" v-loading="state.loading" row-key="id">
				<el-table-column prop="knowledgeTypeName" label="审核类型" show-overflow-tooltip>
					<template #default="{ row }">
						<span v-if="row.workflowModuleStatus === 0" class="color-primary">{{ row.workflowModuleStatusName }}</span>
						<span v-if="row.workflowModuleStatus === 2" class="color-danger">{{ row.workflowModuleStatusName }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="title" label="标题" show-overflow-tooltip></el-table-column>
				<el-table-column prop="knowledgeTypeName" label="类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationName" label="创建人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createBMName" label="创建部门" show-overflow-tooltip width="170"></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip>
					<template #default="{ row }">
						<span v-if="row.workFlowApplyStatus === 1" class="color-primary">{{ row.workFlowApplyStatusName }}</span>
						<span v-if="row.workFlowApplyStatus === 2" class="color-success">{{ row.workFlowApplyStatusName }}</span>
						<span v-if="row.workFlowApplyStatus === 3" class="color-danger">{{ row.workFlowApplyStatusName }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="toNo" 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="toNo" label="到达该节点时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.currentStepTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="toNo" label="审核时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.handleTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="160" fixed="right" align="center">
					<template #default="{ row }">
						<el-button
							link
							type="primary"
							@click="onPreview(row)"
							title="预览"
							v-auth="'knowledge:preview'"
							v-if="[1, 2, 3].includes(row.workFlowApplyStatus)"
						>
							预览
						</el-button>
						<el-button
							link
							type="primary"
							@click="onProcess(row)"
							title="审核"
							v-auth="'system:workflow:handle'"
							v-if="[1].includes(row.workFlowApplyStatus) && row.canHandle"
						>
							审核
						</el-button>
						<el-button
							link
							type="info"
							@click="onRecord(row)"
							title="查看审核记录"
							v-auth="'system:workflow:record'"
							v-if="[2, 3].includes(row.workFlowApplyStatus)"
							>查看审核记录
						</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>
		<!-- 审核记录 -->
    <audit-record ref="auditRecordRef" />
		<!-- 流程组件 -->
		<knowledge-process ref="knowledgeProcessRef" @knowledgeProcessSuccess="knowledgeProcessSuccess" />
	</div>
</template>

<script lang="ts" setup name="knowledgeProcess">
import { ref, reactive, onMounted, defineAsyncComponent } from 'vue';
import { ElMessage } from 'element-plus';
import type { FormInstance } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import { useRouter } from 'vue-router';
import { formatDate } from '/@/utils/formatTime';
import { throttle } from '/@/utils/tools';
import {commonEnum} from '/@/utils/constants';
import { KnowledgeApprovedList, approvedBaseData } from '/@/api/knowledge';

// 引入组件
const KnowledgeProcess = defineAsyncComponent(() => import('/@/views/knowledge/component/Knowledge-process.vue'));  // 流程组件
const AuditRecord = defineAsyncComponent(() => import('/@/components/AuditRecord/index.vue'));  // 审核记录组件

// 定义变量内容
const state = reactive(<any>{
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页显示条数
		EKnowledgeWorkFlowStatus: ' ',  // 审核状态
		EKnowledgeApplyType: '',  // 审核类型
	},
	tableList: [],  // 表格数据
	loading: false, // 加载状态
	total: 0, // 总条数
	eKnowledgeApplyType: [],  // 审核类型数据
	statusOptions: [], // 审核状态数据
});
const ruleFormRef = ref<FormInstance>();  // 表单ref
const router = useRouter(); // 路由
/** 获取审核列表 */
const queryList = throttle(() => {
	if (!auth('knowledge:process:query')) ElMessage.error('抱歉，您没有权限查询知识审核列表');
	else {
		state.loading = true;
		KnowledgeApprovedList(state.queryParams)
			.then((response: any) => {
				state.tableList = response.result.items ?? [];
				state.total = response?.result.total;
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
}, 500);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 500);
// 切换tab 查询列表
const handleClick = () => {
	queryList();
};
// 预览
const onPreview = (row: any) => {
	router.push({
		name: 'knowledgePreview',
		params: {
			id: row.id,
			tagsViewName: '知识预览',
		},
	});
};
// 审核
const knowledgeProcessRef = ref<RefType>(); // 流程组件ref
const onProcess = (row: any) => {
  knowledgeProcessRef.value.openDialog({ id: row.workflowId, title: '提交', processType: 'next', commonEnum: commonEnum.KnowledgeLocution });
};
// 发起更新成功
const knowledgeProcessSuccess = () => {
	queryList();
};
// 查看审核记录
const auditRecordRef = ref<RefType>();  // 审核记录ref
const onRecord = (row: any) => {
  const params = {
    title: `审核记录 （${row.title}）`,
    ...row,
  };
  auditRecordRef.value.openDialog(params);
};
// 页面基础数据
const getBaseData = async () => {
	try {
		const { result } = await approvedBaseData();
		state.statusOptions = result.eKnowledgeWorkFlowStatus;
		state.eKnowledgeApplyType = result.eKnowledgeApplyType;
	} catch (error) {
		console.log(error);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
<style lang="scss" scoped></style>
