
<template>
  <el-dialog v-drag-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="true"
    append-to-body
    style="text-align:left;"
    width="800px"
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <!-- <el-form-item label="logo" prop="logo">
          <el-upload
                class="avatar-uploader"
                name="photoFile"
                :action="uploadUrl"
                :show-file-list="false"
                :headers="headers"
                :data="uploadData"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <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-item label="首页图" prop="homePageImg">
          <el-upload
                class="avatar-uploader"
                name="photoFile"
                :action="uploadUrl"
                :show-file-list="false"
                :headers="headers"
                :data="uploadData"
                :on-success="handleAvatarSuccess2"
                :before-upload="beforeAvatarUpload2"
              >
                <img v-if="fileUrl2" :src="fileUrl2" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
        </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="value">
            <el-input v-if="formModel.type=='text'" v-model="formModel.value" placeholder="请输入内容" style="width:300px"></el-input>
            <mce-editor
                v-if="formModel.type=='longtext'"
                ref="editor"
                :Id="editor1Id"
                uploadName="upfile"
                v-model="formModel.value"
                :url="uploadUrl1"
                :config="editorConfig"
                style="width: 90%"
            ></mce-editor>
            <el-upload
                v-if="formModel.type=='img'"
                class="avatar-uploader"
                name="photoFile"
                :action="uploadUrl"
                :show-file-list="false"
                :headers="headers"
                :data="uploadData"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                >
                <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 MceEditor from "@/components/Tinymce";
import configureApi from "@/api/sys/configure";

import { getToken } from "@/utils/auth"; // get token from cookie

export default {
  props: ["businessKey", "title"],
  computed: {
      ruleValidate (){
        var rules = null;
        rules = {
            title: [
                { required: true, message: "标题不能为空", trigger: "blur" }
            ],
            value: [
                { required: true, message: "内容不能为空", trigger: "blur" }
            ],
        };
        return rules;
      }
  },
  data() {
    return {
      editor1Id: "editor1_" + Date.now(),
      showDialog: true,
      loading: false,
      submitting: false,
      formModel: {
        id:"",
        titel:"",
        value:"",
      },
      newsTypeList: [],
      //上传地址
      uploadUrl: Constant.chargingServerUrl + "/uploadPicture",
      uploadData: {
        subFolder: "configure",
      },
      fileUrl: "",
      fileUrl2: "",
      headers: {
        Authorization: getToken(),
      },
      uploadUrl1: Constant.chargingServerUrl + "/tinymce/upload?token=" + getToken(),
      editorConfig: {
        height: 300,
        width: 660,
      },
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

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

            if(self.fileUrl!=null&&self.fileUrl!=""){
              self.formModel.images = self.fileUrl;
            }

            if (id == null || id.length == 0) {
              return configureApi.add(self.formModel);
            } else {
              return configureApi.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.value = res.data;
      self.fileUrl =
        res.data + "?x-oss-process=image/resize,m_lfit,h_300,w_300";
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  mounted: function () {
    var self = this;

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

        if (jsonData.result) {
          self.formModel = jsonData.data;

          if(self.formModel.type=='img'){
            let value = self.formModel.value;
            if (value != null) {
                self.fileUrl = value 
            }
          }

          // if(self.formModel.type=='longtext'){
          //   if (self.formModel.value != null) {
          //     self.$refs.editor.setContent(self.formModel.value);
          //   }
          // }

        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "mce-editor": MceEditor,
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.user-panel {
  margin: 10px auto;
}
.input-form-main{
    width: 300px;
}
.link-span{
    margin-right: 20px;
}
.el-form-item{
    margin-bottom: 25px;
}
 .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>