<script lang="ts" setup>
import { useRouter } from 'vue-router';

import { fenToYuan } from '@vben/utils';

import { Button, Image } from 'ant-design-vue';

defineOptions({ name: 'ProductItem' });

defineProps({
  spuId: {
    type: Number,
    default: 0,
  },
  picUrl: {
    type: String,
    default: 'https://img1.baidu.com/it/u=1601695551,235775011&fm=26&fmt=auto',
  },
  title: {
    type: String,
    default: '',
  },
  price: {
    type: [String, Number],
    default: '',
  },
  salesCount: {
    type: [String, Number],
    default: '',
  },
  stock: {
    type: [String, Number],
    default: '',
  },
});

const { push } = useRouter();

/** 查看商品详情 */
function openDetail(spuId: number) {
  push({ name: 'ProductSpuDetail', params: { id: spuId } });
}
</script>

<template>
  <div class="product-warp cursor-pointer" @click.stop="openDetail(spuId)">
    <!-- 左侧商品图片-->
    <div class="product-warp-left mr-6">
      <Image
        :initial-index="0"
        :preview-src-list="[picUrl]"
        :src="picUrl"
        class="product-warp-left-img"
        fit="contain"
        preview-teleported
        @click.stop
      />
    </div>
    <!-- 右侧商品信息 -->
    <div class="product-warp-right">
      <div class="description">{{ title }}</div>
      <div class="my-1">
        <span class="mr-5">库存: {{ stock || 0 }}</span>
        <span>销量: {{ salesCount || 0 }}</span>
      </div>
      <div class="flex items-center justify-between">
        <span class="price">￥{{ fenToYuan(price) }}</span>
        <Button size="small" text type="primary">详情</Button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
/** TODO @jawe：下面的，看看要不要用 tailwind 简化，ai 友好； */
.button {
  padding: 5px 10px;
  color: white;
  cursor: pointer;
  background-color: #007bff;
  border: none;
}

.product-warp {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  background-color: rgb(128 128 128 / 30%);
  border: 1px solid var(--el-border-color);
  border-radius: 8px;

  &-left {
    width: 70px;

    &-img {
      width: 100%;
      height: 100%;
      border-radius: 8px;
    }
  }

  &-right {
    flex: 1;

    .description {
      display: -webkit-box;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 1; /* 显示一行 */
      font-size: 16px;
      font-weight: bold;
      -webkit-box-orient: vertical;
    }

    .price {
      color: #ff3000;
    }
  }
}
</style>
