"use client" import { useEffect, useState } from "react" import { useRouter, useParams } from "next/navigation" import { ArrowLeft, ShoppingBag, User, Calendar, DollarSign, Package, MapPin, Phone, Mail, CheckCircle2, Clock, Loader2, AlertCircle } from "lucide-react" interface LineItem { id: number title: string quantity: number price: string sku?: string variant_title?: string } interface OrderDetails { id: number name: string order_number?: number email?: string | null created_at: string updated_at?: string total_price: string subtotal_price?: string total_tax?: string total_discounts?: string currency: string financial_status: string fulfillment_status: string | null customer: { id?: number first_name?: string | null last_name?: string | null email?: string | null phone?: string } | null shipping_address?: { first_name: string last_name: string address1: string address2?: string city: string province: string country: string zip: string phone?: string } billing_address?: { first_name: string last_name: string address1: string address2?: string city: string province: string country: string zip: string } line_items: LineItem[] note?: string tags?: string } export default function OrderDetailsPage() { const router = useRouter() const params = useParams() const orderId = params?.orderId as string const [order, setOrder] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState("") useEffect(() => { if (!orderId) return const fetchOrderDetails = async () => { setLoading(true) setError("") try { const res = await fetch(`/api/shopify/orders/${orderId}`) if (!res.ok) throw new Error("Failed to fetch order details") const data = await res.json() setOrder(data.order) } catch { setError("Failed to load order details. Please try again.") } finally { setLoading(false) } } fetchOrderDetails() }, [orderId]) if (loading) { return (

Loading order details...

) } if (error) { return (

{error}

) } if (!order) { return (

Order not found

) } const contactEmail = [order.email, order.customer?.email] .map((e) => (typeof e === "string" ? e.trim() : "")) .find(Boolean) || "" const customerDisplayName = [order.customer?.first_name, order.customer?.last_name] .filter(Boolean) .join(" ") .trim() const hasCustomerData = Boolean( customerDisplayName || contactEmail || order.customer?.phone ) return (
{/* Header */}

Order Details

{order.name}

{/* Content */}
{/* Order Summary & Status - Symmetric Top Grid */}
{/* Order Date */}
Order Date

{new Date(order.created_at).toLocaleString()}

{/* Total Amount */}
Total Amount

{order.total_price} {order.currency}

{/* Payment Status */}
Payment Status
{order.financial_status === "paid" && } {order.financial_status}
{/* Fulfillment Status */}
Fulfillment Status
{order.fulfillment_status === "fulfilled" ? ( ) : ( )} {order.fulfillment_status || "Unfulfilled"}
{/* Main Content Grid */}
{/* Left Column - Customer & Shipping */}
{/* Customer — always shown; placeholder when nothing is linked */}
Customer
{!hasCustomerData ? (

Customer: --

) : (

{customerDisplayName || "—"}

{contactEmail ? contactEmail : "Email: --"}
{order.customer?.phone ? (
{order.customer.phone}
) : null}
)}
{/* Shipping Address */} {order.shipping_address && (
Shipping Address

{order.shipping_address.first_name} {order.shipping_address.last_name}

{order.shipping_address.address1}

{order.shipping_address.address2 &&

{order.shipping_address.address2}

}

{order.shipping_address.city}, {order.shipping_address.province} {order.shipping_address.zip}

{order.shipping_address.country}

{order.shipping_address.phone && (

{order.shipping_address.phone}

)}
)}
{/* Centered Order Items Card */}
Order Items
{order.line_items.map((item) => (

{item.title}

{item.variant_title && (

{item.variant_title}

)}
Qty: {item.quantity} {item.sku && ( SKU: {item.sku} )}

{(parseFloat(item.price) * item.quantity).toFixed(2)} {order.currency}

{parseFloat(item.price).toFixed(2)} {order.currency} each

))}
{/* Order Summary */} {(order.subtotal_price || order.total_tax || (order.total_discounts && parseFloat(order.total_discounts) > 0)) && (
{order.subtotal_price && (
Subtotal {order.subtotal_price} {order.currency}
)} {order.total_tax && (
Tax {order.total_tax} {order.currency}
)} {order.total_discounts && parseFloat(order.total_discounts) > 0 && (
Discount -{order.total_discounts} {order.currency}
)}
Total {order.total_price} {order.currency}
)}
{/* Order Notes */} {order.note && (
Order Notes

{order.note}

)}
) }