<template>
  <div>
    <el-image
      v-if="isLoaded && realSrc"
      :src="realSrc"
      fit="cover"
      :style="`width:${realWidth};height:${realHeight};`"
      :preview-src-list="realSrcList"
    >
      <div slot="error" class="image-slot">
        <i class="el-icon-picture-outline"></i>
      </div>
    </el-image>

    <div v-else-if="!isLoaded">
      <i class="el-icon-loading"></i>
    </div>

    <div v-else>
      <i class="el-icon-picture-outline"></i>
    </div>
  </div>
</template>

<script>

import { listByIds } from '@/api/system/oss'

export default {
  name: "ImagePreview",
  props: {
    src: {
      type: String,
      default: ""
    },
    width: {
      type: [Number, String],
      default: ""
    },
    height: {
      type: [Number, String],
      default: ""
    }
  },
  data() {
    return {
      localSrc: '',
      isLoaded: false,
      error: null
    }
  },
  async created() {
    await this.loadImage(this.src);
  },
  methods: {
    async loadImage(val) {
      this.isLoaded = false;
      this.error = null;

      if (!val) {
        this.isLoaded = true;
        return;
      }

      try {
        let urls = [];
        if (Array.isArray(val)) {
          urls = val;
        } else {
          const res = await listByIds(val);
          urls = res.data.map(item => item.url);
        }
        this.localSrc = urls.join(',');
        this.isLoaded = true;
      } catch (e) {
        console.error('图片加载失败:', e);
        this.error = e;
        this.isLoaded = true;
      }
    }
  },
  watch: {
    src(val) {
      this.loadImage(val);
    }
  },
  computed: {
    realSrc() {
      if (!this.isLoaded || !this.localSrc) return '';
      return this.localSrc.split(",")[0];
    },
    realSrcList() {
      if (!this.localSrc) return;
      return this.localSrc.split(",");
    },
    realWidth() {
      return typeof this.width == "string" ? this.width : `${this.width}px`;
    },
    realHeight() {
      return typeof this.height == "string" ? this.height : `${this.height}px`;
    }
  },
};
</script>

<style lang="scss" scoped>
.el-image {
  border-radius: 5px;
  background-color: #ebeef5;
  box-shadow: 0 0 5px 1px #ccc;
  ::v-deep .el-image__inner {
    transition: all 0.3s;
    cursor: pointer;
    &:hover {
      transform: scale(1.2);
    }
  }
  ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    color: #909399;
    font-size: 30px;
  }
}
.image-container {
  position: relative;
  width: 100%;
  height: 100%;

  .loading-slot, .empty-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background-color: #f5f7fa;
    border-radius: 5px;

    i {
      font-size: 20px;
      color: #909399;
    }
  }

  .el-icon-loading {
    animation: rotating 2s linear infinite;
  }
}

@keyframes rotating {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
