'use client'; import React, { useEffect, useState } from 'react'; import { useParams, useRouter } from 'next/navigation'; import Link from 'next/link'; import { getOrderById, cancelOrder } from '../../utils/orderApi'; import '../orders.scss'; import './order-details.scss'; interface OrderItem { id: number; product_id: number; product_name: string; price: number; quantity: number; total_price: number; } interface Order { id: number; order_number: string; status: string; total_amount: number; createdAt: string; shipping_address: string; shipping_city: string; shipping_postal_code: string; shipping_country: string; contact_phone: string; contact_email: string; payment_method: string; payment_status: string; tracking_number?: string; estimated_delivery_date?: string; notes?: string; items: OrderItem[]; } const OrderDetailsPage = () => { const params = useParams(); const router = useRouter(); const orderId = params.orderId as string; const [order, setOrder] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchOrderDetails = async () => { try { setLoading(true); const response = await getOrderById(orderId); setOrder(response); } catch (err) { console.error('Error fetching order details:', err); setError('Failed to load order details. Please try again later.'); } finally { setLoading(false); } }; if (orderId) { fetchOrderDetails(); } }, [orderId]); const handleCancelOrder = async () => { if (confirm('Are you sure you want to cancel this order?')) { try { setLoading(true); await cancelOrder(orderId); // Update local state after successful cancellation if (order) { setOrder({ ...order, status: 'cancelled' }); } } catch (err) { console.error('Error cancelling order:', err); alert('Failed to cancel the order. Please try again.'); } finally { setLoading(false); } } }; const formatPrice = (price: any): string => { try { const numValue = typeof price === 'string' ? parseFloat(price) : Number(price); return numValue.toFixed(2); } catch (error) { console.error("Error formatting price:", error); return "0.00"; } }; const getStatusBadgeClass = (status: string) => { switch (status.toLowerCase()) { case 'pending': return 'bg-warning'; case 'processing': return 'bg-info'; case 'shipped': return 'bg-primary'; case 'delivered': return 'bg-success'; case 'cancelled': return 'bg-danger'; default: return 'bg-secondary'; } }; const getPaymentStatusBadgeClass = (status: string) => { switch (status.toLowerCase()) { case 'paid': return 'bg-success'; case 'pending': return 'bg-warning'; case 'failed': return 'bg-danger'; case 'refunded': return 'bg-info'; default: return 'bg-secondary'; } }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleString(); }; if (loading) { return (
Loading...
); } if (error) { return (
{error}
); } if (!order) { return (
Order not found
View All Orders
); } return (

Order #{order.order_number}

Placed on {formatDate(order.createdAt)}

{order.status.charAt(0).toUpperCase() + order.status.slice(1)}
All Orders {order.status === 'pending' && ( )}
Order Summary
Order Status: {order.status.charAt(0).toUpperCase() + order.status.slice(1)}
Payment Status: {order.payment_status.charAt(0).toUpperCase() + order.payment_status.slice(1)}
Payment Method: {order.payment_method}
{order.tracking_number && (
Tracking Number: {order.tracking_number}
)} {order.estimated_delivery_date && (
Estimated Delivery: {new Date(order.estimated_delivery_date).toLocaleDateString()}
)}
Customer Information
Shipping Address
{order.shipping_address}
{order.shipping_city}, {order.shipping_postal_code}
{order.shipping_country}
Contact Information

Email: {order.contact_email}

Phone: {order.contact_phone}

{order.notes && (
Order Notes

{order.notes}

)}
Ordered Items
{order.items.map((item) => ( ))}
Product Price Quantity Total
{item.product_name}
${formatPrice(item.price)} {item.quantity} ${formatPrice(item.total_price)}
Total: ${formatPrice(order.total_amount)}
{order.status === 'shipped' && (
Tracking Information
Tracking Number: {order.tracking_number || 'Not available'}
)}
); }; export default OrderDetailsPage;