<script setup>
import BoxIcon from '@/components/icons/BoxIcon.vue'
import { useMerchImagesApi } from '@/api/merchImages.js'
import { onMounted, ref } from 'vue'
const { getImageUrl } = useMerchImagesApi()

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

const fileList = ref([])

onMounted(async () => {
  try {
    const { imgUrl } = await getImageUrl(props.merch.merch_uuid, 'thumbnail')
    fileList.value = [
      {
        name: 'full.jpg',
        url: imgUrl,
        status: 'finished',
      },
    ]
  } catch (error) {
    fileList.value = []
    if (!error.message?.includes('404')) {
      console.error('Error getting image: ', error)
    }
  }
})

</script>

<template>
  <n-card class="responsive-card">
    <template #header>
      <h3 class="card-title">{{ merch.name }}</h3>
    </template>
    <template #cover>
      <div class="cover-wrapper">
        <img
          v-if="fileList.length > 0 && fileList[0].url"
          :src="fileList[0].url"
          alt="Thumbnail"
          class="thumbnail"
        />
        <BoxIcon v-else />
      </div>
    </template>
  </n-card>
</template>

<style scoped>
.responsive-card {
  width: 180px;
  height: 240px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.card-title {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
}

.cover-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  padding: 12px 0;
  overflow: hidden;
}

.thumbnail {
  width: 100%;
  height: auto;
  max-width: 80%;
  object-fit: contain;
}

.cover-wrapper :deep(svg) {
  width: 100%;
  height: auto;
  max-width: 80%;
}
</style>
