<template>
	<el-dialog v-model="state.dialogVisible" width="500px" draggable title="新增黑名单" @close="close">
		<el-form :model="state.ruleForm" ref="blacklistFormRef" label-width="80px" label-position="left">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="电话号码" prop="phone" :rules="[{ required: true, message: '请填写电话号码', trigger: 'blur' }]">
						<el-input v-model="state.ruleForm.phone" placeholder="请填写电话号码" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
					<el-form-item label="类型" prop="specialFlag" :rules="[{ required: true, message: '请选择类型', trigger: 'change' }]">
						<el-select v-model="state.ruleForm.specialFlag" placeholder="请选择类型" class="w100">
							<el-option v-for="item in props.specialFlagList" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
					</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(blacklistFormRef)">保 存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup name="blackAdd">
import { ref, reactive } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { addBlacklist } from '@/api/public/wex';

const props = defineProps({
	specialFlagList: {
		type: Array,
		default: () => <EmptyArrayType>[],
	},
});
// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList']);

// 定义变量内容

const state = reactive<any>({
	ruleForm: {
		phone: null, // 电话号码
		specialFlag: null,
	},
	dialogVisible: false, // 弹窗显示隐藏
});
// 打开弹窗
const blacklistFormRef = ref<RefType>();
const openDialog = () => {

	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;
		addBlacklist(state.ruleForm).then(() => {
			ElMessage.success('操作成功');
			emit('updateList');
			state.dialogVisible = false;
		});
	});
};
//暴漏变量和方法
defineExpose({ closeDialog, openDialog });
</script>
