<!--流程的流转记录-->
<template>
	<div class="tels-restApply-precess-record">
		<el-dialog :title="state.dialogTitle" v-model="state.dialogVisible" width="90%" draggable destroy-on-close append-to-body>
			<slot name="header"></slot>
			<slot>
				<vxe-grid v-bind="gridOptions">
					<template #expand_content="{ row }">
						<div class="mb5 formatted-text">审核意见：{{ row.opinion }}</div>
						<annex-list name="附件" readonly businessId="" classify="查看附件" v-model="row.files" />
					</template>
					<template #action="{ row }">
						<el-button link type="primary" @click="onCountersign(row)">查看会签</el-button>
					</template>
				</vxe-grid>
			</slot>
			<slot name="footer"></slot>
			<template #footer>
				<el-button @click="closeDialog" class="default-button">关闭</el-button>
			</template>
		</el-dialog>
		<el-dialog title="查看会签" v-model="countersignDialog" append-to-body draggable  destroy-on-close>
			<el-form>
				<el-row>
					<p class="border-title">发起会签信息</p>
					<el-row class="pd15 w100">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="发起人：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="发起部门：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="发起时间：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="会签状态：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col>
							<el-form-item label="发起意见：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
					</el-row>
					<el-divider />
					<el-col>
						<p class="border-title mt10 mb20">会签部门信息</p>
					</el-col>
					<el-row style="border: 1px solid #ccc" class="w100 pd10">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="办理部门：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="期满时间：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="办理时间：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="办理人：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
						<el-col>
							<el-form-item label="办理意见：">{{ currentRow.createUserName }}</el-form-item>
						</el-col>
					</el-row>
				</el-row>
			</el-form>
			<template #footer>
				<el-button @click="closeDialogCountersign" class="default-button">关闭</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="auditRecord">
import { reactive, defineAsyncComponent, ref } from 'vue';
import { workflowTraces } from '@/api/system/workflow';
import { transformFile } from '@/utils/tools';

const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue')); // 附件列表

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	traces: [],
	loading: false,
	dialogTitle: '流程明细',
});

const currentRow = ref();
const gridOptions = reactive<any>({
	border: true,
	loading: false,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	rowConfig: { isHover: true, height: 30, useKey: true },
	expandConfig: { padding: true },
	columns: [
		{ type: 'seq', width: 50 },
		{ type: 'expand', width: 50, slots: { content: 'expand_content' } },
		{ field: 'handleTime', title: '建立时间', width: 160, formatter: 'formatDate' },
		{ field: 'name', title: '当前节点' },
		{ field: 'handler', title: '交办人' },
		{ field: 'name', title: '交办部门' },
		{ field: 'acceptorName', title: '接办人' },
		{ field: 'name', title: '接办部门' },
		{ field: 'name', title: '办理人' },
		{ field: 'acceptTime', title: '签收时间', width: 160, formatter: 'formatDate' },
		{ field: 'handleTime', title: '办理时间', width: 160, formatter: 'formatDate' },
		{ field: 'stepExpiredTime', title: '期满时间', width: 160, formatter: 'formatDate' },
		{ field: 'expiredStatusText', title: '超期状态',minWidth:120 },
		{ field: 'traceTypeText', title: '流转状态' },
		{ field: 'counterSignTypeText', title: '是否会签', width: 90, slots: { default: 'action' } },
	],
	data: [],
});
// 打开弹窗
const openDialog = async (row: any) => {
	gridOptions.loading = true;
	state.dialogVisible = true;
	await getTable(row);
	currentRow.value = row;
};
const getTable = async (row: any) => {
	try {
		// 查询审核记录
		const { result } = await workflowTraces(row.workflowId);
		state.traces = result?.traces ?? [];
		state.traces = formatTraces(state.traces);
		state.dialogTitle = row.dialogTitle ?? '流程明细';
		gridOptions.data = formatTraces(state.traces);
		gridOptions.loading = false;
	} catch (error) {
		console.log(error,'错误')
		gridOptions.loading = false;
	}
};
const formatTraces = (val: any) => {
	if (!val || !val.length) return [];
	val.forEach((item: any) => {
		item.files = transformFile(item.files);
		switch (item.expiredStatus) {
			case 0:
				item.type = 'success';
				break;
			case 1:
				item.type = 'primary';
				break;
			case 2:
				item.type = 'danger';
				break;
			default:
				break;
		}
		if (item.traces?.length) {
			formatTraces(item.traces);
		}
	});
	return val;
};
// 关闭流程弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 会签弹窗
const countersignDialog = ref(false);
// 查看会签
const onCountersign = (row:any)=>{
	console.log(row,'111');
	countersignDialog.value = true;
}
// 关闭会签弹窗
const closeDialogCountersign = ()=>{
	countersignDialog.value = false;
}
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
