<template>
  <div>
      <el-dialog
        v-dialogDrag
        :visible.sync="showModal"
        :close-on-click-modal="false"
        :title="modalTitle"
      >
      <div>
        <Form
          id="edit-form-black"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="90"
          inline
        >
          <Form-item label="封杀ip地址" prop="ip" class="width-48">
            <el-input v-model="formObj.ip" name="ip" placeholder="请输入ip" />
          </Form-item>
          <Form-item label="封杀用户id" prop="userId" class="width-48">
            <el-input v-model="formObj.userId" name="userId" placeholder="请输入用户id" />
          </Form-item>
          <Form-item label="状态" prop="status" class="width-48">
            <el-select v-model="formObj.status" style="width: 100%">
              <el-option v-for="item in statusList" :key="item.value" :value="item.value">
                {{ item.name }}
              </el-option>
            </el-select>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
      </el-dialog>
  </div>
</template>
<script>
import { blackList } from '../../../api'
export default {
  data() {
    return {
      id: '',
      modalTitle: '',
      formObj: {
        ip: '',
        userId: '',
        status: 0
      },
      ruleValidate: {
        status: [
          {
            required: true,
            message: '状态不能为空',
            trigger: 'blur',
            type: 'number'
          }
        ]
      },
      showModal: false,
      statusList: [{ value: 0, name: '封号' }, { value: 1, name: '解封' }],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _resultChange(result, msg) {
      if (result) {
        this.showModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this.$refs.footerModal._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form-black')
          data.status = this.formObj.status
          this._save(data)
        } else {
          this.$refs.footerModal._hideLoading()
          this.$message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await blackList.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const result = await blackList.edit({ id: this.formObj.id, obj: data })
        this._resultChange(result, '编辑成功!')
      }
    },
    _cancel() {
      this.$refs.footerModal._hideLoading()
      this.showModal = false
    },
    _open(formObj) {
      this.showModal = true
      this.$nextTick(() => {
        this.$refs.footerModal._hideLoading()
        this.$refs.formObj.resetFields()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加黑名单信息'
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑黑名单信息'
        }
      })
    }
  }
}
</script>
