<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="车牌号" prop="plateNumber" >
        <a-input v-model="form.plateNumber" placeholder="请输入车牌号" />
      </a-form-model-item>
      <a-form-model-item label="钥匙柜状态" prop="status" >
      </a-form-model-item>
      <a-form-model-item label="上次绑定的车牌号" prop="prevPlateNumber" >
        <a-input v-model="form.prevPlateNumber" placeholder="请输入上次绑定的车牌号" />
      </a-form-model-item>
      <a-form-model-item label="箱子号码" prop="code" >
        <a-input v-model="form.code" placeholder="请输入箱子号码" />
      </a-form-model-item>
      <a-form-model-item label="箱子名称" prop="codeName" >
        <a-input v-model="form.codeName" placeholder="请输入箱子名称" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getKeyCase, addKeyCase, updateKeyCase } from '@/api/clw/KeyCase'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        plateNumber: null,

        status: '0',

        prevPlateNumber: null,

        code: null,

        codeName: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        plateNumber: null,

        status: '0',

        prevPlateNumber: null,

        code: null,

        codeName: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getKeyCase(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateKeyCase(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addKeyCase(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
