<?php
/**
 * Tom's Java Jive - Order Detail Page
 */

$pageTitle = "Order Details - Tom's Java Jive";
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';

CustomerAuth::require();
$customer = CustomerAuth::getFullUser();
$currentPage = 'orders';

$orderId = $_GET['id'] ?? '';

$order = db()->fetch(
    "SELECT * FROM orders WHERE order_id = :id AND customer_id = :cid",
    ['id' => $orderId, 'cid' => $customer['customer_id']]
);

if (!$order) {
    redirect('/account/orders.php');
}

$items = json_decode($order['items'], true);
$shippingAddress = json_decode($order['shipping_address'] ?? '{}', true);

$extraHead = '<link rel="stylesheet" href="/assets/css/account.css?v='. filemtime(__DIR__ . '/../assets/css/account.css') .'">';
require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/includes/sidebar.php';

$statusClass = match($order['order_status']) {
    'pending' => 'warning',
    'confirmed', 'processing' => 'primary',
    'shipped' => 'primary',
    'delivered' => 'success',
    'cancelled', 'refunded' => 'error',
    default => 'primary'
};
?>

<div class="account-header">
    <div style="display: flex; justify-content: space-between; align-items: center;">
        <div>
            <a href="/account/orders.php" class="text-muted" style="font-size: 0.875rem;">
                <i class="fas fa-arrow-left"></i> Back to Orders
            </a>
            <h1 style="margin-top: 0.5rem;">Order #<?= htmlspecialchars($order['order_number']) ?></h1>
        </div>
        <span class="badge badge-<?= $statusClass ?>" style="font-size: 1rem; padding: 0.5rem 1rem;">
            <?= ucfirst($order['order_status']) ?>
        </span>
    </div>
</div>

<!-- Order Progress -->
<?php if (!in_array($order['order_status'], ['cancelled', 'refunded'])): ?>
<div class="section-card">
    <div class="section-card-body">
        <?php
        $steps = ['pending', 'confirmed', 'processing', 'shipped', 'delivered'];
        $currentStep = array_search($order['order_status'], $steps);
        if ($currentStep === false) $currentStep = 0;
        ?>
        <div style="display: flex; justify-content: space-between; position: relative;">
            <div style="position: absolute; top: 15px; left: 0; right: 0; height: 2px; background: var(--color-border);"></div>
            <div style="position: absolute; top: 15px; left: 0; width: <?= ($currentStep / 4) * 100 ?>%; height: 2px; background: var(--color-primary); transition: width 0.3s;"></div>
            
            <?php foreach ($steps as $i => $step): ?>
                <div style="text-align: center; position: relative; z-index: 1;">
                    <div style="width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 0.5rem; <?= $i <= $currentStep ? 'background: var(--color-primary); color: white;' : 'background: var(--color-surface); border: 2px solid var(--color-border);' ?>">
                        <?php if ($i < $currentStep): ?>
                            <i class="fas fa-check"></i>
                        <?php else: ?>
                            <?= $i + 1 ?>
                        <?php endif; ?>
                    </div>
                    <div style="font-size: 0.75rem; <?= $i <= $currentStep ? 'color: var(--color-text);' : 'color: var(--color-text-muted);' ?>">
                        <?= ucfirst($step) ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<div style="display: grid; grid-template-columns: 2fr 1fr; gap: 1.5rem;">
    <div>
        <!-- Order Items -->
        <div class="section-card">
            <div class="section-card-header">
                <h3>Order Items</h3>
            </div>
            <div class="section-card-body" style="padding: 0;">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th style="text-align: center;">Qty</th>
                            <th style="text-align: right;">Price</th>
                            <th style="text-align: right;">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($items as $item): ?>
                            <tr>
                                <td>
                                    <strong><?= htmlspecialchars($item['name']) ?></strong>
                                    <?php if (!empty($item['options'])): ?>
                                        <br><small class="text-muted"><?= htmlspecialchars($item['options']) ?></small>
                                    <?php endif; ?>
                                </td>
                                <td style="text-align: center;"><?= $item['quantity'] ?></td>
                                <td style="text-align: right;"><?= formatCurrency($item['price']) ?></td>
                                <td style="text-align: right;"><?= formatCurrency($item['total']) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- Shipping Info -->
        <?php if (!empty($shippingAddress)): ?>
        <div class="section-card">
            <div class="section-card-header">
                <h3>Shipping Information</h3>
            </div>
            <div class="section-card-body">
                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem;">
                    <div>
                        <h4 style="margin: 0 0 0.5rem; font-size: 0.875rem; color: var(--color-text-muted);">Shipping Address</h4>
                        <p style="margin: 0;">
                            <?= htmlspecialchars($shippingAddress['name'] ?? $order['customer_name']) ?><br>
                            <?= htmlspecialchars($shippingAddress['address'] ?? '') ?><br>
                            <?= htmlspecialchars($shippingAddress['city'] ?? '') ?>, <?= htmlspecialchars($shippingAddress['state'] ?? '') ?> <?= htmlspecialchars($shippingAddress['zip'] ?? '') ?><br>
                            <?= htmlspecialchars($shippingAddress['country'] ?? '') ?>
                        </p>
                    </div>
                    <?php if ($order['tracking_number']): ?>
                    <div>
                        <h4 style="margin: 0 0 0.5rem; font-size: 0.875rem; color: var(--color-text-muted);">Tracking</h4>
                        <p style="margin: 0 0 1rem;">
                            <strong><?= htmlspecialchars($order['tracking_number']) ?></strong>
                        </p>
                        <?php if ($order['tracking_url']): ?>
                            <a href="<?= htmlspecialchars($order['tracking_url']) ?>" target="_blank" class="btn btn-sm btn-primary">
                                <i class="fas fa-truck"></i> Track Package
                            </a>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
    
    <div>
        <!-- Order Summary -->
        <div class="section-card">
            <div class="section-card-header">
                <h3>Order Summary</h3>
            </div>
            <div class="section-card-body">
                <div style="margin-bottom: 1rem;">
                    <div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
                        <span class="text-muted">Subtotal</span>
                        <span><?= formatCurrency($order['subtotal']) ?></span>
                    </div>
                    <?php if ($order['discount'] > 0): ?>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem; color: var(--color-success);">
                        <span>Discount</span>
                        <span>-<?= formatCurrency($order['discount']) ?></span>
                    </div>
                    <?php endif; ?>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
                        <span class="text-muted">Shipping</span>
                        <span><?= $order['shipping'] > 0 ? formatCurrency($order['shipping']) : 'Free' ?></span>
                    </div>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
                        <span class="text-muted">Tax</span>
                        <span><?= formatCurrency($order['tax']) ?></span>
                    </div>
                    <?php if ($order['wallet_amount_used'] > 0): ?>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem; color: var(--color-success);">
                        <span>Wallet</span>
                        <span>-<?= formatCurrency($order['wallet_amount_used']) ?></span>
                    </div>
                    <?php endif; ?>
                </div>
                <div style="border-top: 2px solid var(--color-border); padding-top: 1rem; display: flex; justify-content: space-between;">
                    <strong>Total</strong>
                    <strong style="font-size: 1.25rem; color: var(--color-primary);"><?= formatCurrency($order['total']) ?></strong>
                </div>
            </div>
        </div>
        
        <!-- Payment Info -->
        <div class="section-card">
            <div class="section-card-header">
                <h3>Payment</h3>
            </div>
            <div class="section-card-body">
                <div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
                    <span class="text-muted">Method</span>
                    <span><?= ucfirst($order['payment_method'] ?? 'N/A') ?></span>
                </div>
                <div style="display: flex; justify-content: space-between;">
                    <span class="text-muted">Status</span>
                    <span class="badge badge-<?= $order['payment_status'] === 'paid' ? 'success' : 'warning' ?>">
                        <?= ucfirst($order['payment_status'] ?? 'Pending') ?>
                    </span>
                </div>
            </div>
        </div>
        
        <!-- Order Date -->
        <div class="section-card">
            <div class="section-card-body">
                <div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
                    <span class="text-muted">Order Date</span>
                    <span><?= formatDateTime($order['created_at']) ?></span>
                </div>
                <?php if ($order['updated_at']): ?>
                <div style="display: flex; justify-content: space-between;">
                    <span class="text-muted">Last Updated</span>
                    <span><?= formatDateTime($order['updated_at']) ?></span>
                </div>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- Need Help -->
        <div class="section-card">
            <div class="section-card-body text-center">
                <p class="text-muted" style="margin: 0 0 1rem;">Need help with this order?</p>
                <a href="/contact.php?order=<?= $order['order_number'] ?>" class="btn btn-secondary btn-block">
                    <i class="fas fa-headset"></i> Contact Support
                </a>
            </div>
        </div>
    </div>
</div>

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