<script setup lang="ts">
import { formatNumber } from "@/utils/common";
import { useI18n } from "vue-i18n";

const { t } = useI18n();

interface Props {
  goods: any;
  theme?: string;
}

const props = defineProps<Props>();

const emit = defineEmits<{
  exchange: [];
  imageLoad: [];
  imageError: [];
}>();
</script>

<template>
  <div class="el2-single">
    <div class="el2-single__img-wrap">
      <img 
        :src="goods.image" 
        alt="" 
        class="el2-single__img" 
        @load="emit('imageLoad')" 
        @error="emit('imageError')" 
      />
    </div>

    <div class="el2-single__body">
      <div class="el2-single__name" :class="{ 'line-clamp-2': theme === '3' }">
        <span v-if="goods.tag" class="el2-single__tag">{{ goods.tag }}</span>
        {{ goods.title }}
      </div>

      <div class="el2-single__price" v-if="goods.point2">{{ goods.point }}</div>
      <div class="el2-single__price" v-else-if="goods.onlyPoints">
        {{ formatNumber(goods.onlyPoints) }} {{ t("points") }}
      </div>

      <div class="el2-single__rank" v-if="goods?.topName">
        <span>🏆</span>
        <span v-if="goods.topName" style="margin-left: 5px">{{ goods.topName }}</span>
        <span style="flex: 1"></span>
        <span v-if="goods.top" class="el2-single__rank-tag">{{ goods.top }}</span>
      </div>

      <button type="button" @click.stop="emit('exchange')" class="el2-single__btn">
        {{ t("Exchange") }}
      </button>
    </div>
  </div>
</template>

<style scoped>
.el2-single {
  padding: 10px;
  background: #ffffff;
  box-shadow: 0px 3px 18px 0px rgba(223, 223, 223, 0.93);
  border-radius: 16px;
  border: 2px solid #f1f1f1;
  opacity: 0.9;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

/* PC端优化：纵向卡片，适配网格布局 */
@media (min-width: 768px) {
  .el2-single {
    flex-direction: column;
    padding: 16px;
    height: 100%;
  }

  .el2-single__img-wrap {
    width: 100%;
    aspect-ratio: 1/1;
  }

  .el2-single__img {
    width: 100%;
    height: 100%;
    aspect-ratio: 1/1;
  }

  .el2-single__body {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-top: 12px;
  }

  .el2-single__name {
    font-size: 15px;
    font-weight: 500;
    line-height: 1.4;
    min-height: 42px;
  }

  .el2-single__price {
    font-size: 16px;
    font-weight: 600;
    margin: 8px 0;
  }

  .el2-single__rank {
    margin: 8px 0;
    padding: 6px 10px;
    font-size: 12px;
  }

  .el2-single__btn {
    margin-top: 12px;
    width: 100%;
    padding: 10px 20px;
    font-size: 15px;
    font-weight: 600;
  }
}

.el2-single__body {
  margin-top: auto;
}

.el2-single__img {
  width: 100%;
  aspect-ratio: 4/4;
  display: block;
  object-fit: contain;
  border-radius: 20px;
}

.el2-single__name {
  font-size: 14px;
  margin-top: 5px;
}

.el2-single__name.line-clamp-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.el2-single__tag {
  background-color: #db0011;
  padding: 2px;
  text-align: center;
  color: #ffffff;
  border-radius: 3px;
  margin-right: 3px;
}

.el2-single__price {
  font-size: 14px;
  color: var(--global-primary-color);
  margin-top: 3px;
}

.el2-single__rank {
  padding-left: 5px;
  background-color: #ffc94b42;
  color: #b48825;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 5px;
  font-size: 12px;
  padding-bottom: 5px;
}

.el2-single__rank-tag {
  background: linear-gradient(to right, #c09a98, #e6bc87);
  padding: 2px;
  height: 80%;
  padding-left: 5px;
  text-align: center;
  color: #ffffff;
  border-radius: 20px 0 0 20px;
}

.el2-single__btn {
  width: 100%;
  margin-top: 16px;
  padding: 14px 24px;
  background: var(--global-primary-color);
  color: #ffffff;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.el2-single__btn:hover {
  background: var(--global-primary-color);
  transform: translateY(-2px);
}

.el2-single__btn:active {
  transform: translateY(0);
}
</style>
