<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<{
  click: [];
  add: [count: number];
  imageLoad: [];
  imageError: [];
}>();
</script>

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

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

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

      <div class="el2-item__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-item__rank-tag">{{ goods.top }}</span>
      </div>

      <div class="el2-item__ctrl" @click.stop>
        <div @click="emit('add', -1)" class="no-obfuscate">-</div>
        <div class="el2-item__num no-obfuscate">{{ goods.count }}</div>
        <div @click="emit('add', 1)" class="no-obfuscate">+</div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.el2-item {
  position: absolute;
  padding: 10px;
  cursor: pointer;
  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;
}

.el2-item:hover {
  transform: scale(1);
}

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

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

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

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

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

.el2-item__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-item__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-item__ctrl {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  border-radius: 8px;
  font-weight: 700;
  cursor: pointer;
  overflow: hidden;
}

.el2-item__ctrl > div {
  flex: 1;
  display: flex;
  height: 35px;
  justify-content: center;
  align-items: center;
  background-color: #f2f2f2;
  user-select: none;
}

.el2-item__ctrl > div.el2-item__num {
  background-color: #f2f2f2;
  min-width: 40px;
  font-size: 18px;
  margin: 0 2px;
}

.el2-item__ctrl > div:first-child,
.el2-item__ctrl > div:last-child {
  font-size: 30px;
  background-color: #f2f2f2;
  min-width: 40px;
}

.el2-item__ctrl > div:first-child:hover,
.el2-item__ctrl > div:last-child:hover {
  background-color: #f2f2f2;
}
</style>
