<template>
  <div class="FileUpload">
    <div v-for="(item, index) in fileList" :key="index" class="fileDiv">
      <img :src="item.DownloadLink" alt />
      <div class="fileDivBg">
        <i class="el-icon-delete" @click="deleteFile(index)"></i>
      </div>
    </div>
    <el-upload
      v-show="fileList.length < limit"
      class="avatar-uploader"
      :action="baseURL + '/api/Common/FileUpload'"
      :headers="myHeaders"
      :accept="typeList"
      :multiple="limit > 1"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :show-file-list="false"
    >
      <el-button v-if="button">导入文件</el-button>
      <i v-if="!button" class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
export default {
  name: '',
  props: {
    limit: {
      type: Number,
      default: 1
    },
    type: {
      type: String,
      default: 'image'
    },
    button: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      fileList: [],
      myHeaders: {
        Authorization: 'Bearer '
      },
      imageType: '.jpg, .jpeg, .png', // 图片类型
      videoType: '.mp4', // 视频类型
      audioType: '.mp4', // 音频类型
      fileType: '.doc, .docx', // 文件类型
      pdfType: '.pdf'
    }
  },
  computed: {
    baseURL() {
      // eslint-disable-next-line no-undef
      return global_config.BASE_URL
    },
    typeList() {
      if (this.type === 'image') {
        return this.imageType
      } else if (this.type === 'video') {
        return this.videoType
      } else if (this.type === 'audio') {
        return this.audioType
      } else if (this.type === 'file') {
        return this.fileType
      } else if (this.type === 'pdfType') {
        return this.pdfType
      } else {
        return this.imageType
      }
    }
  },
  created() {
  },
  methods: {
    handleAvatarSuccess(res) {
      if (res.Code === 0) {
        this.fileList = this.fileList.concat(res.Data)
        this.$emit('clearValidate')
      } else if (res.Code === 20002) {
        this.$message.closeAll()
        this.$message.error('登录已过期，请重新登录')
        this.$router.push('/')
      }
    },
    beforeAvatarUpload() {},
    deleteFile(index) {
      this.fileList.splice(index, 1)
      this.$emit('validateBanner')
    }
  }
}
</script>

<style lang="less">
.FileUpload {
  display: flex;
  flex-wrap: wrap;
  & > * {
    margin-right: 10px;
  }
  .fileDiv {
    width: 100px;
    height: 100px;
    line-height: 100px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    overflow: hidden;
    position: relative;
    &:hover {
      .fileDivBg {
        opacity: 1;
      }
    }
    img {
      width: 100%;
      display: inline-block;
      vertical-align: middle;
    }
    .fileDivBg {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      .el-icon-delete {
        cursor: pointer;
        height: 21px;
        line-height: 21px;
        margin: 0 5px;
      }
    }
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
}
</style>
