<template>
	<el-dialog v-model="state.dialogVisible" draggable title="公开" ref="dialogRef" append-to-body destroy-on-close>
		<div class="collapse-container">
			<el-form label-width="100px" :model="state.ruleForm" v-loading="state.loading" class="show-info-form">
				<el-row :gutter="10" class="w100">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="工单标题"> {{ state.publishDetail.title }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="标题整改"> {{ state.publishDetail.arrangeTitle }} </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="受理时间">
							{{ formatDate(state.publishDetail.handleTime, 'YYYY-mm-dd HH:MM:SS') }}
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="事发地址">
							{{ state.publishDetail.fullAddress }}
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="地点整改">
							{{ state.publishDetail.arrangeAddress }}
						</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.publishDetail.content }} </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.publishDetail.arrangeContent }} </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.publishDetail.fileOpinion }}</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.publishDetail.arrangeOpinion }}</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button" :loading="state.loading">取 消</el-button>
				<template v-if="state.publishDetail.status === 0">
					<el-popconfirm title="确定同意公开？" @confirm="agree">
						<template #reference>
							<el-button type="primary" :loading="state.loading">同意</el-button>
						</template>
					</el-popconfirm>
					<el-popconfirm title="确定不同意公开？" @confirm="disagree" width="170">
						<template #reference>
							<el-button type="primary" :loading="state.loading">不同意</el-button>
						</template>
					</el-popconfirm>
				</template>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { centerPublishUserAudit, centerPublishUserDetail } from '@/api/snapshot/publish';
import { formatDate } from '@/utils/formatTime';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	orderDetail: {}, // 工单详情
	publishDetail: {}, // 发布详情
});
/*
 * @param row 工单详情
 *
 * @description 打开弹窗
 * */
const openDialog = async (row: any) => {
	state.loading = true;
	state.dialogVisible = true;
	try {
		state.orderDetail = row ?? {};
		const res = await centerPublishUserDetail(state.orderDetail.id);
		state.publishDetail = res.result ?? {};
		state.loading = false;
		state.dialogVisible = true;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
// 同意
const agree = () => {
	state.loading = true;
	centerPublishUserAudit({ id: state.orderDetail.id, status: 1 })
		.then(() => {
			state.loading = false;
			ElMessage.success('审批完成');
			updateList();
		})
		.catch(() => {
			state.loading = false;
		});
};
// 不同意
const disagree = () => {
	state.loading = true;
	centerPublishUserAudit({ id: state.orderDetail.id, status: 2 })
		.then(() => {
			state.loading = false;
			ElMessage.success('审批完成');
			updateList();
		})
		.catch(() => {
			state.loading = false;
		});
};
// 重办和退回成功
const updateList = () => {
	closeDialog();
	emit('updateList');
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
