<template>
  <div style="width: 100%;height: 100%">
    <el-upload
        style="width: 100%;height: 100%"
        class="avatar-uploader"
        :action="imageUrl"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
      <img style="width: 100%;height: 100%;object-fit: contain" v-if="data.icon" :src="data.icon" class="avatar">
      <i v-else style="width: 100%;height: 100%;font-size: 2vw" class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>
<script>
export default {
  props: ['data'],
  data() {
    return {
      imageUrl: '/prod-api' + "/file/upload",
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      if (res.code === 200) {
        this.$props.data.icon = res.data.url
      }
    },
    beforeAvatarUpload(file) {
      if (!(file.type === 'image/jpeg' || file.type === 'image/png')) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!');
      }
      return file.type === 'image/jpeg' || file.type === 'image/png'
    }
  }
}
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

/deep/ .el-upload {
  width: 100%;
  height: 100%;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 4vw;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>