<template>
  <div
    class="bg-upload"
    :class="{ 'is-disabled': actionDisabled, 'is-easy': isEasy }"
  >
    <el-upload
      action="/apaas/common/file/upload"
      :data="{
        directory: 'file',
        uniqueCode: false,
      }"
      :before-upload="handleBeforeUpload"
      :on-exceed="handleExceed"
      :on-success="handleSuccess"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :limit="limit"
      :disabled="actionDisabled"
      style="max-width: 600px"
      multiple
    >
      <!-- <el-button type="primary">
        上传附件
      </el-button>
      <div class="el-upload__tip" slot="tip">
        支持上传{{ fileTypes.join("、") }}，最大{{ fileMaxSize }}M
      </div> -->
      <template v-if="isEasy">
        <el-button type="primary" size="mini">
          <bg-icon
            :icon="`#${triggerIcon}`"
            v-if="triggerIcon"
            style="margin-right: 8px"
          />
          {{ triggerText }}
        </el-button>
      </template>
      <div class="trigger-content" v-else>
        <div class="trigger-icon">
          <img src="./imgs/ic_updata_cloud.png" />
        </div>
        <p class="trigger-tip" style="text-align: center" v-if="customTips">
          <slot></slot>
        </p>
        <p class="trigger-tip" v-else>
          1.最多允许上传{{ limit }}个附件 <br />
          2.单个附件最大{{ fileMaxSize }}M； <br />
          3.允许上传后缀为传{{ fileTypes.join("、") }} <br />
        </p>
      </div>
      <template v-slot:tip v-if="otherInfo != ''">
        <div class="el-upload__tip" style="color: #909bb6">
          {{ otherInfo }}
        </div>
      </template>
    </el-upload>
  </div>
</template>

<script>
export default {
  name: "BgUpload",
  // model: {
  //   prop: "value",
  //   event: "change",
  // },
  props: {
    modelValue: {
      type: Array,
      default: () => [],
    },
    limit: {
      type: Number,
    },
    fileTypes: {
      type: Array,
      default: () => [
        "doc",
        "docx",
        "xls",
        "xlsx",
        "pdf",
        "jpg",
        "jpeg",
        "png",
      ],
    },
    fileMaxSize: {
      type: Number,
      default: 20, // 单位：M
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    refresh: {
      type: Boolean,
      default: false,
    }, // 是否重新初始化附件（手动刷新组件的附件列表）
    customTips: {
      type: Boolean,
      default: false,
    }, // 是否自定义提示
    isEasy: {
      type: Boolean,
      default: false,
    },
    triggerText: {
      type: String,
      default: "请上传",
    },
    triggerIcon: {
      type: String,
      default: "bg-ic-file",
    },
    otherInfo: {
      type: String,
      default: "",
    },
  },
  emits: ["update:modelValue"],
  data() {
    return {
      fileList: [],
    };
  },
  computed: {
    actionDisabled() {
      return this.disabled ;//|| this.fileList.length === this.limit; 文件数量 === limit 会导致无法删除已上传文件
    },
  },
  watch: {
    modelValue() {
      let newStr = this.modelValue.map((v) => v.url).join(",");
      let oldStr = this.fileList
        .map((v) => (v.response && v.response.data) || v.url)
        .join(",");

      if (newStr !== oldStr) {
        this.fileList = [...this.modelValue];
      }
    },
  },
  methods: {
    initFileList() {
      let urls = (this.value && this.value.split(",")) || [];

      this.fileList = urls.map((url, index) => {
        let temp = url.split("/");
        let name = temp[temp.length - 1] || `附件_${index + 1}`;

        return { name, url };
      });

      this.$emit("update:refresh", false);
    },
    handleBeforeUpload(file) {
      let temp = file.name.split(".");
      let type = temp[temp.length - 1].toLocaleLowerCase();
      let fileTypesOk = this.fileTypes.indexOf(type) > -1;
      let fileMaxSizeOk = file.size / 1024 / 1024 <= this.fileMaxSize;

      if (!fileTypesOk) {
        this.$message.error(
          `上传文件只能是${this.fileTypes.join("、")}这些格式！`
        );
      }

      if (!fileMaxSizeOk) {
        this.$message.error(`上传文件大小不能超过${this.fileMaxSize}M!`);
      }

      return fileTypesOk && fileMaxSizeOk;
    },
    handleExceed(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(val) {
      let a = document.createElement("a"); // 生成一个a元素
      let event = new MouseEvent("click"); // 创建一个单击事件

      a.download = val.name; // 设置图片名称
      a.href = val.url || val.response.data; // 将生成的URL设置为a.href属性
      a.dispatchEvent(event); // 触发a的单击事件
    },
    handleRemove(file, fileList) {
      this.updateFileList(fileList);
    },
    handleSuccess(response, file, fileList) {
      this.updateFileList(fileList);
    },
    updateFileList(fileList) {
      let values = fileList.map((v) => {
        return {
          name: v.name,
          url: (v.response && v.response.data) || v.url,
        };
      });

      this.fileList = fileList;

      this.$emit("update:modelValue", values);
      this.$emit("change");
    },
  },
  mounted() {
    this.fileList = [...this.modelValue];
  },
};
</script>
