
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog :visible.sync="showDialog" :title="title" :modal-append-to-body="false" style="text-align: left" @close="closeDialog" :close-on-click-modal="false">
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="所属区域" prop="areaId">
          <el-select-tree size="mediumn" style="width: 300px" :props="props" :options="queryAreaResult" v-model="formModel.areaId" height="200"></el-select-tree>
        </el-form-item>
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="formModel.licensePlate" placeholder="请输入车牌号" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="车辆图片" prop="url">
          <el-upload
            name="photoFile"
            class="avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            :headers="headers"
            :data="uploadData"
            :on-success="iconHandleAvatarSuccess"
            :before-upload="iconBeforeAvatarUpload"
            :auto-upload="true"
            accept="image/png,image/jpeg"
            list-type="picture-card">
            <img v-if="formModel.url" :src="formModel.url" class="avatar" style="width:144px; height:144px;">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import vehicleApi from "@/api/base/vehicle";
import areaApi from "@/api/base/area";
import SelectTree from "@/components/SelectTree";
import { getToken } from "@/utils/auth";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        areaId: [{ required: true, message: "所属区域不能为空", trigger: "blur" }],
        licensePlate: [{ required: true, message: "车牌号不能为空", trigger: "blur" }],
        url: [{ required: true, message: "车辆图片不能为空", trigger: "blur" }],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      props: {
        value: "id",
        label: "name",
        children: "children"
      },
      queryAreaResult: [],
      //上传图片start
      uploadImgUrl: Constant.serverUrl + "/uploadPicture",
      uploadData: {
        subFolder: "manage",
      },
      headers: {
        Authorization: getToken(),
      },
      //上传图片end
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return vehicleApi.add(self.formModel);
            } else {
              return vehicleApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
    iconHandleAvatarSuccess(res, file) {
      this.loading = false;
      var self = this;

      self.formModel.url = res.data.fileUrl;
    },
    iconBeforeAvatarUpload(file) {
      this.loading = true;
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
        this.loading = false;
      }
      return isLt2M;
    },
  },
  mounted: function () {
    var self = this;

    var formData = new FormData();
    areaApi.queryList(formData).then(response=>{
      var jsonData = response.data;
      this.queryAreaResult = jsonData;
    });

    (function () {
      if (self.businessKey.length == 0) {
        return vehicleApi.create();
      } else {
        return vehicleApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>