
<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="type">
          <el-select v-model="formModel.type" filterable placeholder="请选择类型" style="width: 300px">
            <el-option v-for="typeClass in typeClassResult" :key="typeClass.value" :label="typeClass.name" :value="typeClass.id"></el-option>
          </el-select>
        </el-form-item>
        <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="name">
          <el-input
            v-model="formModel.name"
            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-item label="售价" prop="price">
          <el-input-number v-model="formModel.price" :step="1" :min="0" step-strictly></el-input-number>
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-input
            v-model="formModel.tag"
            placeholder="请输入标签"
            style="width: 300px"
          ></el-input>
        </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 dishesApi from "@/api/base/dishes";
import areaApi from "@/api/base/area";
import SelectTree from "@/components/SelectTree";
import dataDictionaryApi from "@/api/sys/dataDictionary";
import { getToken } from "@/utils/auth";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        areaId: [{ required: true, message: "供应区域不能为空", trigger: "blur" }],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        url: [{ required: true, message: "图片不能为空", trigger: "blur" }],
        price: [{ required: true, message: "售价不能为空", trigger: "blur" }],
        tag: [{ required: true, message: "标签不能为空", trigger: "blur" }],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      props: {
        value: "id",
        label: "name",
        children: "children"
      },
      queryAreaResult: [],
      typeClassResult: [],
      //上传图片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 dishesApi.add(self.formModel);
            } else {
              return dishesApi.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;
    });

    dataDictionaryApi.findByCatalogName({
      catalogName: "菜品分类",
    })
    .then((response) => {
      var jsonData = response.data;
      this.typeClassResult = jsonData.data;
    });

    (function () {
      if (self.businessKey.length == 0) {
        return dishesApi.create();
      } else {
        return dishesApi.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>