<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog :visible.sync="showIngredientDetail" :title="title" append-to-body 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="ingredientId">
          <el-select-tree size="mediumn" style="width: 300px" :props="props" :options="queryIngredientResult" v-model="formModel.ingredientId" height="200"></el-select-tree>
        </el-form-item>
        <el-form-item label="用量" prop="dosage">
          <el-input-number v-model="formModel.dosage" :step="1" :min="0" step-strictly></el-input-number>&nbsp;&nbsp;克
        </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 dishesIngredientApi from "@/api/base/dishesIngredient";
import SelectTree from "@/components/SelectTree";
import ingredientApi from "@/api/base/ingredient";

export default {
  props: ["ingredientKey", "dishesKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        ingredientId: [{ required: true, message: "原材料不能为空", trigger: "blur" }],
        dosage: [{ required: true, message: "用量不能为空", trigger: "blur" }],
      },
      showIngredientDetail: true,
      loading: false,
      submitting: false,
      queryIngredientResult: [],
      props: {
        value: "id",
        label: "name"
      },
    };
  },
  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 dishesIngredientApi.add(self.formModel);
            } else {
              return dishesIngredientApi.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);
            }
          });
        }
      });
    },
  },
  mounted: function () {
    var self = this;

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

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

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