<template>
	<el-dialog v-model="state.dialogVisible" width="50%" draggable title="新增短信" destroy-on-close append-to-body @close="close">
		<el-form :model="state.ruleForm" label-width="100px" ref="ruleFormRef">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="行业类型" prop="industryId" :rules="[{ required: true, message: '请选择行业类型', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.industryId" placeholder="请选择行业类型" class="w100" clearable filterable>
							<el-option v-for="item in industry" :key="item.id" :label="item.name" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="审批类型" prop="status" :rules="[{ required: false, message: '请选择审批类型', trigger: 'change' }]">
						<el-radio-group v-model="state.ruleForm.status">
							<el-radio :value="0">同意</el-radio>
							<el-radio :value="1">不同意</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="公用" prop="isPublic" :rules="[{ required: false, message: '请选择公用', trigger: 'change' }]">
						<el-switch v-model="state.ruleForm.isPublic" inline-prompt active-text="是" inactive-text="否" />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="状态" prop="isEnable" :rules="[{ required: false, message: '请选择审批类型', trigger: 'change' }]">
						<el-switch v-model="state.ruleForm.isEnable" inline-prompt active-text="启用" inactive-text="禁用" />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="排序" prop="displayOrder" :rules="[{ required: false, message: '请填写排序', trigger: 'blur' }]">
						<el-input-number v-model="state.ruleForm.displayOrder" :min="1" :precision="0" class="w100" />
					</el-form-item>
				</el-col>
				<el-col>
					<el-form-item label="短信内容" prop="content" :rules="[{ required: false, message: '请填写短信内容', trigger: 'blur' }]">
						<el-input
							type="textarea"
							:autosize="{ minRows: 6, maxRows: 10 }"
							v-model="state.ruleForm.content"
							placeholder="请填写短信内容"
							clearable
						></el-input>
					</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">
import { onMounted, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { SetingsAdd } from '@/api/system/parameter';
import { addClue, addSms, baseClue, baseIndustry } from '@/api/snapshot/config';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		isEnable: true, // 是否启用
		displayOrder: 1, // 排序
		industryId: null, // 行业类型
		content: null, // 短信内容
		isPublic: true, // 公用
		status: 0, // 审批类型
	},
});
let loading = ref<boolean>(false); // 加载状态
// 打开弹窗
const ruleFormRef = ref<RefType>();
const openDialog = async () => {
	await getBaseData();
	try {
		state.dialogVisible = true;
	} 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;
		addSms(state.ruleForm)
			.then(() => {
				ElMessage({
					message: '操作成功',
					type: 'success',
				});
				emit('updateList');
			})
			.catch(() => {})
			.finally(() => {
				loading.value = false;
				closeDialog();
			});
	});
}, 300);
const industry = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await baseClue();
		industry.value = result.industry;
	} catch (e) {
		console.log(e);
	}
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
