<template>
  <el-upload
      name="upload"
      class="avatar-img-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">JPG/JPEG/PNG</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('getphotoUrl', res.data.path);
        this.fileName = file.name;
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        this.Authorization.Authorization =  'Bearer ' + getToken();
        this.loading = true;
        const isJPG = file.type === "image/jpeg" || file.type === "image/jpg";
        const isPNG = file.type === "image/png";
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG && !isPNG) {
          this.$message.error("Upload images can only be in JPG or PNG format!");
        }
        if (!isLt2M) {
          this.$message.error("The size of the uploaded image cannot exceed 2MB!");
        }
        return (isJPG || isPNG) && isLt2M;
      },
    }
}
</script>

<style scoped>
.avatar-img-uploader{
  width: 100%;
  text-align: center;
}
.avatar-img-uploader .el-upload {
  border: 1px dashed #000;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-img-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-img-uploader-icon {
  display: inline-block;
  width: 160px;
  height: 220px;
  overflow: hidden;
  border: 1px dashed #aaa;
}
.upload-img-box {
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
}
.upload-img-box img{
  max-width: 100%;
  max-height: calc(100% - 40px);
}
.upload-img-box p{
  margin: 0;
  height: 40px;
  line-height: 40px;
  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-img-uploader /deep/ .el-upload{
  width: 100%;
  height: 100%;
}
.avatar-img-uploader /deep/ .el-upload-dragger{
  width: 100%;
  height: 270px;
}
</style>