<template>
  <div class="image-wrapper">
    <img class="cover-image" :src="imageSrc" alt="" @error="loadFailed = true" />
    <p v-if="loadFailed" class="error-tip">图片加载失败</p>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { _checkImgUrl } from '@/utils/index'

const props = defineProps({
  item: { type: Object, required: true },
})

const loadFailed = ref(false)

const imageSrc = computed(() => {
  const item = props.item as {
    imageUrl?: string
    video?: { cover?: { url_list?: string[] } }
  }
  const raw = item.imageUrl || item.video?.cover?.url_list?.[0]
  return _checkImgUrl(raw)
})

watch(imageSrc, () => {
  loadFailed.value = false
})
</script>

<style scoped lang="less">
.image-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
  overflow: hidden;

  .cover-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }

  .error-tip {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    color: rgba(255, 255, 255, 0.7);
    font-size: 14rem;
  }
}
</style>
