<template>
	<el-dialog title="新增外呼任务模板" v-model="state.dialogVisible" width="800px" draggable @close="close">
		<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="100px">
			<el-form-item label="模板名称" prop="templateName" :rules="[{ required: true, message: '请填写模板名称', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.templateName" placeholder="请填写模板名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="模板内容" prop="templateContent" :rules="[{ required: true, message: '请填写模板内容', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.templateContent" placeholder="请填写模板内容" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }"></el-input>
			</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" name="qualityLexiconAdd">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { smartCallOutTemAdd } from "@/api/auxiliary/smartCallOut";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const ruleFormRef = ref<FormInstance>();
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	ruleForm: {
    templateName: null, // 模板名称
    templateContent: null, // 模板内容
	},
	loading: false, // 确定按钮loading
});
// 打开弹窗
const openDialog = async () => {
	try {
		state.dialogVisible = true;
	} catch (e) {
		console.log(e);
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
};
// 保存
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
    smartCallOutTemAdd(state.ruleForm)
      .then(() => {
        ElMessage({
          message: '操作成功',
          type: 'success',
        });
        emit('updateList');
        state.loading = false;
      })
      .catch(() => {})
      .finally(() => {
        state.loading = false;
        closeDialog();
      });
	});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
