
<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="title">
            <el-input
              v-model="formModel.title"
              placeholder="请输入标题"
              style="width: 90%"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="contacts">
            <el-input
              v-model="formModel.contacts"
              placeholder="请输入标题"
              style="width: 90%"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input
              v-model="formModel.contactPhone"
              placeholder="请输入标题"
              style="width: 90%"
            ></el-input>
          </el-form-item>
          <el-form-item label="回馈方式" prop="type">
              <el-select v-model="formModel.feedbackMethodArray" multiple filterable  placeholder="请选择" style="width: 90%">
                  <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                  </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="内容" prop="content">
              <mce-editor
                  ref="editor"
                  uploadName="upfile"
                  v-model="formModel.content"
                  :url="uploadUrl1"
                  style="width: 90%"
              ></mce-editor>
          </el-form-item>
          <el-form-item label="图片" prop="image">
            <el-upload
                style="width:300px"
                class="avatar-uploader"
                name="photoFile"
                :action="uploadUrl"
                :headers="headers"
                :data="uploadData"
                :show-file-list="false"
                :on-success="handleAvatarSuccess">
                <img v-if="fileUrl" :src="fileUrl" class="avatar">
                <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 { getToken } from "@/utils/auth"; // get token from cookie
  import MceEditor from "@/components/Tinymce";
  import loveProjectApi from "@/api/base/loveProject";
  import dataDictionaryApi from "@/api/sys/dataDictionary";
  import feedbackMethodApi from "@/api/base/feedbackMethod";
  
  export default {
    props: ["businessKey", "title"],
    data() {
      return {
        showPwd: true,
        ruleValidate: {
          category: [
            { required: true, message: "类型不能为空", trigger: "blur" },
          ],
          title: [
            { required: true, message: "标题不能为空", trigger: "blur" },
          ],
          amount: [
            { required: true, message: "数量不能为空", trigger: "blur" },
          ],
          studentName: [
            { required: true, message: "学生姓名不能为空", trigger: "blur" },
          ],
          studentType: [
            { required: true, message: "学生类型不能为空", trigger: "blur" },
          ],
        },
        roleList: [],
        formModel: {},
        showDialog: true,
        loading: false,
        submitting: false,
        categoryList:[],
        typeList:[],
        //上传地址
        uploadUrl: Constant.serverUrl + "/uploadPicture",
        uploadData: {
            subFolder: "love-donation",
        },
        fileUrl: "",
        fileUrl1: "",
        headers: {
            Authorization: getToken(),
        },
        uploadUrl1: Constant.serverUrl + "/tinymce/upload?token=" + getToken(),
      };
    },
    created() {
      dataDictionaryApi.findByCatalogName({catalogName: "心愿类型",})
        .then((response) => {
            var jsonData = response.data;
            this.categoryList = jsonData.data;
      });

      feedbackMethodApi.list()
        .then((response) => {
            var jsonData = response.data;
            this.typeList = jsonData.data;
      });
      
    },
    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 loveProjectApi.add(self.formModel);
              } else {
                self.formModel.password = null;
                return loveProjectApi.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);
              }
            });
          }
        });
      },
      handleAvatarSuccess(res, file) {
          var self = this;
          self.formModel.image = res.data;
  
          self.fileUrl =
            res.data + "?x-oss-process=image/resize,m_lfit,h_300,w_300";
      },
    },
    async mounted() {
      var self = this;
      self.loading = true;
  
      (function () {
        if (self.businessKey != null && self.businessKey.length > 0) {
          return loveProjectApi.edit(self.businessKey);
        } else {
          return loveProjectApi.create();
        }
      })()
        .then((response) => {
          var jsonData = response.data;
  
          if (jsonData.result) {
            self.formModel = jsonData.data;
            self.showModal = true;
  
            let image = self.formModel.image;
              if (image != null) {
                  self.fileUrl = image 
              }
  
            if (self.formModel.content != null) {
              self.$refs.editor.setContent(self.formModel.content);
            }
  
          } else {
            self.$message.error(jsonData.message + "");
          }
  
          self.loading = false;
        })
        .catch((error) => {
          self.$message.error(error + "");
          self.loading = false;
        });
    },
    components: {
      // "el-select-tree": SelectTree
      "mce-editor": MceEditor,
    },
  };
  </script>
  <style scoped>
  .user-panel {
    margin: 10px auto;
  }
  .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  </style>