<template>
	<el-dialog v-model="state.dialogVisible" width="50%" draggable title="修改常用意见" @close="close">
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="意见内容" prop="content" :rules="[{ required: true, message: '请填写意见内容', trigger: 'blur' }]">
						<el-input
							v-model="state.ruleForm.content"
							placeholder="请填写意见内容"
							:autosize="{ minRows: 6, maxRows: 10 }"
							type="textarea"
							show-word-limit
							maxlength="2000"
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="意见类型" prop="commonType" :rules="[{ required: true, message: '请选择意见类型', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.commonType" placeholder="请选择意见类型" clearable class="w100">
							<el-option v-for="item in commonType" :value="item.key" :key="item.key" :label="item.value" />
						</el-select>
					</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="onSubmit(ruleFormRef)" :loading="loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="auxiliaryAdviceEdit">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { commonUpdate } from '@/api/auxiliary/advice';
import other from '@/utils/other';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
const props = defineProps({
	commonType: {
		type: Array,
		default: () => [],
	},
});
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		content: '', // 意见内容
		CommonType: '', // 意见类型
		isOpen: true, // 是否公开意见
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = async (row: any) => {
	state.dialogVisible = true;
	ruleFormRef.value?.resetFields();
	try {
		state.ruleForm = other.deepClone(row);
	} catch (error) {
		console.log(error);
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.resetFields();
};
// 新增
const onSubmit = throttle(async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid: boolean) => {
		if (!valid) return;
		loading.value = true;
		commonUpdate(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				loading.value = false;
				closeDialog();
				emit('updateList');
			})
			.catch(() => {})
			.finally(() => {
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
