<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 paging = ref(null)
  /** 顶部分类选项 */
  const topNavOptions = ref([])
  const formData = reactive({
    name: '',
    type: '',
    pageNum: 1,
    pageSize: 15
  })
  /** 商品列表 */
  const cardList = ref([])

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

  const getListData = async (pageNum, pageSize, state) => {
    if (state === 1) {
      cardList.value = []
    }
    try {
      const res = await getProductList({
        pageNum,
        pageSize,
        categoryId: formData.type,
        productName: formData.name
      })
      paging.value.complete(res.rows)
    } catch (error) {
      paging.value.complete(false)
    }
  }

  const onTop = value => {
    formData.type = value
    formData.name = ''
    getListData(1, formData.pageSize, 1)
  }

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

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

<template>
  <z-paging
    ref="paging"
    v-model="cardList"
    :default-page-no="formData.pageNum"
    :default-page-size="formData.pageSize"
    safe-area-inset-bottom
    use-safe-area-placeholder
    :paging-style="{ 'background-color': '#f9f9f9' }"
    @query="getListData"
  >
    <view class="mall-list">
      <view class="top-box">
        <cb-search
          v-model="formData.name"
          placeholder="搜索热门商品"
          @search="getListData(1, formData.pageSize, 1)"
        ></cb-search>
        <view class="top-options">
          <view
            :class="{ active: formData.type === '' }"
            class="text"
            @click="onTop('')"
          >
            全部
          </view>
          <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>
  </z-paging>
</template>

<style lang="scss" scoped>
  @import '@/styles/top-selector.scss';
  .mall-list {
    .top-box {
      background: #ffffff;
      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>
