
<template>
    <el-dialog
      :visible.sync="showDialog"
      :title="title"
      :modal-append-to-body="false"
      style="text-align: left;"
      @close="closeDialog"
      :close-on-click-modal="false"
      width="900px"
    >
      <div class="user-panel" v-loading="loading">
        <el-form
          ref="form"
          :model="formModel"
          :rules="ruleValidate"
          :label-width="'100px'"
        >
        <el-form-item label="数据代码" prop="code">
            <el-input v-model="formModel.code" placeholder="请输入数据代码" style="width: 300px"></el-input>
          </el-form-item>
        <el-form-item label="标题" prop="title">
            <el-input v-model="formModel.title" placeholder="请输入标题" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="内容类型" prop="contentType">
            <el-select v-model="formModel.contentType" placeholder="请选择" style="width: 300px">
              <el-option
                v-for="item in contentTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input v-if="formModel.contentType=='1'" v-model="formModel.content"  placeholder="请输入内容" style="width: 300px"></el-input>
            <mce-editor
                v-if="formModel.contentType=='2'"
                ref="editor"
                uploadName="upfile"
                v-model="formModel.content"
                :url="uploadUrl1"
                :config="editorConfig"
                style="width: 90%"
            ></mce-editor>
            <el-upload
              v-if="formModel.contentType=='3'"
              style="width:300px"
              class="avatar-uploader"
              name="photoFile"
              :action="uploadUrl"
              :headers="headers"
              :data="uploadData"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" 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 aboutUsApi from "@/api/base/aboutUs";
  import MceEditor from "@/components/Tinymce";
  
  export default {
    props: ["businessKey", "title"],
    data() {
      return {
          showPwd: true,
          ruleValidate: {
              code: [
              { required: true, message: "数据代码不能为空", trigger: "blur" },
              ],
              title: [
              { required: true, message: "标题不能为空", trigger: "blur" },
              ],
              contentType: [
              { required: true, message: "内容类型不能为空", trigger: "blur" },
              ],
          },
          roleList: [],
          formModel: {},
          showDialog: true,
          loading: false,
          submitting: false,
          enterpriseList:[],
          //上传地址
          uploadUrl: Constant.serverUrl + "/uploadPicture",
          uploadData: {
              subFolder: "water-tank",
          },
          fileUrl: "",
          headers: {
              Authorization: getToken(),
          },
          uploadUrl1: Constant.serverUrl + "/tinymce/upload?token=" + getToken(),
          editorConfig: {
            height: 300,
            width: 660,
          },
          contentTypeList:[
            {
              "label":"文本",
              "value":"1"
            },
            {
              "label":"富文本",
              "value":"2"
            },
            {
              "label":"图片",
              "value":"3"
            }
          ],
          imageUrl: ''
      };
    },
    created() {
    },
    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 aboutUsApi.add(self.formModel);
              } else {
                return aboutUsApi.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.content = res.data;
  
          self.imageUrl =
            res.data + "?x-oss-process=image/resize,m_lfit,h_300,w_300";
      },
      beforeAvatarUpload(file) {
          const isLt2M = file.size / 1024 / 1024 < 2;
          if (!isLt2M) {
              this.$message.error('上传头像图片大小不能超过 2MB!');
          }
          return isLt2M;
      }
    },
    async mounted() {
      var self = this;
      self.loading = true;
  
      (function () {
        if (self.businessKey != null && self.businessKey.length > 0) {
          return aboutUsApi.edit(self.businessKey);
        } else {
          return aboutUsApi.create();
        }
      })()
        .then((response) => {
          self.loading = false;
          var jsonData = response.data;
  
          if (jsonData.result) {
              self.formModel = jsonData.data;

             if(self.formModel.contentType=='3'){
                if (self.formModel.content != null) {
                  self.imageUrl = self.formModel.content;
                }
              }
          } else {
            self.$message.error(jsonData.message + "");
          }

        })
        .catch((error) => {
          self.$message.error(error + "");
          self.loading = false;
        });
    },
    components: {
        "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>