<template>
	<el-dialog v-model="state.dialogVisible" width="500px" draggable :title="dialogTitle" @close="close">
		<el-form :model="state.ruleForm" ref="blacklistFormRef" label-width="90px" label-position="left" :disabled="state.loading">
			<el-form-item label="电话号码" prop="phoneNum" :rules="[{ required: true, message: '请填写电话号码', trigger: 'blur' }]">
				<el-input v-model="state.ruleForm.phoneNum" placeholder="请填写电话号码" clearable></el-input>
			</el-form-item>
			<el-form-item label="有效期" prop="validDateTime" :rules="[{ required: false, message: '请选择有效期', trigger: 'change' }]">
				<template #label>
					<p class="flex-center-align">
						有效期
						<el-tooltip content="不选择有效期表示永久有效" placement="top-start" trigger="hover">
							<SvgIcon name="ele-QuestionFilled" class="ml3"></SvgIcon>
						</el-tooltip>
					</p>
				</template>
				<el-date-picker
					v-model="state.ruleForm.validDateTime"
					type="datetime"
					value-format="YYYY-MM-DD[T]HH:mm:ss"
					placeholder="请选择有效期"
					class="w100"
				/>
			</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(blacklistFormRef)" :loadin="state.loading">保 存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { addBlackListXT, addWhiteListXT } from '@/api/tels/blackList';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);
const dialogTitle = ref('新增黑名单'); // 弹窗标题
// 定义变量内容

const state = reactive<any>({
	ruleForm: {
		phoneNum: null, // 电话号码
		validDateTime: null, // 结束时间
	},
	dialogVisible: false, // 弹窗显示隐藏
	loading: false,
});
// 打开弹窗
const blacklistFormRef = ref<RefType>();
const logTypeStr = ref('1'); // 黑白名单类型
const openDialog = (logType: string) => {
	logTypeStr.value = logType;
	if (logType === '1') {
		dialogTitle.value = '新增黑名单';
	} else {
		dialogTitle.value = '新增白名单';
	}
	state.dialogVisible = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	blacklistFormRef.value?.resetFields();
	blacklistFormRef.value?.clearValidate();
};
// 新增保存
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		if (logTypeStr.value === '1') {
			// 黑名单
			addBlackListXT(state.ruleForm)
				.then(() => {
					ElMessage.success('新增成功');
					emit('updateList');
					state.dialogVisible = false;
					state.loading = false;
				})
				.catch(() => {
					state.loading = false;
				});
		} else {
			addWhiteListXT(state.ruleForm)
				.then(() => {
					ElMessage.success('新增成功');
					emit('updateList');
					state.dialogVisible = false;
					state.loading = false;
				})
				.catch(() => {
					state.loading = false;
				});
		}
	});
};
//暴漏变量和方法
defineExpose({ closeDialog, openDialog });
</script>
