<template>
  <a-modal
    title="操作资源编辑"
    v-model="visible"
    :maskClosable="false"
    class="cust-modal"
  >
    <template slot="footer">
      <a-button key="back" @click="close">返回</a-button>
      <a-button key="submit" type="primary" :disabled="loading" @click="handleOk">
        确认
      </a-button>
    </template>
    <a-form :form="form" ref="form">
      <a-form-item label='操作地址' :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
        <a-input v-decorator="['operateUrl',{rules: [{ required: true, message: '请输入操作地址' }]}]" placeholder="请输入操作地址，例如：/system/code"/>
      </a-form-item>
      <a-form-item label='请求类型' :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
        <a-input style="width: 100%" v-decorator="['operateMethod',{rules: [{ required: true, message: '请输入请求类型' }]}]" placeholder="请输入请求类型，例如：GET"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
  import pick from 'lodash.pick'

  let initData = {
    operateUrl: '',
    operateMethod: ''
  }
  let propertyList = mapKeyToList(initData);

  export default {
    name: 'SysBusinessResourceItemDetailCom',
    beforeCreate() {
      this.form = this.$form.createForm(this)
      for (let property of propertyList) {
        this.form.getFieldDecorator(property)
      }
    },
    data() {
      return {
        visible: false,
        loading: false
      }
    },
    methods: {
      show(data) {
        if (data) {
          this.form.setFieldsValue(pick(data, propertyList))
        } else {
          this.form.setFieldsValue(initData)
        }
        this.visible = true
      },
      handleOk() {
        this.loading = true;
        this.form.validateFieldsAndScroll((err, values) => {
          if (!err) {
            this.visible = false;
            this.$emit('modifyItem', values)
          }
          this.loading = false;
        })
      },
      close(){
          this.visible = false
          this.loading = false;
      }
    }
  }
</script>