<template>
  <a-modal 
  title="打标签"
  :width="640" 
  :visible="visible" 
  :confirmLoading="loading" 
  okText="确定"
  cancelText="取消"
  @ok="() => { $emit('ok') }" 
  @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['key', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="标签">
          <a-select placeholder="请选择" v-decorator="['Type', {initialValue: '1', rules: [{required: true}]}]">
           <a-select-option v-for="i in tagList" :key="i.key">
             {{ i.name }}
           </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'

  // 表单字段
  const fields = [ 'key','Type']
  import tag from '@/static/tag.json'
  export default {
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      model: {
        type: Object,
        default: () => null
      }
    },
    data() {
      tagList:[]
      this.formLayout = {
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 7
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 13
          }
        }
      }
      return {
        form: this.$form.createForm(this)
      }
    },
    created() {
      this.tagList= tag.data
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.$watch('model', () => {
        if(this.model){
          console.log(this.model)
          this.model && this.form.setFieldsValue(pick(this.model, fields))
        }
       
      })
    }
  }
</script>
