<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
       <a-form-model-item label="类别" prop="ipType" >
        <a-radio-group  button-style="solid" v-model="form.ipType"  >
          <a-radio-button value="1" :disabled="white">白名单</a-radio-button>
          <a-radio-button value="2" :disabled="!white">黑名单</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="IP地址" prop="ipAddr" >
        <a-input v-model="form.ipAddr" placeholder="请输入IP地址" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入备注" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getIp, addIp, updateIp } from '@/api/security/ip'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      loading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,
        ipType: null,
        ipAddr: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      white: true,
      rules: {
        ipAddr: [{ required: true, message: 'IP地址不能为空', trigger: 'blur' },
               { validator: this.checkIP, message: 'IP地址格式不正确', trigger: 'blur' }
        ],
         remark: [{ max: 100, message: '最多可输入100个字', trigger: 'blur' }]
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,
        ipType: null,
        ipAddr: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
    },
    checkIP (rule, value, callback) {
      const ipRules = /^((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))$/
      const pwdRegex = new RegExp(ipRules)
         if (!pwdRegex.test(value)) {
        callback(new Error('IP地址输入格式有误'))
      }
       callback()
    },

    /** 新增按钮操作 */
    handleAdd (type) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
      this.form.ipType = type
      if(type=='1'){
        this.white = false
      }else{
        this.white = true
      }
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getIp(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id !== undefined && this.form.id !== null) {
            updateIp(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          } else {
            addIp(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
