'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var elementPlus = require('element-plus');
var index = require('../../svg-icon/index.js');
var FileUploadRule = require('../../utils/upload/FileUploadRule.js');

"use strict";
const _hoisted_1 = { class: "me-upload-file__text" };
const _hoisted_2 = ["accept"];
const _hoisted_3 = { class: "me-upload-file__tip" };
const __default__ = vue.defineComponent({
  name: "me-upload-file-drag"
});
var script = /* @__PURE__ */ vue.defineComponent({
  ...__default__,
  props: {
    rules: { type: Array, required: true },
    uploadRequest: { type: Function, required: true },
    icon: { type: String, required: false, default: "ri-arrow-up-circle-line" },
    url: { type: String, required: true }
  },
  emits: ["uploadSuccess"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const props = __props;
    const dragover = vue.ref(false);
    const inputRef = vue.ref();
    const progress = vue.ref(0);
    const tips = vue.ref();
    const accept = vue.ref();
    const fileUploadRule = new FileUploadRule.default();
    fileUploadRule.setChangeHandler(() => {
      tips.value = fileUploadRule.getUploadTips();
      accept.value = fileUploadRule.getAccept();
    });
    props.rules.forEach((item) => {
      fileUploadRule.add(item.name, item.size);
    });
    const handleUpload = (file) => {
      if (!fileUploadRule.check(file)) {
        elementPlus.ElMessage.error(fileUploadRule.getErrorMessage());
        return;
      }
      const formData = new FormData();
      formData.append("file", file);
      props.uploadRequest({
        url: props.url,
        method: "POST",
        data: formData,
        onUploadProgress: function(progressEvent) {
          progress.value = Math.round(progressEvent.loaded / progressEvent.total * 100);
        }
      }).then((res) => {
        emits("uploadSuccess", res);
      });
    };
    const inputClick = () => {
      inputRef.value?.click();
    };
    const handleInputChange = (e) => {
      const target = e.target;
      const selectedFile = target.files?.[0];
      console.log("\u{1F680} ~ handleInputChange ~ selectedFile:", selectedFile);
      selectedFile && handleUpload(selectedFile);
    };
    const onDrop = async (e) => {
      dragover.value = false;
      e.stopPropagation();
      const files = Array.from(e.dataTransfer.files);
      if (files.length > 1) {
        elementPlus.ElMessage.error("\u53EA\u80FD\u9009\u62E9\u4E00\u4E2A\u6587\u4EF6");
        return;
      }
      const uploadFile = files[0];
      handleUpload(uploadFile);
    };
    const onDragover = () => {
      dragover.value = true;
    };
    const onDragleave = (e) => {
      console.log("\u{1F680} ~ onDragleave ~ e:", e);
      if (!e.currentTarget.contains(e.relatedTarget)) dragover.value = false;
    };
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock(
        vue.Fragment,
        null,
        [
          vue.createElementVNode(
            "div",
            {
              class: vue.normalizeClass(["me-upload-file-dragger is-drag", { "is-dragover": dragover.value }]),
              onClick: inputClick,
              onDrop: vue.withModifiers(onDrop, ["prevent"]),
              onDragover: vue.withModifiers(onDragover, ["prevent"]),
              onDragleave: vue.withModifiers(onDragleave, ["prevent"])
            },
            [
              vue.createVNode(vue.unref(index.MeSvgIcon), {
                size: 24,
                name: __props.icon
              }, null, 8, ["name"]),
              vue.createElementVNode("div", _hoisted_1, [
                vue.renderSlot(_ctx.$slots, "tip", {}, () => [
                  _cache[0] || (_cache[0] = vue.createTextVNode(
                    "\u62D6\u62FD\u6587\u4EF6\u4E0A\u4F20 \u6216 ",
                    -1
                    /* CACHED */
                  )),
                  _cache[1] || (_cache[1] = vue.createElementVNode(
                    "em",
                    null,
                    "\u70B9\u51FB\u4E0A\u4F20",
                    -1
                    /* CACHED */
                  ))
                ])
              ]),
              vue.createElementVNode("input", {
                ref_key: "inputRef",
                ref: inputRef,
                onChange: handleInputChange,
                accept: accept.value,
                type: "file",
                style: { "display": "none" }
              }, null, 40, _hoisted_2)
            ],
            34
            /* CLASS, NEED_HYDRATION */
          ),
          vue.createElementVNode("div", _hoisted_3, [
            (vue.openBlock(true), vue.createElementBlock(
              vue.Fragment,
              null,
              vue.renderList(tips.value, (item) => {
                return vue.openBlock(), vue.createElementBlock(
                  "span",
                  {
                    key: item.name
                  },
                  " \u4E0A\u4F20\u6587\u4EF6\u652F\u6301 " + vue.toDisplayString(item.name) + " \u683C\u5F0F\uFF0C \u5E76\u4E14\u6587\u4EF6\u5927\u5C0F\u4E0D\u80FD\u5927\u4E8E " + vue.toDisplayString(item.size),
                  1
                  /* TEXT */
                );
              }),
              128
              /* KEYED_FRAGMENT */
            ))
          ]),
          progress.value > 0 && progress.value < 100 ? (vue.openBlock(), vue.createBlock(vue.unref(elementPlus.ElProgress), {
            key: 0,
            percentage: progress.value,
            "stroke-width": 4,
            striped: "",
            "striped-flow": ""
          }, null, 8, ["percentage"])) : vue.createCommentVNode("v-if", true)
        ],
        64
        /* STABLE_FRAGMENT */
      );
    };
  }
});

exports.default = script;
//# sourceMappingURL=upload-file-drag.vue2.js.map
