<template>
	<div class="telslog-callRecord-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="待审核" name="1"></el-tab-pane>
				<el-tab-pane label="审核通过" name="2"></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 in state.directionList" :key="item.value" :label="item.label" :value="item.value" />
						</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="getList" :loading="state.loading" v-waves> <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-tabs>
		</el-card>
		<el-card shadow="never">
			<div class="flex-center-between mb20">
				<p class="table-title">信息列表</p>
				<div>
					<el-button type="primary" :disabled="!state.multipleSelection.length" @click="onImportTable">
						<SvgIcon name="iconfont icon-daochu" class="mr5" />导出
					</el-button>
				</div>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableList" v-loading="state.loading" row-key="id" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" :reserve-selection="true" />
				<el-table-column prop="knowledgeTypeName" label="审核类型" show-overflow-tooltip>
					<template #default="{ row }">
						<span v-if="row.workflowModuleStatus === 0" style="color: var(--el-color-primary)">发布知识审核</span>
						<span v-if="row.workflowModuleStatus === 1" style="color: var(--el-color-info)">更新知识审核</span>
						<span v-if="row.workflowModuleStatus === 2" style="color: var(--el-color-danger)">删除知识审核</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></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip>
					<template #default="{ row }">
						<span v-if="row.status === 1" style="color: var(--el-color-primary)">待审批</span>
						<span v-if="row.status === 2" style="color: var(--el-color-danger)">审批成功</span>
						<span v-if="row.status === 3" style="color: var(--el-color-success)">已撤销</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="200" fixed="right" align="center">
					<template #default="{row}">
						<el-button text type="primary" @click="onPreview(row)" title="预览" v-if="[1,2].includes(row.status)"> 预览 </el-button>
						<el-button text type="primary" @click="onProcess(row)" title="审核" v-if="[1].includes(row.status)"> 审核 </el-button>
						<el-button text type="info" @click="onRecord(row)" title="查看审核记录" v-if="[1,2].includes(row.status)">查看审核记录 </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="getList" />
		</el-card>
		<!-- 预览 -->
		<Preview ref="previewRef" />
		<!-- 审核记录 -->
		<ProcessRecord ref="processRecordRef" />
		<!-- 流程组件 -->
		<Process ref="processRef" @onSubmitProcess="onSubmitProcess" />
	</div>
</template>

<script lang="ts" setup name="knowledgeProcess">
import { ref, reactive, onMounted, defineAsyncComponent } from 'vue';
import type { FormInstance } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import table2excel from 'js-table2excel';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
// 引入节流
import { throttle,commonEeum } from '/@/utils/tools';
import { KnowledgeApprovedlist } from '/@/api/knowledge';

// 引入组件
const Preview = defineAsyncComponent(() => import('/@/views/knowledge/component/Preview.vue'));
const ProcessRecord = defineAsyncComponent(() => import('/@/views/knowledge/component/ProcessRecord.vue'));
const Process = defineAsyncComponent(() => import('/@/views/knowledge/component/Process.vue'));

// 定义变量内容
const state = reactive(<any>{
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		EKnowledgeWorkFlowStatus: ' ',
	},
	tableList: [],
	loading: false,
	total: 0,
	multipleSelection: [],
	directionList: [
		{
			label: '发布知识审核',
			value: 0,
		},
		{
			label: '更新知识审核',
			value: 1,
		},
		{
			label: '删除知识审核',
			value: 2,
		},
	],
});
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const ruleFormRef = ref<FormInstance>();
const previewRef = ref();
const processRecordRef = ref();
const processRef = ref();
/** 获取审核列表 */
const getList = () => {
	state.queryParams.PageIndex = 1;
	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;
		});
};
// 多选表格
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	getList();
}, 1000);
// 切换tab 查询列表
const handleClick = () => {
	getList();
};
// 预览
const onPreview = (row: any) => {
	previewRef.value.openDialog(row);
};
// 审核
const onProcess = (row: any) => {
	processRef.value.openDialog({ id: row.workflowId, title:'提交', processType: 'next', commonEeum: commonEeum.OrderCirculation });
};
// 发起更新成功
const onSubmitProcess = ()=>{
	getList();
}
// 查看审核记录
const onRecord = (row: any) => {
	processRecordRef.value.openDialog(row);
};
// 导出表格
const onImportTable = () => {
	const tabeHeader = [
		{ key: 'userName', colWidth: '', title: '用户名称', type: 'text', isCheck: true },
		{ key: 'fromNo', colWidth: '', title: '主叫号码', type: 'text', isCheck: true },
		{ key: 'toNo', colWidth: '', title: '被叫号码', type: 'text', isCheck: true },
		{ key: 'callTypeText', colWidth: '', title: '通话类型', type: 'text', isCheck: true },
		{ key: 'callStatusText', colWidth: '', title: '通话状态', type: 'text', isCheck: true },
		{ key: 'callDirectionText', colWidth: '', title: '呼叫方向', type: 'text', isCheck: true },
		{ key: 'endByText', colWidth: '', title: '通话结束方', type: 'text', isCheck: true },
		{ key: 'phoneIspText', colWidth: '', title: '运营商', type: 'text', isCheck: true },
		{ key: 'ringOffTypeText', colWidth: '', title: '挂断类型', type: 'text', isCheck: true },
		{ key: 'attribution', colWidth: '', title: '归属地', type: 'text', isCheck: true },
		// { key: 'creationTime', colWidth: '', title: '创建时间', type: 'text', isCheck: true },
		// { key: 'image', colWidth: '', width: '70', height: '40', title: '图片描述', type: 'image', isCheck: true },
	];
	table2excel(tabeHeader, state.multipleSelection, `${themeConfig.value.globalTitle} ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`);
};
onMounted(() => {
	getList();
});
</script>
<style lang="scss" scoped></style>
