<template>
	<el-dialog v-model="state.dialogVisible" draggable title="延期详情" ref="dialogRef" width="50%" append-to-body destroy-on-close>
		<el-form label-width="110px" ref="ruleFormRef" :model="state.ruleForm" class="show-info-form">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="工单编号"> {{ state.ruleForm?.order?.no }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="工单标题"> {{ state.ruleForm?.order?.title }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="申请人">{{ state.ruleForm?.creatorName }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="申请部门">{{ state.ruleForm?.creatorOrgName }} </el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="申请时间">{{ formatDate(state.ruleForm?.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="当前期满时间"> {{ formatDate(state.ruleForm?.beforeDelay, '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.ruleForm?.delayNum }} {{ state.ruleForm?.delayUnitText }} </el-form-item>
				</el-col>
				<!--				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="延期后期满时间">{{ formatDate(state.ruleForm?.afterDelay, '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.ruleForm?.isProDelay ? '是' : '否' }} </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.ruleForm?.delayReason }} </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.ruleForm.files" readonly classify="延期申请附件" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="processDetail">流程明细</el-button>
				<el-button type="primary" @click="onAudit" v-if="state.ruleForm?.isCanHandle" v-auths="['business:delay:audit:todo', 'business:delay:audit']"
					>延期审批</el-button
				>
			</span>
		</template>
	</el-dialog>
	<!-- 审核记录 -->
	<audit-record ref="auditRecordRef" />
	<!--  流程审批  -->
	<process-audit ref="processAuditRef" @orderProcessSuccess="orderProcessSuccess" @orderProcessFailed="orderProcessFailed"></process-audit>
</template>
<script setup lang="ts" name="delayDetail">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { delayDetail } from '@/api/business/delay';
import { formatDate } from '@/utils/formatTime';
import { transformFile } from '@/utils/tools';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';
import { ElMessage } from 'element-plus';

const AuditRecord = defineAsyncComponent(() => import('@/components/AuditRecord/index.vue')); // 流程明细
const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue')); // 附件列表
const ProcessAudit = defineAsyncComponent(() => import('@/components/ProcessAudit/index.vue')); // 流程审批

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {},
	currentId: '',
});
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
/*
 * @param row 工单详情
 * @description 打开弹窗
 * */
const openDialog = async (row: any) => {
	state.loading = true;
	state.currentId = row.id;
	state.dialogVisible = true;
	try {
		const res = await delayDetail(state.currentId);
		state.ruleForm = res.result ?? {};
		state.ruleForm.files = transformFile(state.ruleForm.files);
		state.loading = false;
	} catch (e) {
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 延期审批
const processAuditRef = ref<RefType>(); // 流程审批ref
const onAudit = () => {
	const params = {
		id: state.ruleForm.workflowId,
		processType: '延期审批',
		orderDetail: state.ruleForm.order,
		extra: {
			dialogTitle: '延期审批',
			inputPlaceholder: '办理意见',
			annexName: '延期附件',
			classify: '延期上传',
		},
	};
	processAuditRef.value.openDialog(params);
};
// 流程明细
const auditRecordRef = ref<RefType>(); // 审核记录ref
const processDetail = () => {
	if (['ZiGong'].includes(themeConfig.value.appScope)) {
		// 自贡
		if (state.ruleForm?.automaticDelayNum === null) {
			// 没有自动延期正常查看
			const params = {
				dialogTitle: `延期记录 （${state.ruleForm.order?.title}）`,
				...state.ruleForm,
			};
			auditRecordRef.value.openDialog(params);
		} else {
			// 有自动延期，则不能查看
			ElMessage.warning('该工单已自动延期，没有流程明细');
		}
	} else {
		const params = {
			dialogTitle: `延期记录 （${state.ruleForm.order?.title}）`,
			...state.ruleForm,
		};
		auditRecordRef.value.openDialog(params);
	}
};
// 审核成功
const orderProcessSuccess = () => {
	emit('updateList');
	closeDialog();
};
// 审核失败
const orderProcessFailed = () => {
	emit('updateList');
	closeDialog();
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
