<template>
	<el-dialog v-model="state.dialogVisible" draggable title="催办详情" ref="dialogRef" width="50%" destroy-on-close @close="close">
		<div class="collapse-container">
			<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.orderDetail.no }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="工单标题"> {{ state.orderDetail.title }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="催办人"> {{ state.urgeDetail.crUser }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="催办部门"> {{ state.urgeDetail.creatorOrgName }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="催办时间"> {{ formatDate(state.urgeDetail.creationTime, 'YYYY-mm-dd HH:MM:SS') }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
						<el-form-item label="被催办部门">{{ state.urgeDetail.orgName }}</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.urgeDetail.applyContent }}</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="附件" prop="handleResult" :rules="[{ required: false, message: '请选择附件', trigger: 'change' }]">
							<annex-list name="催办附件" readonly classify="催办附件" v-model="state.urgeDetail.files" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">关 闭</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts" name="businessUrgeDetailDialog">
import { defineAsyncComponent, reactive } from 'vue';
import { formatDate } from '@/utils/formatTime';
import { urgeDetail, urgeSign } from '@/api/business/urge';
import { ElMessage } from 'element-plus';
import { transformFile } from '@/utils/tools';

// 引入组件
const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue'));

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		acceptSms: false, // 发送催办短信
		applyContent: '', // 催办内容
	},
	urgeDetail: {}, // 催办详情
	orderDetail: {}, // 工单详情
	orgData: [], // 被催办部门
	urgeObj: {},
});
const getDetail = async (id: string) => {
	try {
		const { result } = await urgeDetail(id);
		state.urgeDetail = result ?? {};
		state.orderDetail = result?.order ?? {};
		state.urgeDetail.files = transformFile(state.urgeDetail.files);
		state.dialogVisible = true;
	} catch (e) {
		console.log(e);
	}
};
// 催办签收
const onSign = (id: string) => {
	urgeSign({ id }).then(() => {
		ElMessage.success('签收成功');
		emit('updateList');
	});
};
// 打开弹窗
const openDialog = (val: any) => {
	state.urgeObj = val;
	getDetail(val.id);
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	if (!state.urgeObj.signTime) {
		// 如果没有签收时间签收
		onSign(state.urgeObj.id);
	}
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
