<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '%myOrders.title%',
    navigationBarBackgroundColor: '#fff',
  },
}
</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 { orderList, orderStatusEnum } from '@/api/order'
import { t } from '@/locale'
import { formatNumber } from '@/utils'
import { getPageParams, toPage } from '@/utils/page'

defineOptions({
  name: 'MyOrders', // 我的订单
})

const userInfo = computed(() => {
  return getUserInfoHook()
})
// z-paging
const paging = ref(null)
// 类似mixins，如果是页面滚动务必要写这一行，并传入当前ref绑定的paging，注意此处是paging，而非paging.value
useZPaging(paging)
const tab = ref<number>(0)
const tabList = ref<any>([
  {
    label: 'myOrders.tab.all',
    value: 0,
  },
  {
    label: 'myOrders.tab.toPay',
    value: 1,
  },
  {
    label: 'myOrders.tab.success',
    value: 2,
  },
  {
    label: 'myOrders.tab.failed',
    value: 3,
  },
  {
    label: 'myOrders.tab.reward',
    value: 4,
  },
])
const orderStatusEnumData = ref<any>([])
async function getOrderStatus() {
  try {
    const res = await orderStatusEnum({ id: 1 })
    if (res.code === '200') {
      orderStatusEnumData.value = res.data
    }
  }
  catch {

  }
}

// 搜索结果
const dataList = ref<any[]>([])
async function queryList(pageNo: number, pageSize: number) {
  // 此处请求仅为演示，请替换为自己项目中的请求
  try {
    const res = await orderList({
      page: pageNo,
      size: pageSize,
      type: tab.value, // 根据tab的值来查询不同状态的订单
    })
    paging.value.complete(res.data.list)
  }
  catch {
    paging.value.complete(false)
  }
}
onLoad((options) => {
  getOrderStatus()

  // 处理页面参数，如果有type参数则切换到对应的tab
  if (options) {
    const params = getPageParams(options)
    if (params.type !== undefined) {
      // 确保type值在有效范围内
      const typeValue = Number(params.type)
      if (typeValue >= 0 && typeValue <= 4) {
        tab.value = typeValue
      }
    }
  }
})
</script>

<template>
  <z-paging ref="paging" v-model="dataList" use-page-scroll @query="queryList">
    <template #top>
      <wd-tabs v-model="tab" custom-class="bg-transparent!" @click="() => queryList(1, 20)">
        <wd-tab v-for="tabItem in tabList" :key="tabItem.value" :title="t(tabItem.label)" :name="tabItem.value" />
      </wd-tabs>
    </template>
    <wd-card v-for="item in dataList" :key="item.orderId" type="rectangle" custom-class="px-24rpx! py-6rpx!" custom-content-class="py-18rpx!" custom-title-class="py-18rpx!" @click="toPage('/pages/myOrders/orderDetail', { id: item.id })">
      <template #title>
        <view class="flex items-center justify-between">
          <view class="text-28rpx text-#000">
            {{ $t('myOrders.order.id') }}：{{ item.orderId }}
          </view>
          <wd-text size="26rpx" type="primary" :text="orderStatusEnumData.find((i:any) => i.code === item.status)?.name" />
        </view>
      </template>
      <view class="flex justify-center gap-24rpx">
        <view class="h-140rpx w-140rpx shrink-0 text-center">
          <image
            :src="item?.orderInfoVO?.[0]?.image"
            class="h-full w-full"
            mode="aspectFit"
          />
        </view>
        <view class="flex-1">
          <view class="line-clamp-2 text-28rpx text-#000">
            {{ item?.orderInfoVO?.[0]?.productName }}
          </view>
          <view class="py-4rpx text-24rpx text-#3A444C">
            {{ $t('myOrders.order.color') }}： {{ item?.orderInfoVO?.[0]?.sku }}
          </view>
          <view class="flex items-center justify-between text-24rpx text-#3A444C">
            <view>
              ৳ {{ formatNumber(item?.orderInfoVO?.[0]?.price) }}
            </view>
            <view>
              {{ $t('myOrders.order.quantity') }}：{{ item?.orderInfoVO?.[0]?.payNum }}
            </view>
          </view>
        </view>
      </view>
    </wd-card>
  </z-paging>
</template>

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