<template>
  <a-modal
    title="代码项编辑"
    v-model="visible"
    @ok="handleOk"
    :maskClosable="false"
    class="cust-modal"
  >
    <a-form :form="form" ref="form">
      <a-form-item label='名称' :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
        <a-input v-decorator="['name',{rules: [{ required: true, message: '请输入名称' }]}]"/>
      </a-form-item>
      <a-form-item label='值' :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
        <a-input-number style="width: 100%" v-decorator="['valueInt',{rules: [{ required: false }]}]"/>
      </a-form-item>
      <a-form-item label='字符串值' :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
        <a-input style="width: 100%" v-decorator="['valueStr',{rules: [{ required: false }]}]"/>
      </a-form-item>
      <a-form-item label='最小值' :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
        <a-input-number style="width: 100%" v-decorator="['valueMin',{rules: [{ required: false }]}]"/>
      </a-form-item>
      <a-form-item label='最大值' :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
        <a-input-number style="width: 100%" v-decorator="['valueMax',{rules: [{ required: false }]}]"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
  import pick from 'lodash.pick'

  let initData = {
    name: '',
    valueInt: '',
    valueStr: '',
    valueMin: '',
    valueMax: '',
  }
  let propertyList = mapKeyToList(initData);

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