import "../../../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                                */
/* empty css                                                                       */
/* empty css                                                                     */
import { ref, watch, createBlock, openBlock, mergeProps, toHandlers, withCtx, createVNode, createElementVNode, unref, createTextVNode } from "vue";
import { UploadFilled as upload_filled_default } from "../../../../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
/* empty css             */
import _export_sfc from "../../../../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElUpload } from "../../../../../../node_modules/element-plus/es/components/upload/index.mjs";
import { ElIcon } from "../../../../../../node_modules/element-plus/es/components/icon/index.mjs";
import { ElMessage } from "../../../../../../node_modules/element-plus/es/components/message/index.mjs";
const _sfc_main = /* @__PURE__ */ Object.assign({
  inheritAttrs: false
}, {
  __name: "Upload",
  props: {
    config: {
      type: Object,
      required: true
    },
    modelValue: {
      type: Array,
      default: () => []
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    uploadUrl: {
      type: String,
      default: ""
      // TODO: 设置默认上传地址
    },
    headers: {
      type: Object,
      default: () => ({})
    },
    uploadData: {
      type: Object,
      default: () => ({})
    },
    uploadName: {
      type: String,
      default: "file"
    },
    multiple: {
      type: Boolean,
      default: false
    },
    limit: {
      type: Number,
      default: 1
    },
    accept: {
      type: String,
      default: ""
    },
    maxSize: {
      type: Number,
      default: 10
      // MB
    },
    autoUpload: {
      type: Boolean,
      default: true
    }
  },
  emits: ["update:modelValue", "change", "blur"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const fileList = ref([]);
    watch(() => props.modelValue, (newVal) => {
      if (newVal !== fileList.value) {
        fileList.value = newVal || [];
      }
    }, { immediate: true });
    watch(fileList, (newVal) => {
      emit("update:modelValue", newVal);
      emit("change", newVal);
    });
    const handlePreview = (file) => {
      console.log("预览文件:", file);
    };
    const handleRemove = (file) => {
      console.log("删除文件:", file);
    };
    const handleSuccess = (response, file) => {
      console.log("上传成功:", response, file);
      ElMessage.success("上传成功");
    };
    const handleError = (error, file) => {
      console.error("上传失败:", error, file);
      ElMessage.error("上传失败");
    };
    const handleExceed = () => {
      ElMessage.warning(`最多只能上传 ${props.limit} 个文件`);
    };
    const beforeUpload = (file) => {
      const isLtLimit = file.size / 1024 / 1024 < props.maxSize;
      if (!isLtLimit) {
        ElMessage.error(`文件大小不能超过 ${props.maxSize}MB`);
        return false;
      }
      return true;
    };
    return (_ctx, _cache) => {
      const _component_el_icon = ElIcon;
      const _component_el_upload = ElUpload;
      return openBlock(), createBlock(_component_el_upload, mergeProps({
        "file-list": fileList.value,
        "onUpdate:fileList": _cache[0] || (_cache[0] = ($event) => fileList.value = $event)
      }, toHandlers(__props.config.eventHandlers), {
        action: __props.uploadUrl,
        headers: __props.headers,
        data: __props.uploadData,
        name: __props.uploadName,
        multiple: __props.multiple,
        limit: __props.limit,
        accept: __props.accept,
        disabled: __props.disabled,
        readonly: __props.readonly,
        "on-preview": handlePreview,
        "on-remove": handleRemove,
        "on-success": handleSuccess,
        "on-error": handleError,
        "on-exceed": handleExceed,
        "before-upload": beforeUpload,
        "auto-upload": __props.autoUpload,
        drag: ""
      }), {
        default: withCtx(() => [
          createVNode(_component_el_icon, { class: "el-icon--upload" }, {
            default: withCtx(() => [
              createVNode(unref(upload_filled_default))
            ]),
            _: 1
          }),
          _cache[1] || (_cache[1] = createElementVNode("div", { class: "el-upload__text" }, [
            createTextVNode(" 拖拽文件到此处或"),
            createElementVNode("em", null, "点击上传")
          ], -1))
        ]),
        _: 1
      }, 16, ["file-list", "action", "headers", "data", "name", "multiple", "limit", "accept", "disabled", "readonly", "auto-upload"]);
    };
  }
});
const _Upload = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-fef25575"]]);
export {
  _Upload as default
};
