<template>
  <el-upload
      name="upload"
      class="avatar-file-uploader"
      action="/api/meeting/upload/file/"
      drag
      :headers="Authorization"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload">
      <div v-if="imageUrl" class="upload-img-box">
        <img :src="imageUrl"/>
        <p>{{fileName}}</p>
      </div>
      <div v-else class="upload-btn-box">
        <div>
            <div class="upload-btn">+</div>
            <div slot="tip" class="el-upload__tip">PDF/WORD/PPT</div>
        </div>
      </div>
    </el-upload>
</template>

<script>
import { getToken } from "@/utils/auth";
export default {
    data(){
        return {
            imageUrl: "",
            fileName: '',
            Authorization: {},
        }
    },
    methods: {
      handleAvatarSuccess(res, file) {
        // console.log('上传成功',res)
        // console.log('上传成功',file)
        this.$emit('getFileUrl', res.data);
        this.fileName = file.name;


        var fileStr = /\.(docx|doc|pdf|pptx|ppt|DOCX|DOC|PDF|PPTX|PPT)$/;
        const isTargetFile = fileStr.test(file.name);
        if (!isTargetFile) {
          this.$message.error("Upload file can only be in WORD/PDF/PPT format!");
        }


        let suffixTxt = '';
        var startIndex = file.name.lastIndexOf(".");
	      if(startIndex != -1){
          suffixTxt = file.name.substring(startIndex+1, file.name.length)
        }
        if(suffixTxt == 'doc' || suffixTxt == 'docx' || suffixTxt == 'DOC' || suffixTxt == 'DOCX') {
          this.imageUrl = require('@/assets/img/conference/word.png');
        }else if(suffixTxt == 'pdf' || suffixTxt == 'PDF'){
          this.imageUrl = require('@/assets/img/conference/pdf.png');
        }else if(suffixTxt == 'ppt' || suffixTxt == 'pptx' || suffixTxt == 'PPT' || suffixTxt == 'PPTX'){
          this.imageUrl = require('@/assets/img/conference/pdf.png');
        }

        return isTargetFile;
      },
      beforeAvatarUpload(file) {
        this.Authorization.Authorization =  'Bearer ' + getToken();
        return true;
      },
    }
}
</script>

<style scoped>
.avatar-file-uploader{
  width: 100%;
  height: 100%;
  text-align: center;
}
.avatar-file-uploader .el-upload {
  border: 1px dashed #000;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-file-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-file-uploader-icon {
  display: inline-block;
  width: 160px;
  height: 220px;
  overflow: hidden;
  border: 1px dashed #aaa;
}
.avatar {
  width: 160px;
  height: 220px;
  display: inline-block;
}
.upload-img-box {
  width: 100%;
  height: 200px;
}
.upload-img-box img{
  max-width: 100%;
  max-height: 100%;
}
.upload-img-box p{
  margin: 0;
  font-size: 14px;
  color: #666;
  line-height: 100%;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upload-btn-box {
  display: table;
  height: 100%;
  width: 100%;
}
.upload-btn-box>div {
  display: table-cell;
  vertical-align: middle;
}
.upload-btn{
  display: inline-block;
  width: 30px;
  height: 30px;
  text-align: center;
  line-height: 24px;
  font-size: 40px;
  border: 3px solid #666;
  color: #666;
  border-radius: 6px;
}
.el-upload__tip {
  line-height: 20px;
}
.avatar-file-uploader /deep/ .el-upload{
  width: 100%;
  height: 100%;
}
.avatar-file-uploader /deep/ .el-upload-dragger{
  width: 100%;
  height: 270px;
}
</style>