'use client' import { useEffect, useState, useCallback } from 'react' import { useSearchParams, useRouter } from 'next/navigation' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Separator } from '@/components/ui/separator' import { CheckCircle, Package, Truck, Clock, ArrowRight, Star, Gift, Heart } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' import Link from 'next/link' import Image from 'next/image' import { toast } from 'sonner' import ConfettiBoom from 'react-confetti-boom' interface OrderConfirmation { orderId: string total: number status: string estimatedDelivery: string createdAt: string items: { id: string name: string quantity: number price: number image: string discount: number }[] customer: { name: string email: string } shippingAddress?: { name: string address: string address2?: string city: string state: string zipCode: string phone?: string } } export default function OrderConfirmationPage() { const searchParams = useSearchParams() const router = useRouter() const [orderData, setOrderData] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [showConfetti, setShowConfetti] = useState(false) const fetchOrderData = useCallback(async (orderId: string) => { try { setLoading(true) setError(null) const response = await fetch(`/api/order-confirmation/${orderId}`) if (!response.ok) { if (response.status === 401) { router.push('/auth/signin?callbackUrl=/order-confirmation?orderId=' + orderId) return } throw new Error('Order not found') } const data = await response.json() setOrderData(data) } catch (error) { console.error('Error fetching order:', error) setError('Failed to load order details') } finally { setLoading(false) } }, [router]) useEffect(() => { const orderId = searchParams.get('orderId') if (!orderId) { router.push('/products') return } fetchOrderData(orderId) }, [searchParams, router, fetchOrderData]) useEffect(() => { if (orderData) { // Trigger confetti after data loads setShowConfetti(true) // Show success toast toast.success('🎉 Order placed successfully!') // Stop confetti after 4 seconds const timer = setTimeout(() => { setShowConfetti(false) }, 4000) return () => clearTimeout(timer) } }, [orderData]) const getStatusColor = (status: string) => { switch (status) { case 'PENDING': return 'bg-amber-500' case 'PAID': return 'bg-emerald-500' case 'SHIPPED': return 'bg-emerald-600' case 'DELIVERED': return 'bg-green-500' case 'CANCELLED': return 'bg-red-500' default: return 'bg-gray-500' } } if (loading) { return (
{/* Header Skeleton */}
{/* Content Skeleton */}
{/* Main Content Skeleton - 2 columns */}
{/* Order Summary Card Skeleton */}
{/* Order header info skeleton */}
{/* Items list skeleton */}
{[1, 2, 3].map((i) => (
))}
{/* Shipping Address Card Skeleton */}
{/* Sidebar Skeleton */}
{/* Status Card Skeleton */}
{/* Progress Card Skeleton */}
{[1, 2, 3].map((i) => (
))}
{/* Action Buttons Skeleton */}
) } if (error || !orderData) { return (

{error || 'Order not found'}

) } return ( <> {/* Confetti Animation with highest z-index */} {showConfetti && (
)}
{/* Professional Header */}

Order Confirmed!

Thank you {orderData.customer.name}! Your order has been confirmed and is being prepared.

Order #{orderData.orderId.slice(-8).toUpperCase()}
{/* Floating Celebration Elements */}
{/* Professional Content Layout */}
{/* Main Order Information - Takes 2 columns */} {/* Order Summary Card */} Order Summary {/* Order Header Info */}
Order ID
#{orderData.orderId.slice(-8).toUpperCase()}
Status
{orderData.status}
Total Amount
₹{orderData.total.toFixed(2)}
{/* Items List */}

Items Ordered

{orderData.items.map((item, index) => { const discountedPrice = item.discount > 0 ? item.price - (item.price * item.discount / 100) : item.price return (
{item.name} {item.discount > 0 && (
{item.discount}% OFF
)}

{item.name}

Qty: {item.quantity}
₹{discountedPrice.toFixed(2)} {item.discount > 0 && ( ₹{item.price.toFixed(2)} )}
₹{(discountedPrice * item.quantity).toFixed(2)}
) })}
{/* Shipping Address Card */} {orderData.shippingAddress && ( Shipping Address

{orderData.shippingAddress.name}

{orderData.shippingAddress.address}

{orderData.shippingAddress.address2 && (

{orderData.shippingAddress.address2}

)}

{orderData.shippingAddress.city}, {orderData.shippingAddress.state} {orderData.shippingAddress.zipCode}

{orderData.shippingAddress.phone && (

Phone: {orderData.shippingAddress.phone}

)}
)}
{/* Sidebar - Order Status & Actions */} {/* Customer & Status Card */} Order Status
Customer
{orderData.customer.name}
{orderData.customer.email}
Expected Delivery
{new Date(orderData.estimatedDelivery).toLocaleDateString('en-US', { weekday: 'long', month: 'short', day: 'numeric' })}
{/* Progress Card */} Order Progress
1
Order Processing
We're preparing your items
2
Shipped
You'll receive tracking information
3
Delivered
Your order arrives!
{/* Action Buttons */}
) }