<?php
require_once 'config.php';
require_once 'includes/order.php';

$pageTitle = 'Order Details';
$orderModel = new Order();

$orderId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$order = $orderModel->getById($orderId);

// Check if user owns this order or is admin
if (!$order || (!isLoggedIn() || ($order['user_id'] != $_SESSION['user_id'] && !isAdmin()))) {
    header('Location: account.php');
    exit;
}

$pageTitle = 'Order #' . $order['order_number'];

require_once 'includes/header.php';
?>

<!-- Order Detail Page -->
<section class="products-section" style="padding-top: calc(var(--header-height) + var(--spacing-xl));">
    <div class="container">
        <div class="breadcrumb">
            <a href="<?php echo SITE_URL; ?>/index.php">Home</a>
            <span>/</span>
            <a href="<?php echo SITE_URL; ?>/account.php">My Account</a>
            <span>/</span>
            <span>Order #<?php echo htmlspecialchars($order['order_number']); ?></span>
        </div>
        
        <h1 class="section-title" style="margin-bottom: var(--spacing-xl);">Order #<?php echo htmlspecialchars($order['order_number']); ?></h1>
        
        <div style="display: grid; grid-template-columns: 2fr 1fr; gap: var(--spacing-xl);">
            <!-- Order Items -->
            <div>
                <div class="admin-card">
                    <div class="admin-card-header">
                        <h2>Order Items</h2>
                    </div>
                    
                    <table class="admin-table">
                        <thead>
                            <tr>
                                <th>Product</th>
                                <th>Price</th>
                                <th>Qty</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($order['items'] as $item): ?>
                                <tr>
                                    <td>
                                        <?php echo htmlspecialchars($item['product_name']); ?><br>
                                        <small style="color: var(--medium-gray);">
                                            <?php echo $item['size'] ? 'Size: ' . htmlspecialchars($item['size']) : ''; ?>
                                            <?php echo $item['color'] ? ' | Color: ' . htmlspecialchars($item['color']) : ''; ?>
                                        </small>
                                    </td>
                                    <td><?php echo formatPrice($item['product_price']); ?></td>
                                    <td><?php echo $item['quantity']; ?></td>
                                    <td><?php echo formatPrice($item['subtotal']); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="3"><strong>Total</strong></td>
                                <td><strong><?php echo formatPrice($order['total_amount']); ?></strong></td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            
            <!-- Order Summary -->
            <div>
                <div class="admin-card">
                    <div class="admin-card-header">
                        <h2>Order Status</h2>
                    </div>
                    <p><strong>Status:</strong> <span class="status-badge status-<?php echo $order['status']; ?>"><?php echo htmlspecialchars($order['status']); ?></span></p>
                    <p><strong>Payment:</strong> <span class="status-badge status-<?php echo $order['payment_status'] === 'paid' ? 'delivered' : 'pending'; ?>"><?php echo htmlspecialchars($order['payment_status']); ?></span></p>
                </div>
                
                <div class="admin-card">
                    <div class="admin-card-header">
                        <h2>Shipping Address</h2>
                    </div>
                    <p><?php echo nl2br(htmlspecialchars($order['shipping_address'])); ?></p>
                </div>
                
                <div class="admin-card">
                    <div class="admin-card-header">
                        <h2>Order Details</h2>
                    </div>
                    <p><strong>Order Date:</strong> <?php echo date('F j, Y g:i A', strtotime($order['created_at'])); ?></p>
                    <p><strong>Payment Method:</strong> <?php echo htmlspecialchars($order['payment_method']); ?></p>
                    <?php if ($order['notes']): ?>
                        <p><strong>Notes:</strong> <?php echo htmlspecialchars($order['notes']); ?></p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</section>

<?php require_once 'includes/footer.php'; ?>
