<template>

  <el-dialog :modal-append-to-body="false" :title="title" v-bind="$attrs" width="500px"
             @close="onClose"
             @open="onOpen"
             v-on="$listeners"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.whiteListName" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="电话" prop="phoneNumber">
        <el-input v-model="form.phoneNumber" placeholder="请输入电话"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitAddForm()">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addConfig, updateConfig } from '@/api/system/config'

export default {
  name: 'EditWhiteList',
  dicts: ['sys_yes_no'],

  props: {
    // 图片数量限制
    title: {
      type: String,
      default: '添加白名单'
    }
  },
  data() {
    return {
      configName: undefined,
      form: {},
      // 白名单配置表单验证
      rules: {
        whiteListName: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        whiteListType: [
          { required: true, message: '白名单类型不能为空', trigger: 'change' }
        ]
      }
    }

  },
  methods: {
    /** 提交按钮 */
    submitAddForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.configId != undefined) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.closeModal()
            })
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.closeModal()
            })
          }
        }
      })
    },
    reset() {
      this.form = {
        whiteListType: undefined,
        whiteListName: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.closeModal()
    },
    closeModal() {
      this.$emit('close')
    },
    onOpen() {
      this.active = this.current
      this.key = ''
    },
    onClose() {
    }
  }
}
</script>
