<!--用户选择组件-->
<template>
  <div class="ace-upload">
    <upload
      v-if="!noFiles"
      v-bind="$props"
      ref="upload"
      :col-num="colNum"
      :configuration-key="configurationKey"
      :form-id="formId"
      :app-name="appName"
      :allow-upload="allowFileUpload"
      :allow-download="allowFileDownload"
      :allow-delete="allowFileDelete"
      :allow-preview="allowPreview"
      :max-secret-level="maxSecretLevel"
      :max-preview-size="maxPreviewSize"
      :default-secret-level="defaultSecretLevel"
      @fileAdded="fileAdded"
      @fileDeleting="fileDeleting"
      @fileDeleteError="fileDeleteError"
      @fileDeleted="fileDeleted"
      @spaceAllocating="spaceAllocating"
      @spaceAllocated="spaceAllocated"
      @spaceAllocateError="spaceAllocateError"
      @uploadProgress="uploadProgress"
      @uploadComplete="uploadComplete"
      @uploadSuccess="uploadSuccess"
      @uploadError="uploadError"
      @uploadFinished="uploadFinished"
      @initError="initError"
      @initSuccess="initSuccess"
      @error="error"
    ></upload>
    <upload-no-files
      v-else
      v-bind="$props"
      ref="upload"
      :configuration-key="configurationKey"
      :form-id="formId"
      :app-name="appName"
      :allow-upload="allowUpload"
      :allow-download="allowDownload"
      :allow-delete="allowDelete"
      :max-secret-level="maxSecretLevel"
      :default-secret-level="defaultSecretLevel"
      @fileAdded="fileAdded"
      @fileDeleting="fileDeleting"
      @fileDeleteError="fileDeleteError"
      @fileDeleted="fileDeleted"
      @spaceAllocating="spaceAllocating"
      @spaceAllocated="spaceAllocated"
      @spaceAllocateError="spaceAllocateError"
      @uploadProgress="uploadProgress"
      @uploadComplete="uploadComplete"
      @uploadSuccess="uploadSuccess"
      @uploadError="uploadError"
      @uploadFinished="uploadFinished"
      @initError="initError"
      @initSuccess="initSuccess"
      @error="error"
    ></upload-no-files>
  </div>
</template>
<script>
import upload from "./components/FileUploader";
import uploadNoFiles from "./components/FileUploaderNoFiles";
export default {
  name: "AceUpload",
  components: {
    upload,
    uploadNoFiles,
  },
  created() {},
  computed: {
    uploadInstance(){
      return this.$refs.upload;
    },
    fileList(){
      return this.$refs.upload.fileList
    },
    allowFileUpload() {
      return this.allowUpload;
    },
    allowFileDownload() {
      return this.allowDownload;
    },
    allowFileDelete() {
      return this.allowDelete;
    },
  },
  props: {
    colNum: {
      type: Number,
      default: 3,
    },
    allowPreview: {
      type: Boolean,
      default: function () {
        return true;
      },
    },
    appName: {
      type: String,
      default() {
        return this.$appName;
      },
    },
    noFiles: {
      type: Boolean,
      default: false,
    },
    defaultSecretLevel: {
      type: Number,
      default: function () {
        return 0;
      },
    },
    configurationKey: {
      type: String,
      require: true,
      default: function () {
        return "";
      },
    },
    formId: {
      type: String,
      require: true,
      default: function () {
        return "";
      },
    },
    allowUpload: {
      type: Boolean,
      default: function () {
        return true;
      },
    },
    allowDownload: {
      type: Boolean,
      default: function () {
        return true;
      },
    },
    allowDelete: {
      type: Boolean,
      default: function () {
        return true;
      },
    },
    maxSecretLevel: {
      type: Number,
      default: function () {
        return 1;
      },
    },
    refresh: {
      type: Function,
      default: function () {
        this.load();
      },
    },
    maxPreviewSize: {
      type: Number,
      default: null,
    },
  },
  data() {
    return {};
  },
  methods: {
    //文件添加 事件
    fileAdded(file) {
      this.$emit("fileAdded", file);
    },
    //文件删除 事件
    fileDeleting(file) {
      this.$emit("fileDeleting", file);
    },
    //文件删除发生错误 事件
    fileDeleteError(file, code, msg) {
      this.$emit("fileDeleteError", file, code, msg);
    },
    //文件删除 事件
    fileDeleted(file) {
      this.$emit("fileDeleted", file);
    },
    //空间分配中 事件
    spaceAllocating(file) {
      this.$emit("spaceAllocating", file);
    },
    //空间分配 事件
    spaceAllocated(file) {
      this.$emit("spaceAllocated", file);
    },
    //空间分配失败 事件
    spaceAllocateError(file, code, msg) {
      this.$emit("spaceAllocateError", file, code, msg);
    },
    //文件上传进度 事件
    uploadProgress(file, percentage) {
      this.$emit("uploadProgress", file, percentage);
    },
    //完整上传 事件
    uploadComplete(file) {
      this.$emit("uploadComplete", file);
    },
    //上传成功 事件
    uploadSuccess(file, response) {
      this.$emit("uploadSuccess", file, response);
    },
    //上传发生错误 事件
    uploadError(file, reason) {
      this.$emit("uploadError", file, reason);
    },
    //上传完成 事件
    uploadFinished() {
      this.$emit("uploadFinished");
    },
    //组件初始化发生错误 事件
    initError(code, msg) {
      this.$emit("initError", code, msg);
    },
    //组件初始化成功 事件
    initSuccess(webUploader, files, secretLevels, configuration) {
      this.$emit("initSuccess", webUploader, files, secretLevels, configuration);
    },
    //所有错误
    error(type, reason) {
      this.$emit("error", type, reason);
    },
  },
};
</script>

<style lang="scss"></style>
