import { usePathname, useRouter } from '@/shared/config/i18n/navigation'; import formatDate from '@/shared/lib/formatDate'; import formatPrice from '@/shared/lib/formatPrice'; import { cn } from '@/shared/lib/utils'; import { Button } from '@/shared/ui/button'; import { Card, CardContent } from '@/shared/ui/card'; import { GlobalPagination } from '@/shared/ui/global-pagination'; import { useQuery } from '@tanstack/react-query'; import { Calendar, CheckCircle, Clock, Loader2, Package, RefreshCw, } from 'lucide-react'; import { useTranslations } from 'next-intl'; import { useSearchParams } from 'next/navigation'; import { useEffect, useState } from 'react'; import { order_api, OrderListRes } from '../lib/api'; import useOrderStore from '../lib/order'; const HistoryTabs = () => { const t = useTranslations(); const searchParams = useSearchParams(); const { setOrder } = useOrderStore(); const [page, setPage] = useState(1); const PAGE_SIZE = 36; const router = useRouter(); const pathname = usePathname(); const { data, isLoading } = useQuery({ queryKey: ['order_list', page], queryFn: () => order_api.list({ page, page_size: PAGE_SIZE }), select(data) { return data.data; }, }); useEffect(() => { const urlPage = Number(searchParams.get('page')) || 1; setPage(urlPage); }, [searchParams]); const handlePageChange = (newPage: number) => { const params = new URLSearchParams(searchParams.toString()); params.set('page', newPage.toString()); router.push(`${pathname}?${params.toString()}`, { scroll: true, }); }; const getStatusConfig = (status: 'NEW' | 'DONE') => { return status === 'DONE' ? { bgColor: 'bg-emerald-100', textColor: 'text-emerald-600', icon: CheckCircle, text: 'Yetkazildi', } : { bgColor: 'bg-yellow-100', textColor: 'text-yellow-600', icon: Clock, text: 'Kutilmoqda', }; }; const getPaymentTypeText = (type: 'CASH' | 'ACCOUNT_NUMBER') => { return type === 'CASH' ? 'Naqd pul' : 'Hisob raqami'; }; if (isLoading) { return (
); } if (!data?.results || data.results.length === 0) { return (

{t('Buyurtmalar topilmadi')}

{t('Hali buyurtma qilmagansiz')}

); } return ( <>

{t('Buyurtmalar tarixi')}

{data.results.length} ta buyurtma
{data.results.map((order: OrderListRes, idx: number) => { const statusConfig = getStatusConfig(order.status); const StatusIcon = statusConfig.icon; return (
{/* Status Timeline */}
{idx < data.results.length - 1 && (
)}
{/* Order Card */} {/* Header */}

#{order.order_number}

{statusConfig.text}
{formatDate.format( order.created_at, 'DD.MM.YYYY HH:mm', )}

{formatPrice( order.total_price + order.delivery_price, true, )}

{getPaymentTypeText(order.payment_type)}

{order.comment && (

Izoh:

{order.comment}

)} {/* Total Price Breakdown */}
Mahsulotlar narxi: {formatPrice(order.total_price, true)}
Yetkazish: {formatPrice(order.delivery_price, true)}
Jami: {formatPrice( order.total_price + order.delivery_price, true, )}
{/* Actions */}
); })}
); }; export default HistoryTabs;