<template>
  <el-dialog
    :visible="visible"
    custom-class="vehicle-dialog"
    :title="isEdit ? 'EDIT MODEL' : 'ADD MODEL'"
    :before-close="hideDialog"
    :show-close="false"
    :close-on-click-modal="false">
    <el-form
      ref="vicForm"
      :model="form"
      :rules="rules"
      label-position="top"
      class="vehicle-dialog-form">
      <el-form-item
        label="Brand:"
        prop="name">
        <el-autocomplete
          class="add-input"
          v-model="form.brand"
          :trigger-on-focus="false"
          :fetch-suggestions="getBrandOptions"
          @select="onChangeBrand">
          <template slot-scope="{ item }">
            <div class="name">{{ item }}</div>
          </template>
        </el-autocomplete>
      </el-form-item>
      <el-form-item
        label="Model:"
        prop="name">
        <el-input
          class="add-input"
          v-model="form.model"
          maxlength="20"/>
      </el-form-item>
      <div class="flexcc" style="margin-top: 30px;">
        <el-button
          class="cancel-button"
          @click="hideDialog">
          BACK
        </el-button>
        <el-button
          type="primary"
          @click="onSave"
          :loading="loading">
          SAVE
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import api from '../../api/vehicles.js';
export default {
  name: "DialogVehicleModel",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      isEdit: false,
      loading: false,
      form: {
        vehicleModelId: "",
        brand: "",
        model: ""
      },
      rules: {
        
      }
    };
  },
  watch: {
    visible(news, old) {
      if (news) {
        this.init();
        this.isEdit = this.item.vehicleModelId ? true : false;
        if (this.isEdit) {
          this.getModelInfo()
        }
        this.$nextTick(() => {
          this.$refs.vicForm.clearValidate();
        })
      }
    }
  },
  mounted() {
    
  },
  methods: {
    init() {
      this.form = {
        vehicleModelId: "",
        brand: "",
        model: ""
      }
      this.loading = false;
    },
    hideDialog(e, r) {
      this.$emit("hide", r)
    },
    getBrandOptions(query, cb) {
      if (query) {
        api.getBrandsOptions(query).then(res => {
          cb(res.data)
        }).catch(err => {
          this.$message({
            message: error,
            type: 'error'
          })
          cb([])
        })
      } else {
        cb([])
      }
    },
    onChangeBrand(content) {
      console.log("选择品牌", content);
      this.form.brand = content;
    },
    getModelInfo() {
      this.form.vehicleModelId = this.item.vehicleModelId;
      this.form.brand = this.item.brand;
      this.form.model = this.item.model;
    },
    onSave() {
      this.$refs['vicForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.isEdit ? this.updateModel() : this.createModel();
        }
      })
    },
    createModel() {
      api.addVehicleModel(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Add successfully"
        });
        this.hideDialog(true, true);
      }).catch(err => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      });
    },
    updateModel() {
      api.updateVehicleModel(this.form).then(res => {
        this.$message({
          type: 'success',
          message: "Update successfully"
        });
        this.hideDialog(true, true);
      }).catch(err => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      });
    }
  }
}
</script>

<style>
.vehicle-dialog {
  width: 100%;
  max-width: 450px;
}

.vehicle-dialog .el-dialog__body {
  padding: 10px 20px 30px;
}
.vehicle-dialog .add-input {
  width: 100%;
  min-width: 150px;
}
.vehicle-dialog-form .el-form-item {
  margin-bottom: 10px;
}
.vehicle-dialog-form .el-form-item .el-form-item__label {
  padding: 0;
  font-size: 13px;
}
</style>