<template>
	<el-dialog v-model="state.dialogVisible" draggable title="添加催办" append-to-body ref="dialogRef" destroy-on-close @close="close">
		<el-form :model="state.ruleForm" label-width="90px" ref="ruleFormRef">
			<el-form-item label="催办内容" class="textarea" prop="opinion" :rules="[{ required: true, message: '请填写催办内容', trigger: 'blur' }]">
				<el-input
					v-model="state.ruleForm.opinion"
					type="textarea"
					show-word-limit
					:autosize="{ minRows: 6, maxRows: 20 }"
					placeholder="请填写催办内容"
					:maxlength="2000"
				/>
			</el-form-item>
			<el-form-item label="" prop="acceptSms" :rules="[{ required: false, message: '请选择发送催办短信', trigger: 'change' }]">
				<el-checkbox v-model="state.ruleForm.acceptSms" label="发送催办短信" />
			</el-form-item>
		</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="state.loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { orderComplement } from '@/api/business/order';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['onSupplySuccess']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		opinion: null, // 补充内容
		acceptSms: false, // 发送短信
	},
	fileList: [], // 附件列表
	loading: false, // 提交按钮loading
});
const ruleFormRef = ref<RefType>();
// 打开弹窗
const ids = ref<EmptyArrayType>([]);
const openDialog = async (val: any) => {
	ids.value = val;
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 提交
const onSubmit = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		let request = {
			...state.ruleForm,
			ids: ids.value,
		};
		orderComplement(request)
			.then(() => {
				state.loading = false;
				state.dialogVisible = false;
				emit('onSupplySuccess');
			})
			.catch(() => {
				state.loading = false;
				state.dialogVisible = false;
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
