<template>
  <a-modal 
  :title=title
  :width="640" 
  :visible="visible" 
  :confirmLoading="loading" 
  cancelText="取消"
  okText="保存"
  @ok="() => { $emit('ok') }" 
  @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item label="名称">
          <a-input v-decorator="['Name', {rules: [{required: true, message: '必填项不能为空！'}]}]" />
        </a-form-item>
        <a-form-item label="机器码">
          <a-input v-decorator="['ecode', {rules: [{required: true, message: '必填项不能为空！'}]}]" />
        </a-form-item>
        <a-form-item label="序列">
          <a-input v-decorator="['FacEcode', {rules: [{required: true, message: '必填项不能为空！'}]}]" />
        </a-form-item>
        <a-form-item label="类型">
          <a-select placeholder="请选择" v-decorator="['Type', {initialValue: '1', rules: [{required: true}]}]">
            <a-select-option value="1">小白WIFI秤</a-select-option>
            <a-select-option value="2">小白4G秤</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'

  // 表单字段
  const fields = ['Name', 'id', 'ecode', 'FacEcode', 'Type']

  export default {
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      model: {
        type: Object,
        default: () => null
      }
    },
    data() {
      this.formLayout = {
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 7
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 13
          }
        }
      }
      return {
        title: '添加设备',
        form: this.$form.createForm(this)
      }
    },
    created() {
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.$watch('model', () => {
        if(this.model){
          console.log(this.model)
          this.title = '编辑设备'
          // this.model.Type = 1?'小白WIFI秤':'小白4G秤'
          this.model && this.form.setFieldsValue(pick(this.model, fields))
        }else{
          this.title = '添加设备'
        }
       
      })
    }
  }
</script>
