import {useMemo} from 'react'
import Taro from '@tarojs/taro'
import {Button} from '@nutui/nutui-react-taro'
import {View, Text} from '@tarojs/components'
import OrderList from '@/user/order/components/OrderList'

export default function RiderOrders() {
  const isLoggedIn = useMemo(() => {
    return !!Taro.getStorageSync('access_token') && !!Taro.getStorageSync('UserId')
  }, [])

  const riderId = useMemo(() => {
    const raw = Number(Taro.getStorageSync('UserId'))
    return Number.isFinite(raw) && raw > 0 ? raw : undefined
  }, [])

  if (!isLoggedIn) {
    return (
      <View className="bg-gray-50 min-h-screen p-4">
        <View className="bg-white rounded-lg p-4">
          <Text className="text-sm text-gray-700">请先登录</Text>
          <View className="mt-3">
            <Button type="primary" size="small" onClick={() => Taro.navigateTo({url: '/passport/login'})}>
              去登录
            </Button>
          </View>
        </View>
      </View>
    )
  }

  if (!riderId) {
    return (
      <View className="bg-gray-50 min-h-screen p-4">
        <View className="bg-white rounded-lg p-4">
          <Text className="text-sm text-gray-700">未获取到骑手信息（UserId）</Text>
        </View>
      </View>
    )
  }

  return (
    <View className="bg-gray-50 min-h-screen">
      <View className="px-3">
        <OrderList mode="rider" baseParams={{riderId}} />
      </View>
    </View>
  )
}

