<script setup>
  import { reactive, ref } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { getCategory, getProductList } from '@/api/mall'
  import { navigateTo } from '@/utils/router'

  /** 顶部分类选项 */
  const topNavOptions = ref([])
  const formData = reactive({
    name: '',
    type: '0'
  })
  /** 商品列表 */
  const cardList = ref([])

  const categoryList = async () => {
    const res = await getCategory()
    topNavOptions.value = res.data
  }

  const getListData = async () => {
    const res = await getProductList()
    cardList.value = res.rows
    console.log(res.rows)
  }

  const onTop = value => {
    formData.type = value
  }

  const onGo = item => {
    navigateTo('/pages/mall/detail', { productId: item.id })
  }

  onLoad(async () => {
    await categoryList()
    await getListData()
  })
</script>

<template>
  <view class="mall-list">
    <view class="top-box">
      <cb-search v-model="formData.name"></cb-search>
      <view class="top-options">
        <view
          v-for="(item, index) in topNavOptions"
          :key="index"
          :class="{ active: item.id === formData.type }"
          class="text"
          @click="onTop(item.id)"
        >
          {{ item.categoryName }}
        </view>
      </view>
    </view>

    <!-- 商品卡片 -->
    <view class="card-list">
      <view
        v-for="item in cardList"
        :key="item.id"
        class="card-item"
        @click="onGo(item)"
      >
        <image
          :src="item.mainImage"
          mode="scaleToFill"
          class="imghead"
        ></image>
        <text class="title">{{ item.productName }}</text>
        <view class="price">
          <view class="num-box">
            <text class="num">￥</text>
            <text class="num">{{ item.minPrice }}</text>
          </view>
          <!-- <text class="buy">好评率：99%</text> -->
        </view>
        <!-- 拼单数量 -->
        <text class="bottom-name">拼单数量:{{ item.salesCount }}件</text>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
  @import '@/styles/top-selector.scss';
  .mall-list {
    .top-box {
      padding: 24rpx;
    }

    .card-list {
      background: #f9f9f9;
      padding: 32rpx 24rpx;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;

      .card-item {
        border-radius: 14rpx;
        overflow: hidden;
        margin-bottom: 18rpx;
        display: flex;
        flex-direction: column;
        width: 344rpx;
        background: #ffffff;
        font-family: PingFang SC, PingFang SC;
        font-style: normal;
        text-transform: none;
        .imghead {
          width: 100%;
          height: 288rpx;
        }
        .title {
          font-weight: bold;
          font-size: 28rpx;
          color: #333333;
          padding: 16rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .price {
          padding: 0 16rpx;
          display: flex;
          justify-content: space-between;
          align-items: baseline;
          .num-box {
            display: flex;
            align-items: baseline;
            .num {
              font-weight: 500;
              font-size: 24rpx;
              color: #eb1c26;
              &:last-child {
                margin-left: 2rpx;
                font-weight: bold;
                font-size: 32rpx;
                color: #eb1c26;
              }
            }
          }
          .buy {
            font-weight: 500;
            font-size: 24rpx;
            color: #00d993;
          }
        }

        .bottom-name {
          padding: 16rpx 16rpx 26rpx 16rpx;
          font-weight: 500;
          font-size: 24rpx;
          color: #999999;
        }
      }
    }
  }
</style>
