"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Alert, AlertDescription } from "@/components/ui/alert" import { Package, Search, CheckCircle, XCircle, Clock, AlertCircle, Mail, Truck, MapPin, Calendar, } from "lucide-react" import { PublicPageHeader, PublicSurface } from "@/components/public-surface" interface Order { id: string customerId: string | null customerEmail: string items: OrderItem[] totalAmount: number currency: string status: "pending" | "paid" | "fulfilled" | "cancelled" | "refunded" paymentIntentId: string | null stripeSessionId: string | null createdAt: string updatedAt: string shippingAddress?: { name: string address: string city: string state: string zipCode: string country: string } trackingNumber?: string estimatedDelivery?: string } interface OrderItem { productId: string productName: string price: number quantity: number priceId: string } export function OrderTracking() { const [orderId, setOrderId] = useState("") const [order, setOrder] = useState(null) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [isSearching, setIsSearching] = useState(false) const [showSuccess, setShowSuccess] = useState(false) // Track an order const trackOrder = async () => { if (!orderId.trim()) { setError("Please enter an order ID") return } setIsSearching(true) setLoading(true) setError(null) setOrder(null) try { // In a real application, you would call your API to fetch the order // For demo purposes, we'll simulate API call with timeout await new Promise((resolve) => setTimeout(resolve, 1000)) // Simulate API response const mockOrder: Order = { id: orderId, customerId: null, customerEmail: "customer@example.com", items: [ { productId: "prod_123", productName: "Vending Machine - SEAGA HY900", price: 2499.99, quantity: 1, priceId: "price_123", }, { productId: "prod_456", productName: "Vending Machine Stand", price: 299.99, quantity: 1, priceId: "price_456", }, ], totalAmount: 2799.98, currency: "usd", status: "paid", paymentIntentId: "pi_123", stripeSessionId: "cs_123", createdAt: "2024-01-15T10:30:00Z", updatedAt: "2024-01-16T14:20:00Z", shippingAddress: { name: "John Doe", address: "123 Main Street", city: "Salt Lake City", state: "UT", zipCode: "84101", country: "USA", }, trackingNumber: "1Z999AA1234567890", estimatedDelivery: "2024-01-20", } setOrder(mockOrder) } catch (err) { setError(err instanceof Error ? err.message : "Failed to find order") } finally { setLoading(false) setIsSearching(false) } } // Handle Enter key press const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter") { trackOrder() } } // Format date const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }) } // Get status badge variant const getStatusVariant = (status: Order["status"]) => { switch (status) { case "pending": return "secondary" case "paid": return "default" case "fulfilled": return "default" case "cancelled": return "destructive" case "refunded": return "destructive" default: return "secondary" } } // Get status icon const getStatusIcon = (status: Order["status"]) => { switch (status) { case "pending": return case "paid": return case "fulfilled": return case "cancelled": return case "refunded": return default: return null } } // Get status message const getStatusMessage = (status: Order["status"]) => { switch (status) { case "pending": return "Order is being processed" case "paid": return "Payment received. Preparing for shipment" case "fulfilled": return "Order has been shipped" case "cancelled": return "Order was cancelled" case "refunded": return "Order has been refunded" default: return "Unknown status" } } // Clear order after successful display useEffect(() => { if (order) { const timer = setTimeout(() => { setShowSuccess(true) }, 2000) return () => clearTimeout(timer) } }, [order]) return (
{/* Search Form */} Find Your Order Enter your order ID to track your package
setOrderId(e.target.value)} onKeyPress={handleKeyPress} className="flex-1" />
{error && ( {error} )} {showSuccess && ( Order found successfully! Scroll down for tracking details. )}
{/* Order Details */} {order && (
{/* Order Status */} Order Status {getStatusIcon(order.status)} {order.status.charAt(0).toUpperCase() + order.status.slice(1)} Order ID: {order.id} • Placed on {formatDate(order.createdAt)}

{getStatusMessage(order.status)}

{order.status === "fulfilled" && order.trackingNumber && (
Tracking Information
Tracking Number
{order.trackingNumber}
Estimated Delivery
{new Date( order.estimatedDelivery! ).toLocaleDateString()}
)}
{/* Customer Information */} Customer Information

Contact Information

{order.customerEmail}
{order.shippingAddress && (

Shipping Address

{order.shippingAddress.name}
{order.shippingAddress.address}
{order.shippingAddress.city},{" "} {order.shippingAddress.state}{" "} {order.shippingAddress.zipCode}
{order.shippingAddress.country}
)}
{/* Order Items */} Order Items {order.items.length} item{order.items.length !== 1 ? "s" : ""}{" "} • Total ${order.totalAmount.toFixed(2)}
{order.items.map((item, index) => (

{item.productName}

${item.price.toFixed(2)} • Qty: {item.quantity}

${(item.price * item.quantity).toFixed(2)}
))}
Total:
${order.totalAmount.toFixed(2)}
{/* Timeline */} {order.status !== "cancelled" && order.status !== "refunded" && ( Order Timeline

Order Placed

{formatDate(order.createdAt)}

Payment Confirmed

{formatDate(order.updatedAt)}

{order.status === "fulfilled" && (

Order Shipped

Tracking: {order.trackingNumber}

Estimated Delivery:{" "} {new Date( order.estimatedDelivery! ).toLocaleDateString()}

)}
)}
)} {!order && !loading && (

No order found

Enter your order ID above to track your shipment status

)}
) }