(function () {
  CKEDITOR.dialog.add("simpleupload", function (a) {
    return {
      title: "上传图片",
      minWidth: 300, //弹出框的最小宽度
      minHeight: 80, //弹出框的最小高度
      contents: [
        {
          id: "tab1",
          label: "",
          title: "",
          expand: true,
          padding: 0,
          elements: [
            {
              type: "html",
              html:
                '<input style="width:100%; outline:medium; border:none;" id="cke_custom_input_' +
                a.name +
                '" type="file" accept="image/png, image/jpeg, image/jpg">',
            },
          ],
        },
      ],
      onOk: function () {
        if (!a.config.customSavefolder) {
          var _folderWarning = a.showNotification("请设置上传文件夹", 'warning');
          setTimeout(() => {
            _folderWarning.hide();
          }, 2000);
          return;
        }
        // 点击上传按钮
        var xhr = new XMLHttpRequest();
        var inputDom = document.querySelector("#cke_custom_input_" + a.name);
        var file = inputDom.files[0]; // 获取选中的图片文件
        var formData = new FormData();
        formData.append("savefolder", a.config.customSavefolder);
        formData.append("iscover", "0");
        formData.append("isrename", "1");
        var filename = file.name;
        formData.append("file", file);
        var strArr = filename.split(".");
        filename = strArr[0];
        var suffix = strArr[strArr.length - 1];
        suffix = suffix.toLowerCase();
        if (suffix === "png" || suffix === "jpeg" || suffix === "jpg") {
          xhr.open("POST", window._config.baseImageUrl + "up/upfile", true);
          xhr.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
              var _response = JSON.parse(this.response);
              if (_response.code === 0) {
                // 显示消息弹窗
                a.showNotification(_response.msg, 'success');
                a.insertHtml("<p><img src=" + window._config.showImageUrl + _response.obj + "></p>");
              } else {
                var _rwarningMsg = a.showNotification(_response.msg, 'warning');
                setTimeout(() => {
                  _rwarningMsg.hide();
                }, 2000);
              }
            }
          };
          xhr.send(formData);
          inputDom.value = null;
        } else {
          var _warningMsg = a.showNotification("请上传png、jpeg、jpg格式的图片", 'warning');
          setTimeout(() => {
            _warningMsg.hide();
          }, 2000);
        }
      },
      onLoad: function () {
        //弹出框中默认的按钮为【确定】和【取消】，这里将【确定】按钮的文字为【上传】，鼠标悬停时显示也为上传
        // document.querySelector(
        //   ".cke_dialog_ui_button_ok > .cke_dialog_ui_button"
        // ).innerText = "上传";
        // document
        //   .querySelector(".cke_dialog_ui_button_ok")
        //   .setAttribute("title", "上传");
      },
    };
  });
})();
