<template>
	<div class="system-blacklist-add-container">
		<el-dialog v-model="state.dialogVisible" width="500px" draggable :title="dialogTitle">
			<el-form :model="state.ruleForm" ref="whitelistFormRef" label-width="80px" label-position="left">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="电话号码" prop="telPhone" :rules="[{ required: true, message: '请输入电话号码', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.telPhone" 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="remark" :rules="[{ required: false, message: '请填写备注', trigger: 'blur' }]">
              <el-input
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 6 }"
                  maxlength="2000"
                  placeholder="请填写备注"
                  v-model="state.ruleForm.remark"
              ></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(whitelistFormRef)">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

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

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList', 'openDialog', 'closeDialog']);

// 定义变量内容
const dialogTitle = ref<string>('新增白名单');
const state = reactive<any>({
	ruleForm: {
    telPhone: '',  // 电话号码
    remark: '', // 备注
	},
	dialogVisible: false, // 弹窗显示隐藏
});
// 打开弹窗
const whitelistFormRef = ref<RefType>();
const openDialog = async (row?:any) => {
	state.dialogVisible = true;
	emit('openDialog');
  await nextTick(() => {
    whitelistFormRef.value?.resetFields();
    whitelistFormRef.value?.clearValidate();
    if(row && row.zTelPhoneID) {
      dialogTitle.value = '编辑白名单';
      state.ruleForm = other.deepClone(row);
    }else{
      dialogTitle.value = '新增白名单';
      state.ruleForm = {
        telPhone: '',  // 电话号码
        remark: '', // 备注
      };
    }
  });
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
	emit('closeDialog');
};
// 新增保存
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: boolean) => {
    if(!valid) return;
    const operation = state.ruleForm.zTelPhoneID ? whitelistEdit : whitelistAdd; // 判断是新增还是编辑
      Reflect.deleteProperty(state.ruleForm, 'time');
    operation(state.ruleForm).then(() => {
        ElMessage.success('操作成功');
        emit('updateList');
        state.dialogVisible = false;
      });
	});
};
//暴漏变量和方法
defineExpose({ closeDialog, openDialog });
</script>
