<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationStyle: 'custom',
  },
}
</route>

<script lang="ts" setup>
// 必须导入需要用到的页面生命周期（即使在当前页面上没有直接使用到）
// eslint-disable-next-line unused-imports/no-unused-imports
import { onPageScroll, onReachBottom } from '@dcloudio/uni-app'
import useZPaging from 'z-paging/components/z-paging/js/hooks/useZPaging.js'
import { getRankList } from '@/api/product'
import { t } from '@/locale'
import { formatNumber, formatSales } from '@/utils'
import { goBack, toPage } from '@/utils/page'

defineOptions({
  name: 'BestSellers', // 销量
})

// 获取屏幕边界到安全区域距离
const systemInfo = uni.getSystemInfoSync()
const safeAreaInsets = systemInfo.safeAreaInsets
// z-paging
const paging = ref(null)
// 类似mixins，如果是页面滚动务必要写这一行，并传入当前ref绑定的paging，注意此处是paging，而非paging.value
useZPaging(paging)

// 搜索结果
const dataList = ref([])
async function queryList(pageNo: number, pageSize: number) {
  try {
    const res = await getRankList({
      page: pageNo,
      size: pageSize,
    })
    paging.value.complete(res.data.list)
  }
  catch {
    paging.value.complete(false)
  }
}

// 根据排名获取标签背景色
function getRankBgColor(index: number) {
  const colors = {
    0: '#DEA90B', // 第一名
    1: '#5E719E', // 第二名
    2: '#D47128', // 第三名
  }
  return colors[index] || '#A5ADBD' // 其余排名
}

// 根据排名获取排名数字
function getRankNumber(index: number) {
  return index + 1
}
</script>

<template>
  <z-paging ref="paging" v-model="dataList" use-page-scroll @query="queryList">
    <template #top>
      <view class="relative from-[#FA2B19] to-[#FE6232] bg-gradient-to-br" :style="{ paddingTop: `${safeAreaInsets?.top}px` }">
        <wd-navbar :bordered="false" custom-class="bg-transparent!">
          <template #left>
            <wd-icon name="thin-arrow-left" color="#fff" size="32rpx" @click="() => goBack()" />
          </template>
          <template #title>
            <view class="text-white font-bold text-32rpx!">
              {{ $t('bestSellers.title') }}
            </view>
          </template>
        </wd-navbar>
        <image
          src="/static/icons/seller-nav.png"
          class="absolute bottom-12rpx right-12rpx h-166.27rpx w-180rpx"
        />
      </view>
    </template>
    <view class="pt-20rpx">
      <view v-for="(item, index) in dataList" :key="item.productId" class="mb-20rpx mb-20rpx flex items-center gap-24rpx bg-white p-24rpx" @click="toPage({ url: '/pages/productDetail/productDetail', params: { productId: item.productId } })">
        <view class="relative">
          <view class="h-160rpx w-160rpx shrink-0">
            <image
              :src="item.image"
              class="h-full w-full"
              mode="aspectFit"
            />
          </view>
          <!-- 左上角TOP标签 -->
          <view
            class="absolute left-10rpx top-10rpx h-52rpx w-48rpx flex items-center justify-center rounded-4rpx text-20rpx text-white font-bold"
            :style="{ backgroundColor: getRankBgColor(index), clipPath: 'polygon(0 0, 100% 0, 100% 70%, 50% 100%, 0 70%)' }"
          >
            <view class="flex flex-col items-center leading-none">
              <text class="text-16rpx">
                TOP
              </text>
              <text class="text-18rpx">
                {{ getRankNumber(index) }}
              </text>
            </view>
          </view>
        </view>
        <view class="flex-1">
          <view class="line-clamp-2 mb-3px h-80rpx break-all text-28rpx">
            {{ item.productName }}
          </view>
          <view class="mb-3px text-28rpx text-#FF0010">
            ৳ {{ formatNumber(item.price) }}
          </view>
          <view class="flex items-center rounded-8rpx from-white via-[rgba(255,210,212,0.58)] to-[rgba(255,0,16,0.2)] bg-gradient-to-l px-10rpx py-6rpx text-24rpx text-#FF0010">
            <image src="/static/icons/fire.png" class="mr-8rpx h-24rpx w-24rpx" />
            <text>{{ t('bestSellers.successfullyGrouped', [formatSales(item.sales)]) }}</text>
          </view>
        </view>
      </view>
    </view>
  </z-paging>
</template>

<style lang="scss" scoped>
//
</style>
