<template>
	<el-dialog v-model="state.dialogVisible" draggable title="甄别审批" ref="dialogRef" width="50%" append-to-body destroy-on-close @close="close">
		<template #header>
			<el-tabs v-model="state.activeName" @tab-change="handleClick">
				<el-tab-pane
					:name="item.value"
					v-for="item in state.tabPaneList"
					:key="item.value"
					:label="item.label"
					:disabled="state.loading"
				></el-tab-pane>
			</el-tabs>
		</template>
		<div v-show="state.activeName === '0'">
			<el-form label-width="110px" :model="state.infoForm" class="show-info-form" v-loading="state.loading">
				<p class="border-title">申请信息</p>
				<el-row :gutter="10">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="工单编号"> {{ state.infoForm?.no }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="工单标题"> {{ state.infoForm?.visit?.order?.title }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="申请人">{{ state.infoForm?.creatorName }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="申请部门">{{ state.infoForm?.creatorOrgName }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="申请时间">{{ formatDate(state.infoForm?.creationTime, 'YYYY-mm-dd HH:MM:SS') }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="申请类型"> {{ state.infoForm?.typeDicName }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="发起甄别耗时"> {{ state.infoForm?.timeConsuminText }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="是否省甄别">{{ state.infoForm?.isProvince ? '是' : '否' }} </el-form-item>
					</el-col>
					<template v-if="['ZiGong'].includes(themeConfig.appScope)">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="不满意原因">{{ state.infoForm?.visitDetail?.orgNoSatisfiedReasonText }} </el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
							<el-form-item label="部门回访内容" class="formatted-text">{{ state.infoForm?.visitDetail?.visitContent }} </el-form-item>
						</el-col>
					</template>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="申请理由" class="formatted-text">{{ state.infoForm?.content }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="附件"
							><annex-list name="甄别申请附件" v-model="state.infoForm.files" readonly classify="甄别申请附件" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<p class="border-title mt10 mb10">甄别审核明细</p>
			<vxe-table :expand-config="{ trigger: 'row', padding: true }" border :data="tableData" :loading="state.loading" max-height="500px">
				<vxe-column type="seq" width="70"></vxe-column>
				<vxe-column type="expand" width="60">
					<template #content="{ row }">
						<div class="mb5 formatted-text">{{ row.handleModeTypeText }}：{{ row.opinion }}</div>
						<annex-list name="附件" readonly businessId="" classify="查看附件" v-model="row.files" />
					</template>
				</vxe-column>
				<vxe-column field="name" title="节点"></vxe-column>
				<vxe-column field="handlerName" title="审批人"></vxe-column>
				<vxe-column field="handleTime" title="审批时间" width="160">
					<template #default="{ row }">{{ formatDate(row.handleTime, 'YYYY-mm-dd HH:MM:SS') }}</template>
				</vxe-column>
				<vxe-column field="handleModeText" title="状态"></vxe-column>
			</vxe-table>
			<p class="border-title mt20 mb10">审核意见</p>
			<el-form :model="state.ruleForm" label-width="110px" ref="ruleFormRef" v-loading="state.loading">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="审批结果" prop="isPass" :rules="[{ required: true, message: '请选择审批结果', trigger: 'change' }]">
							<el-radio-group v-model="state.ruleForm.isPass">
								<el-radio :value="true">同意</el-radio>
								<el-radio :value="false">不同意</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<template v-if="state.ruleForm.isPass">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
							<el-form-item label="下一环节" prop="nextStepCode" :rules="[{ required: true, message: '请选择下一环节', trigger: 'change' }]">
								<el-select v-model="state.ruleForm.nextStepCode" placeholder="请选择下一环节" class="w100" @change="selectNextStep">
									<el-option v-for="item in state.nextStepOptions" :key="item.key" :label="item.value" :value="item.key" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="showHandlers">
							<el-form-item
								label="办理对象"
								prop="nextHandlers"
								:rules="[{ required: nextHandlersRequired, message: '请选择办理对象', trigger: 'change' }]"
							>
								<el-select-v2
									v-model="state.ruleForm.nextHandlers"
									:options="state.handlerOptions"
									placeholder="请选择办理对象"
									class="w100"
									multiple
									clearable
									collapse-tags
									collapse-tags-tooltip
									filterable
									value-key="key"
									@change="selectHandlers"
									:multiple-limit="multipleLimit"
								/>
							</el-form-item>
						</el-col>
					</template>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="" prop="isSms">
							<el-checkbox v-model="state.ruleForm.isSms" label="短信通知" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="审批意见" prop="opinion" :rules="[{ required: true, message: '请填写审批意见', trigger: 'blur' }]">
							<common-advice
								@chooseAdvice="chooseAdvice"
								v-model="state.ruleForm.opinion"
								placeholder="请填写审批意见"
								:loading="state.loading"
								:commonEnum="commonEnum.Discriminate"
								:maxlength="AppConfigInfo.handleOpinionWordLimit"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="附件" :rules="[{ required: false, message: '请上传附件', trigger: 'change' }]">
							<annex-list name="甄别附件" classify="甄别上传" v-model:format="handleFiles" />
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item
							label="省甄别附件"
							:rules="[{ required: false, message: '请上传省甄别附件', trigger: 'change' }]"
							v-if="isPickFile && state.ruleForm.isPass"
						>
							<vxe-grid v-bind="gridOptions" class="w100" ref="gridRef" @checkbox-all="selectAllChangeEvent" @checkbox-change="selectChangeEvent">
								<template #action="{ row }">
									<el-button type="primary" link @click="onDownload(row)">下载</el-button>
									<el-button type="primary" link @click="onPreview(row)">预览</el-button>
								</template>
							</vxe-grid>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<!-- 历史工单 -->
		<div v-show="state.activeName === '1'">
			<history-order :ruleForm="state.order" :orderId="orderId" ref="historyOrderRef" />
		</div>
		<template #footer v-if="state.activeName === '0'">
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="state.loading">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts" name="discernDetail">
import { computed, defineAsyncComponent, reactive, ref } from 'vue';
import { formatDate } from '@/utils/formatTime';
import { discernApprove, discernApproveParams, screenDetail, screenFiles } from '@/api/business/discern';
import { transformFile } from '@/utils/tools';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { workflowReject, workflowTraces } from '@/api/system/workflow';
import { commonEnum } from '@/utils/constants';
import { useAppConfig } from '@/stores/appConfig';
import { storeToRefs } from 'pinia';
import other from '@/utils/other';
import { useThemeConfig } from '@/stores/themeConfig';
import { fileDownloadByUrl } from '@/api/public/file';

const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue')); // 附件列表
const CommonAdvice = defineAsyncComponent(() => import('@/components/CommonAdvice/index.vue')); // 常用意见
const HistoryOrder = defineAsyncComponent(() => import('@/components/OrderDetail/History.vue')); // 历史工单

const appConfigStore = useAppConfig();
const { AppConfigInfo } = storeToRefs(appConfigStore); // 系统配置信息
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const emit = defineEmits(['updateList']); // 定义事件
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	infoForm: {},
	ruleForm: {
		isPass: true,
		nextHandlers: [],
		isSms: false,
		opinion: '', // 意见
		nextStepCode: '', // 下一节点
		nextStepName: '', // 下一节点名称
		backToCountersignEnd: false,
		stepId: null,
		workflowId: null,
	},
	handlerOptions: [],
	nextStepOptions: [],
	activeName: '0', // tab切换
	tabPaneList: [
		// tab列表
		{
			label: '甄别审批',
			value: '0',
		},
		{
			label: '历史工单',
			value: '1',
		},
	],
	order: {},
});
// 泸州和自贡不展示历史工单选项
// 根据地区动态过滤 tabPaneList
const filteredTabPaneList = computed(() => {
	if (['LuZhou', 'ZiGong'].includes(themeConfig.value.appScope)) {
		return state.tabPaneList.filter((item: any) => item.value !== '1');
	}
	return state.tabPaneList;
});

// 更新 state.tabPaneList
state.tabPaneList = filteredTabPaneList.value;
/*
 * @params row 工单详情
 * @description 打开弹窗
 * */
const orderId = ref('');
const discernId = ref(''); // 甄别ID
const openDialog = async (row: any) => {
	state.dialogVisible = true;
	state.loading = true;
	discernId.value = row.id;
	try {
		const { result } = await screenDetail(row.id);
		state.infoForm = result ?? {};
		state.infoForm.files = transformFile(state.infoForm.files);
		state.ruleForm.workflowId = row.workflowId;
		state.order = result.order ?? {};
		orderId.value = result.orderId;
		await getWorkflow(result.workflowId);
		await selectNextStepOptions(result.workflowId, row.id);
		state.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 切换tab 查询列表
const handleClick = (val: string) => {
	switch (val) {
		case '0': // 甄别审批
			break;
		case '1': // 历史工单
			getHistoryList();
			break;
		default:
			break;
	}
};
// 查询历史工单
const historyOrderRef = ref<RefType>(); // 历史工单
const getHistoryList = async () => {
	state.loading = true;
	try {
		historyOrderRef.value.searchHistory();
		state.loading = false;
	} catch (error: any) {
		console.log(error);
		state.loading = false;
	}
};
const formatTraces = (val: any) => {
	if (!val || !val.length) return [];
	val.forEach((item: any) => {
		item.files = transformFile(item.files);
		// 需要给意见做一个映射关系 正常就是审核意见 其他状态就是其他意见 比如重办就是重办意见
		switch (item.handleMode) {
			case 0:
				item.handleModeTypeText = '审核意见';
				break;
			case 100:
				item.handleModeTypeText = '退回意见';
				break;
			case 200:
				item.handleModeTypeText = '特提意见';
				break;
			case 201:
				item.handleModeTypeText = '重办意见';
				break;
			case 203:
				item.handleModeTypeText = '发布退回意见';
				break;
			default:
				break;
		}
	});
	return val;
};
// 获取流程
const tableData = ref<EmptyArrayType>([]);
const getWorkflow = async (workflowId: string) => {
	try {
		// 查询审核记录
		const { result } = await workflowTraces(workflowId);
		tableData.value = formatTraces(result?.traces);
	} catch (e) {
		console.log(e);
	}
};
const gridOptions = reactive<any>({
	border: true,
	maxHeight: 500,
	rowConfig: {
		isHover: true,
	},
	columns: [
		{ type: 'checkbox', width: 50 },
		{ field: 'fileName', title: '文件名' },
		{ field: 'orgName', title: '上传部门' },
		{ field: 'userName', title: '上传人' },
		{ title: '操作', width: 120, fixed: 'right', align: 'center', slots: { default: 'action' } },
	],
	data: [],
});
const gridRef = ref<RefType>();
const provinceFiles = ref<EmptyArrayType>([]); // 省甄别附件
const selectAllChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		provinceFiles.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		provinceFiles.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
// 下载附件
const onDownload = (row: any) => {
	// 确定是否下载
	ElMessageBox.confirm(`确定要下载附件 ${row.fileName} 吗？`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			fileDownloadByUrl({
				Source: 'hotline',
				Id: row.additions,
			}).then((res: any) => {
				let blob: Blob = new Blob([res.data], { type: res.data.type }); // 创建blob 设置blob文件类型 data 设置为后端返回的文件(例如mp3,jpeg) type:这里设置后端返回的类型 为 mp3
				let down: HTMLAnchorElement = document.createElement('a'); // 创建A标签
				let href: string = window.URL.createObjectURL(blob); // 创建下载的链接
				down.href = href; // 下载地址
				down.download = row.fileName; // 下载文件名
				document.body.appendChild(down);
				down.click(); // 模拟点击A标签
				document.body.removeChild(down); // 下载完成移除元素
				window.URL.revokeObjectURL(href); // 释放blob对象
			});
		})
		.catch(() => {});
};
// 预览附件
const onPreview = (row: any) => {
	const url = import.meta.env.VITE_API_UPLOAD_URL + row.path;
	window.open(url, '_blank');
};
const nextHandlersRequired = ref<Boolean>(false); // 办理对象是否必填
const canStartCountersign = ref<Boolean>(false); // 是否可以会签
const selectNext = ref<any>({});
const isPickFile = ref<Boolean>(false); // 是否要展示选择的附件列表选择
// 查询流程办理节点
const selectNextStepOptions = async (workflowId: string, discernId: string) => {
	try {
		const { result } = await discernApproveParams(workflowId); //获取甄别审批流程参数
		state.nextStepOptions = result.steps; //办理对象选择内容
		canStartCountersign.value = result.canStartCountersign ?? false; // 是否可以发起会签
		isPickFile.value = result.isPickFile ?? false;
		if (result.isPickFile) {
			getAllFiles(discernId);
		}
		if (state.nextStepOptions.length === 1) {
			// 下一节点是否只有一个 默认选中第一个
			setTimeout(() => {
				state.ruleForm.nextStepCode = state.nextStepOptions[0].key; // 下一节点code
				state.ruleForm.nextStepName = state.nextStepOptions[0].value; // 下一节点name
				state.ruleForm.backToCountersignEnd = state.nextStepOptions[0].backToCountersignEnd ?? false; // 是否回到会签结束节点
			}, 100);
			selectNextStep(state.nextStepOptions[0].key); // 查询流程下一节点参数
		} else {
			state.ruleForm.nextStepCode = '';
			state.ruleForm.nextStepName = '';
		}
		state.ruleForm.stepId = result.stepId;
		state.loading = false;
	} catch (e) {
		console.log(e);
	}
};
// 获取甄别的文件
const getAllFiles = (id: string) => {
	screenFiles(id)
		.then((res: any) => {
			gridOptions.data = res.result ?? [];
		})
		.catch((err) => {
			console.log(err, '2222');
		});
};
const selectNextStep = (val: any) => {
	ruleFormRef.value?.resetFields('nextHandlers');
	const next = state.nextStepOptions.find((item: any) => item.key === val);
	selectNext.value = next;
	const items = next.items; //获取下一节点
	state.ruleForm.nextStepName = next.value; // 下一节点name
	state.ruleForm.handlerType = next.handlerType;
	state.ruleForm.businessType = next.businessType;
	state.ruleForm.flowDirection = next.flowDirection;
	state.ruleForm.backToCountersignEnd = next.backToCountersignEnd ?? false; // 是否回到会签结束节点
	state.handlerOptions = items ?? [];
	state.handlerOptions = state.handlerOptions.map((item: any) => {
		return {
			value: {
				...item,
			},
			label: item.value,
		};
	});
	// 办理对象是否必填
	nextHandlersRequired.value = ![0].includes(next.handlerType);

	if (items.length === 1) {
		// 如果办理对象只有一个默认选中
		state.ruleForm.nextHandlers = [items[0]];
	}
};
// 选择办理对象
const selectHandlers = () => {
	if (state.ruleForm.nextHandlers.length <= 1) {
		// 如果只有一个办理对象就不需要发起会签
		state.ruleForm.isStartCountersign = false;
	}
};
// 是否展示办理对象 (结束节点不展示: next.stepType===2 表示为结束节点)
const showHandlers = computed(() => {
	const next = state.nextStepOptions.find((item: any) => item.key === state.ruleForm.nextStepCode);
	if (!next) return true;
	// 话务部到派单 派单组到一级部门 也不需要展示办理对象
	return next?.stepType !== 2;
});

// 办理对象是否能够选择多个(可以发起会签可以选择多个，不能发起会签只能选择一个) 加个判断 选择的下一环节必须是部门（会签必须是选择的部门）
const multipleLimit = computed(() => {
	return canStartCountersign.value && selectNext.value.businessType === 2 ? 0 : 1;
});
// 选择常用意见 填入填写框 办理
const chooseAdvice = (item: any) => {
	state.ruleForm.opinion += item.content;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.resetFields();
	state.activeName = '0';
	provinceFiles.value = [];
	discernId.value = '';
	orderId.value = '';
};
// 提交
const afterSubmit = (emitType?: 'updateList', showMessage?: boolean, message?: string) => {
	state.loading = false;
	closeDialog();
	const msg = message ?? '甄别审批成功';
	if (showMessage) ElMessage.success(msg);
	if (emitType) emit(emitType);
};
const ruleFormRef = ref<RefType>();
const handleFiles = ref<EmptyArrayType>([]); // 附件
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		let submitObj = other.deepClone(state.ruleForm);
		if (state.ruleForm.isPass) {
			state.loading = true;
			// 审批通过 下一步
			discernApprove({ ...submitObj, reviewResult: 1, files: handleFiles.value, provinceFiles: provinceFiles.value, screenId: discernId.value })
				.then(() => {
					afterSubmit('updateList', true);
				})
				.catch(() => {
					afterSubmit('updateList');
				});
		} else {
			if (['ZiGong'].includes(themeConfig.value.appScope)) {
				ElMessageBox.confirm(`审批不同意后，申请人无法再次申请甄别，是否确认提交？`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					draggable: true,
					autofocus: false,
				})
					.then(() => {
						state.loading = true;
						// 审批拒绝
						const requestDiscernAudit = {
							opinion: state.ruleForm.opinion,
							workflowId: state.ruleForm.workflowId,
							files: handleFiles.value,
							stepId: state.ruleForm.stepId,
						};
						workflowReject(requestDiscernAudit)
							.then(() => {
								afterSubmit('updateList', true);
							})
							.catch(() => {
								afterSubmit('updateList');
							});
					})
					.catch(() => {});
			} else {
				// 审批拒绝
				const requestDiscernAudit = {
					opinion: state.ruleForm.opinion,
					workflowId: state.ruleForm.workflowId,
					files: handleFiles.value,
					stepId: state.ruleForm.stepId,
				};
				workflowReject(requestDiscernAudit)
					.then(() => {
						afterSubmit('updateList', true);
					})
					.catch(() => {
						afterSubmit('updateList');
					});
			}
		}
	});
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
