<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-row :gutter="10">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="手机编码" prop="mobileCode" :rules="[{ required: true, message: '请输入手机编码', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.mobileCode" placeholder="请输入手机编码" clearable />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="座机编码" prop="telCode" :rules="[{ required: true, message: '请输入座机编码', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.telCode" placeholder="请输入座机编码" clearable />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="运营商" prop="oFlag" :rules="[{ required: true, message: '请选择运营商', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.oFlag" placeholder="请选择运营商" clearable class="w100">
						<el-option v-for="item in OFlagOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="卡类型" prop="operatorName" :rules="[{ required: true, message: '请输入卡类型', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.operatorName" placeholder="请输入卡类型" clearable />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-form-item label="归属地" prop="mobileAreaName" :rules="[{ required: true, message: '请输入归属地', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.mobileAreaName" placeholder="请输入归属地" clearable />
					</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="state.loading">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { getObservationsReply } from '@/api/business/observations';
import { addNumberInfo, getNumberInfoBaseData } from '@/api/auxiliary/numberInfo';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	ruleForm: {
		mobileCode: null, // 手机编码
		telCode: null, // 座机编码
		oFlag: null, // 运营商
		operatorName: null, // 卡类型
		mobileAreaName: null, // 归属地
	},
	loading: false, // 提交按钮loading
});
// 查询基础数据
const OFlagOptions = ref<EmptyArrayType>([]); // 运营商
const getBaseData = async () => {
	try {
		const { result } = await getNumberInfoBaseData();
		OFlagOptions.value = result ?? [];
	} catch (e) {
		console.log(e);
	}
};
const ruleFormRef = ref<RefType>();
// 打开弹窗
const openDialog = async () => {
	getBaseData();
	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;
		addNumberInfo(state.ruleForm)
			.then(() => {
				ElMessage.success('新增成功');
				state.loading = false;
				state.dialogVisible = false;
				emit('updateList');
			})
			.catch(() => {
				state.loading = false;
				state.dialogVisible = false;
			});
	});
}, 300);
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
