import React, { useEffect, useState } from 'react'; import { useParams, Link, useSearchParams } from 'react-router-dom'; import { useThankYouSettings } from '@/hooks/useAppearanceSettings'; import Container from '@/components/Layout/Container'; import { CheckCircle, ShoppingBag, Package, Truck, User } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { formatPrice } from '@/lib/currency'; import { apiClient } from '@/lib/api/client'; export default function ThankYou() { const { orderId } = useParams<{ orderId: string }>(); const [searchParams] = useSearchParams(); const orderKey = searchParams.get('key'); const { template, headerVisibility, footerVisibility, backgroundColor, customMessage, elements, isLoading: settingsLoading } = useThankYouSettings(); const [order, setOrder] = useState(null); const [relatedProducts, setRelatedProducts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const isLoggedIn = (window as any).woonoowCustomer?.user?.isLoggedIn; useEffect(() => { const fetchOrderData = async () => { if (!orderId) return; try { // Use public order endpoint with key validation const keyParam = orderKey ? `?key=${orderKey}` : ''; const orderData = await apiClient.get(`/checkout/order/${orderId}${keyParam}`) as any; setOrder(orderData); // Fetch related products from first order item if (orderData.items && orderData.items.length > 0) { const firstProductId = orderData.items[0].product_id; const productData = await apiClient.get(`/shop/products/${firstProductId}`) as any; if (productData.related_products && productData.related_products.length > 0) { setRelatedProducts(productData.related_products.slice(0, 4)); } } } catch (err: any) { console.error('Failed to fetch order data:', err); setError(err.message || 'Failed to load order'); } finally { setLoading(false); } }; fetchOrderData(); }, [orderId, orderKey]); if (loading || settingsLoading || !order) { return (
); } const getStatusLabel = (status: string) => { const statusMap: Record = { 'pending': 'Pending Payment', 'processing': 'Processing', 'on-hold': 'On Hold', 'completed': 'Completed', 'cancelled': 'Cancelled', 'refunded': 'Refunded', 'failed': 'Failed', }; return statusMap[status] || status.charAt(0).toUpperCase() + status.slice(1); }; // Render receipt style template if (template === 'receipt') { return (
{/* Receipt Container */}
{/* Receipt Header */}

PAYMENT RECEIPT

Order #{order.number}

{new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' })}

{/* Custom Message */}

{customMessage}

{/* Order Items */} {elements.order_details && (
ITEM AMOUNT
{order.items.map((item: any) => (
{item.name}
Qty: {item.qty}
{formatPrice(item.total)}
))}
{/* Totals */}
SUBTOTAL: {formatPrice(parseFloat(order.subtotal || 0))}
{parseFloat(order.shipping_total || 0) > 0 && (
SHIPPING: {formatPrice(parseFloat(order.shipping_total))}
)} {parseFloat(order.tax_total || 0) > 0 && (
TAX: {formatPrice(parseFloat(order.tax_total))}
)}
TOTAL: {formatPrice(parseFloat(order.total || 0))}
{/* Payment & Status Info */}
Payment Method: {order.payment_method || 'N/A'}
Status: {getStatusLabel(order.status)}
{/* Customer Info */}
Bill To:
{order.billing?.first_name} {order.billing?.last_name}
{order.billing?.email}
{order.billing?.phone && (
{order.billing.phone}
)}
)} {/* Receipt Footer */}

{order.status === 'pending' ? 'Awaiting payment confirmation' : 'Thank you for your business!'}

{elements.continue_shopping_button && ( )} {isLoggedIn && ( )}
{/* Related Products */} {elements.related_products && relatedProducts.length > 0 && (

You May Also Like

{relatedProducts.map((product: any) => (
{product.image ? ( {product.name} ) : ( )}

{product.name}

{formatPrice(parseFloat(product.price || 0))}

))}
{/* Totals */}
SUBTOTAL: {formatPrice(parseFloat(order.subtotal || 0))}
{parseFloat(order.shipping_total || 0) > 0 && (
SHIPPING: {formatPrice(parseFloat(order.shipping_total))}
)} {parseFloat(order.tax_total || 0) > 0 && (
TAX: {formatPrice(parseFloat(order.tax_total))}
)}
TOTAL: {formatPrice(parseFloat(order.total || 0))}
{/* Payment & Status Info */}
Payment Method: {order.payment_method || 'N/A'}
Status: {getStatusLabel(order.status)}
{/* Customer Info */}
Bill To:
{order.billing?.first_name} {order.billing?.last_name}
{order.billing?.email}
{order.billing?.phone && (
{order.billing.phone}
)}
)} {/* Receipt Footer */}

{order.status === 'pending' ? 'Awaiting payment confirmation' : 'Thank you for your business!'}

{elements.continue_shopping_button && ( )}
{/* Related Products */} {elements.related_products && relatedProducts.length > 0 && (

You May Also Like

{relatedProducts.map((product: any) => (
{product.image ? ( {product.name} ) : ( )}

{product.name}

{formatPrice(parseFloat(product.price || 0))}

))}
)}
); } // Render basic style template (default) return (
{/* Success Header */}

Order Confirmed!

Order #{order.number}

{/* Custom Message */}

{customMessage}

{/* Order Details */} {elements.order_details && (

Order Details

{/* Order Items */}
{order.items.map((item: any) => (
{item.image && typeof item.image === 'string' ? ( {item.name} ) : ( )}

{item.name}

Quantity: {item.qty}

{formatPrice(item.total)}

))}
{/* Order Summary */}
Subtotal {formatPrice(parseFloat(order.subtotal || 0))}
{parseFloat(order.shipping_total || 0) > 0 && (
Shipping {formatPrice(parseFloat(order.shipping_total))}
)} {parseFloat(order.tax_total || 0) > 0 && (
Tax {formatPrice(parseFloat(order.tax_total))}
)}
Total {formatPrice(parseFloat(order.total || 0))}
{/* Customer Info */}

Customer Information

Email

{order.billing?.email || 'N/A'}

Phone

{order.billing?.phone || 'N/A'}

{/* Order Status */}

Order Status: {getStatusLabel(order.status)}

{order.status === 'pending' ? 'Awaiting payment confirmation' : "We'll send you shipping updates via email"}

)} {/* Action Buttons */}
{elements.continue_shopping_button && ( )} {isLoggedIn && ( )}
{/* Related Products */} {elements.related_products && relatedProducts.length > 0 && (

You May Also Like

{relatedProducts.map((product: any) => (
{product.image ? ( {product.name} ) : ( )}

{product.name}

{formatPrice(parseFloat(product.price || 0))}

))}
)}
); }