<template>
	<div class="knowledge-delApply-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="Keyword">
						<el-input
							v-model="state.queryParams.Keyword"
							placeholder="标题/创建人/创建部门/申请部门"
							@keyup.enter="queryList"
							clearable
							style="width: 300px"
						>
						</el-input>
					</el-form-item>
					<el-form-item label="申请时间" prop="crTime">
						<el-date-picker
							v-model="state.queryParams.crTime"
							type="datetimerange"
							unlink-panels
							range-separator="至"
							start-placeholder="开始时间"
							end-placeholder="结束时间"
							:shortcuts="shortcuts"
							@change="timeChange"
							value-format="YYYY-MM-DD[T]HH:mm:ss"
						/>
						<!-- value-format="YYYY-MM-DD" -->
					</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">
			<!-- 表格 -->
			<el-table :data="state.tableList" v-loading="state.loading" row-key="id">
				<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="hotspotName" label="热点" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationName" label="创建人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationBMName" label="创建部门" show-overflow-tooltip></el-table-column>
				<el-table-column prop="pageView" label="阅读次数" show-overflow-tooltip></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-info">{{ row.workFlowApplyStatusName }}</span>
						<span v-if="row.workFlowApplyStatus === 4" 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 label="操作" width="190" fixed="right" align="center">
					<template #default="{ row }">
						<el-button
							link
							type="primary"
							@click="onRecord(row)"
							v-auth="'system:workflow:record'"
							title="查看审核记录"
							v-if="[1, 2, 3, 4].includes(row.workFlowApplyStatus)"
						>
							查看审核记录
						</el-button>
						<el-button
							link
							type="danger"
							@click="onRecall(row)"
							title="撤回申请"
							v-auth="'system:workflow:stop'"
							v-if="[1].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" />
	</div>
</template>

<script lang="ts" setup name="knowledgeDelApply">
import { ref, reactive, onMounted, defineAsyncComponent } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import other from '/@/utils/other';
import { formatDate } from '/@/utils/formatTime';
import { throttle } from '/@/utils/tools';
import { shortcuts } from '/@/utils/constants';
import { KnowledgeDeleteList, deleteBase } from '/@/api/knowledge';
import { workflowTerminate } from '/@/api/system/workflow';

// 引入组件
const AuditRecord = defineAsyncComponent(() => import('/@/components/AuditRecord/index.vue'));  // 审核记录

// 定义变量内容
const state = reactive(<any>{
	queryParams: {
		PageIndex: 1, // 页码
		PageSize: 10, // 每页条数
		EKnowledgeWorkFlowStatus: ' ', // 状态
		StartApplyTime: '', // 开始时间
		EndApplyTime: '', // 结束时间
	},
	tableList: [], // 表格数据
	loading: false, // 加载状态
	total: 0, // 总条数
	statusOptions: [], // 状态下拉
});
const ruleFormRef = ref<FormInstance>();  // 表单ref
// 创建时间
const timeChange = (val: string[]) => {
	state.queryParams.StartApplyTime = val[0];
	state.queryParams.EndApplyTime = val[1];
};
/** 获取知识库删除列表 */
const queryList = throttle(() => {
	if (!auth('knowledge:delApply:query')) ElMessage.error('抱歉，您没有权限获取删除申请列表');
	else {
		state.loading = true;
		let request = other.deepClone(state.queryParams);
		Reflect.deleteProperty(request, 'crTime'); // 删除无用的参数
		KnowledgeDeleteList(request)
			.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();
	state.queryParams.StartApplyTime = '';
	state.queryParams.EndApplyTime = '';
	queryList();
}, 500);
// 切换tab 查询列表
const handleClick = () => {
	queryList();
};
// 查看申请记录
const auditRecordRef = ref<RefType>();  // 审核记录ref
const onRecord = (row: any) => {
	const params = {
		title: `审核记录 （${row.title}）`,
		...row,
	};
	auditRecordRef.value.openDialog(params);
};
// 撤回
const onRecall = (row: any) => {
	ElMessageBox.prompt('撤回原因', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		inputPlaceholder: '请输入撤回原因，不超过50字符',
		draggable: true,
		inputErrorMessage: '请输入撤回原因',
		inputType: 'textarea',
		inputValidator: (value) => {
			if (value) return true;
			else return '请输入撤回原因';
		},
	})
		.then(({ value }) => {
			let req = {
				workflowId: row.workflowId,
				opinion: value,
			};
			ElMessageBox.confirm(`是否确定要撤回申请？`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				draggable: true,
				cancelButtonClass: 'default-button',
				autofocus: false,
			})
				.then(() => {
					workflowTerminate(req)
						.then(() => {
							ElMessage.success('操作成功');
							queryList();
						})
						.catch(() => {});
				})
				.catch(() => {});
		})
		.catch(() => {});
};
// 页面基础数据
const getBaseData = async () => {
	try {
		const { result } = await deleteBase();
		state.statusOptions = result;
	} catch (error) {
		console.log(error);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
<style lang="scss" scoped></style>
