<template>
  <a-drawer
    title="修改域信息"
    :maskClosable="false"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="domainEditVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='域名称' v-bind="formItemLayout">
        <a-input v-decorator="['domainName',
                   {rules: [
                    { required: true, message: '域名称不能为空'},
                    { max: 20, message: '长度不能超过20个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='域编号' v-bind="formItemLayout">
        <a-input-number v-decorator="['orderNum']" style="width: 100%"/>
      </a-form-item>
      <a-form-item label='域描述' v-bind="formItemLayout">
        <a-input v-decorator="['domainId']" style="width: 100%"/>
      </a-form-item>
    </a-form>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-drawer>
</template>
<script>
const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'DomainEdit',
  props: {
    domainEditVisible: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      domain: {}
    }
  },
  methods: {
    reset () {
      this.loading = false
      this.button = {}
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleCheck (checkedKeys) {
      this.checkedKeys = checkedKeys
    },
    handleExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
    },
    setFormValues ({...domain}) {
      this.form.getFieldDecorator('domainName')
      this.form.setFieldsValue({'domainName': domain.name})
      this.form.getFieldDecorator('orderNum')
      this.form.setFieldsValue({'orderNum': domain.order})
      this.form.getFieldDecorator('domainId')
      this.form.setFieldsValue({'domainId': domain.domainId})
      this.domain.domainId = domain.domainId
    },
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          let domain = this.form.getFieldsValue()
          domain.domainId = this.domain.domainId
          this.$put('domains', {
            ...domain
          }).then(() => {
            this.reset()
            this.$emit('success')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  },
  watch: {
    domainEditVisible () {
      if (this.domainEditVisible) {
        this.$get('menu').then((r) => {
          this.allTreeKeys = r.data.ids
          this.$get('role/menu/' + this.roleInfoData.roleId).then((r) => {
            this.defaultCheckedKeys.splice(0, this.defaultCheckedKeys.length, r.data)
            this.checkedKeys = r.data
            this.expandedKeys = r.data
          })
        })
      }
    }
  }
}
</script>
