<template>
	<el-dialog v-model="state.dialogVisible" draggable title="公开" ref="dialogRef" append-to-body destroy-on-close @close="close">
		<div class="collapse-container">
			<el-form label-width="100px" ref="ruleFormRef" :model="state.ruleForm" v-loading="state.loading">
				<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="整改后标题" prop="arrangeTitle" :rules="[{ required: false, message: '请填写整改后标题', trigger: 'blur' }]">
							<el-input placeholder="请填写整改后标题" v-model="state.ruleForm.arrangeTitle" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="受理时间">
							{{ formatDate(state.publishDetail.creationTime, '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="地点整改" prop="arrangeAddress" :rules="[{ required: false, message: '请填写地点整改', trigger: 'blur' }]">
							<el-input placeholder="请填写地点整改" v-model="state.ruleForm.arrangeAddress" clearable></el-input>
						</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="整改后内容" prop="arrangeContent" :rules="[{ required: false, message: '请填写整改后内容', trigger: 'blur' }]">
							<el-input
								placeholder="请填写整改后内容"
								type="textarea"
								:autosize="{ minRows: 6, maxRows: 10 }"
								v-model="state.ruleForm.arrangeContent"
								show-word-limit
								:maxlength="5000"
							></el-input>
						</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="整改后结果" prop="arrangeOpinion" :rules="[{ required: false, message: '请填写整改后内容', trigger: 'blur' }]">
							<el-input
								type="textarea"
								placeholder="请填写整改后办件结果"
								:autosize="{ minRows: 6, maxRows: 10 }"
								v-model="state.ruleForm.arrangeOpinion"
								show-word-limit
								:maxlength="AppConfigInfo.nationalPlatformWordLimit"
							></el-input>
						</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>
				<el-button type="primary" @click="onPublish(ruleFormRef)" :loading="state.loading">保 存</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { storeToRefs } from 'pinia';
import { useAppConfig } from '@/stores/appConfig';
import { centerPublishAdd, centerPublishDetail } from '@/api/snapshot/publish';
import { formatDate } from '@/utils/formatTime';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 是否显示弹窗
	loading: false, // 是否显示加载
	ruleForm: {
		arrangeTitle: '', // 整改后标题
		arrangeContent: '', // 整改后内容
		arrangeOpinion: '', // 整改后结果
		handleTime: '', // 受理时间
		arrangeAddress: '', // 整改后地址
	},
	orderDetail: {}, // 工单详情
	publishDetail: {}, // 发布详情
});
const ruleFormRef = ref<RefType>(); // 表单ref

const appConfigStore = useAppConfig();
const { AppConfigInfo } = storeToRefs(appConfigStore); // 系统配置信息
/*
 * @param row 工单详情
 *
 * @description 打开弹窗
 * */
const openDialog = async (row: any) => {
	state.loading = true;
	state.dialogVisible = true;
	try {
		state.orderDetail = row ?? {};
		const res = await centerPublishDetail(state.orderDetail.id);
		state.publishDetail = res.result ?? {};
		state.ruleForm.arrangeTitle = state.publishDetail.title;
		state.ruleForm.arrangeAddress = state.publishDetail.fullAddress;
		state.ruleForm.arrangeContent = state.publishDetail.content;
		state.ruleForm.arrangeOpinion = state.publishDetail.fileOpinion;
		state.loading = false;
		state.dialogVisible = true;
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value.clearValidate();
	ruleFormRef.value.resetFields();
};
// 发布
const onPublish = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		const request = {
			...state.ruleForm,
			orderId: state.publishDetail.id,
			handleTime: state.publishDetail.creationTime,
		};
		centerPublishAdd(request)
			.then(() => {
				ElMessage.success('发布成功');
				state.loading = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
// 重办和退回成功
const updateList = () => {
	closeDialog();
	emit('updateList');
};
defineExpose({
	openDialog,
	closeDialog,
});
</script>
