<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="false">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="车牌号" prop="license">
            <Input v-model="formObj.license" name="license" placeholder="请输入车牌号" /></Input>
          </Form-item>
          <Form-item label="品牌" prop="brand">
            <Input v-model="formObj.brand" name="brand" placeholder="请输入品牌" /></Input>
          </Form-item>
          <Form-item label="所属类型" prop="type">
            <el-select v-model="formObj.type" filterable clearable size="small" style="width:100%">
              <el-option v-for="item in typeList" :key="item.name" :value="item.name">
                {{ item.name }}
              </el-option>
            </el-select>
          </Form-item>
          <Form-item label="购买/租用时间" prop="buyDate">
            <Date-picker
              v-model="formObj.buyDate"
              type="date"
              placeholder="购买/租用时间"
              format="yyyy-MM-dd"
              :editable="false"
              style="width: 100%;"
              @on-change="_paymentTimeChange"
            />
            <input name="buyDate" type="hidden">
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注" /></Input>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { resDict, car } from '../../../api'

export default {
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        license: '',
        brand: '',
        type: '',
        buyDate: '',
        remark: ''
      },
      typeList: [],
      ruleValidate: {
        license: [
          { required: true, message: '车牌号不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          data.type = this.formObj.type
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await car.add(data)
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await car.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _paymentTimeChange(data) {
      $('input[name="buyDate"]').val(data)
    },
    // 查询字典
    _dicSearch: async function() {
      const result = await resDict.getItem('车辆所属类型')
      this.typeList = result
    },
    _open(formObj) {
      this.$refs.formObj.resetFields()
      this.showEditModal = true
      this._hideLoading()
      this._dicSearch()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加物料类型'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑物料类型'
        if (formObj.buyDate === undefined) {
          this.formObj.buyDate = ''
        } else {
          this.formObj.buyDate = new Date(formObj.buyDate)
        }
      }
    }
  }
}
</script>
