<template>
	<div class="system-blacklist-add-container">
		<el-dialog v-model="state.dialogVisible" width="500px" draggable title="新增黑名单">
			<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="telePhone" :rules="[{ required: true, message: '请输入电话号码', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.telePhone" 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="time" :rules="[{ required: false, message: '请选择时间', trigger: 'change' }]">
							<el-date-picker
								v-model="state.ruleForm.time"
								type="datetimerange"
								range-separator="至"
								start-placeholder="开始时间"
								end-placeholder="结束时间"
								value-format="YYYY-MM-DD HH:mm:ss"
                @change="(val:any) => {
                    state.ruleForm.setTime = val[0];
                    state.ruleForm.removeTime = val[1]
                  }"
							/>
						</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: true, message: '请填写备注', trigger: 'blur' }]">
              <el-input
                  type="textarea"
                  :autosize="{ minRows: 6, maxRows: 10 }"
                  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(blacklistFormRef)">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

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

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

// 定义变量内容

const state = reactive<any>({
	ruleForm: {
    telePhone: '',  // 电话号码
    time:[],  // 时间
    setTime: '',  // 开始时间
    removeTime: '', // 结束时间
	},
	dialogVisible: false, // 弹窗显示隐藏
});
// 打开弹窗
const blacklistFormRef = ref<RefType>();
const openDialog = () => {
	blacklistFormRef.value?.resetFields();
	state.dialogVisible = true;
	emit('openDialog');
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
	emit('closeDialog');
};
// 新增保存
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: boolean) => {
    if(!valid) return;
    Reflect.deleteProperty(state.ruleForm, 'time');
    blacklistAdd(state.ruleForm).then(() => {
      ElMessage.success('新增成功');
      emit('updateList');
      state.dialogVisible = false;
    });
	});
};
//暴漏变量和方法
defineExpose({ closeDialog, openDialog });
</script>
