<template>
	<el-drawer v-model="drawer" title="政民互动" destroy-on-close @close="close">
		<el-form :model="state.ruleForm" ref="ruleFormRef" @submit.native.prevent label-width="80px">
			<el-form-item label="标题" prop="title">
				<el-input v-model="state.ruleForm.title" disabled />
			</el-form-item>
			<el-form-item label="受理内容" prop="content">
				<el-input v-model="state.ruleForm.content" disabled type="textarea" :rows="4" />
			</el-form-item>
			<el-form-item label="办理结果" prop="fileOpinion">
				<el-input v-model="state.ruleForm.fileOpinion" disabled type="textarea" :rows="4" />
			</el-form-item>
			<el-form-item label="整理结果" prop="answerContent" :rules="[{ required: true, message: '请填写整理结果', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.answerContent" type="textarea" :rows="4" />
			</el-form-item>
			<el-form-item label="公开意见" prop="publicOpinion" :rules="[{ required: true, message: '请填写公开意见', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.publicOpinion" type="textarea" :rows="4" maxlength="300" show-word-limit />
			</el-form-item>
			<el-alert title="注意" type="warning" description="公开后，省平台可再次审核修改公开内容" show-icon :closable="false" />
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDrawer" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
			</span>
		</template>
	</el-drawer>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { provinceZmhdAdd } from '@/api/province';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	ruleForm: {
		answerContent: '',
		publicOpinion: '',
		title: '',
		content: '',
		fileOpinion: '',
	},
});

const drawer = ref(false);
const loading = ref(false);
const openDrawer = (row: any) => {
	drawer.value = true;
	loading.value = false;
	state.ruleForm = {
		title: row.title,
		content: row.content,
		fileOpinion: row.fileOpinion,
		orderId: row.id,
	};
};
const closeDrawer = () => {
	drawer.value = false;
};
// 保存
const ruleFormRef = ref<RefType>();
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		provinceZmhdAdd(state.ruleForm)
			.then(() => {
				ElMessage.success('操作成功');
				emit('updateList');
				closeDrawer();
				loading.value = false;
			})
			.catch(() => {
				loading.value = false;
			});
	});
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
defineExpose({
	openDrawer,
	closeDrawer,
});
</script>
