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

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

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

if (empty($orderId)) {
    redirect('/');
}

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

if (!$order) {
    redirect('/');
}

// Clear cart and pending order
clearCart();
unset($_SESSION['pending_order_id']);

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

require_once __DIR__ . '/includes/header.php';
?>

<section class="section" style="padding-top: 2rem;">
    <div class="container">
        <div class="card" style="max-width: 700px; margin: 0 auto;">
            <div class="card-body text-center" style="padding: 3rem;">
                <div style="width: 80px; height: 80px; background: rgba(16, 185, 129, 0.15); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 1.5rem;">
                    <i class="fas fa-check" style="font-size: 2.5rem; color: var(--color-success);"></i>
                </div>
                
                <h1 style="margin-bottom: 0.5rem;">Thank You!</h1>
                <p class="text-muted" style="font-size: 1.125rem; margin-bottom: 2rem;">
                    Your order has been placed successfully.
                </p>
                
                <div style="background: var(--color-background); padding: 1.5rem; border-radius: var(--radius-lg); text-align: left; margin-bottom: 2rem;">
                    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
                        <span class="text-muted">Order Number</span>
                        <strong style="font-size: 1.25rem;"><?= htmlspecialchars($order['order_number']) ?></strong>
                    </div>
                    
                    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
                        <span class="text-muted">Status</span>
                        <span class="badge badge-success">
                            <?= $order['payment_status'] === 'paid' ? 'Paid' : 'Processing' ?>
                        </span>
                    </div>
                    
                    <div style="display: flex; justify-content: space-between; align-items: center;">
                        <span class="text-muted">Total</span>
                        <strong><?= formatCurrency($order['total']) ?></strong>
                    </div>
                </div>
                
                <div style="text-align: left; margin-bottom: 2rem;">
                    <h3 style="margin-bottom: 1rem;">Order Details</h3>
                    
                    <?php foreach ($items as $item): ?>
                        <div style="display: flex; justify-content: space-between; padding: 0.75rem 0; border-bottom: 1px solid var(--color-border);">
                            <span>
                                <?= htmlspecialchars($item['name']) ?> 
                                <span class="text-muted">x<?= $item['quantity'] ?></span>
                            </span>
                            <span><?= formatCurrency($item['total']) ?></span>
                        </div>
                    <?php endforeach; ?>
                    
                    <div style="display: flex; justify-content: space-between; padding: 0.75rem 0; border-bottom: 1px solid var(--color-border);">
                        <span>Subtotal</span>
                        <span><?= formatCurrency($order['subtotal']) ?></span>
                    </div>
                    
                    <div style="display: flex; justify-content: space-between; padding: 0.75rem 0; border-bottom: 1px solid var(--color-border);">
                        <span>Shipping</span>
                        <span><?= $order['shipping_cost'] > 0 ? formatCurrency($order['shipping_cost']) : 'FREE' ?></span>
                    </div>
                    
                    <div style="display: flex; justify-content: space-between; padding: 0.75rem 0; font-weight: 600; font-size: 1.125rem;">
                        <span>Total</span>
                        <span><?= formatCurrency($order['total']) ?></span>
                    </div>
                </div>
                
                <div style="text-align: left; margin-bottom: 2rem;">
                    <h3 style="margin-bottom: 1rem;">Shipping Address</h3>
                    <p class="text-muted mb-0">
                        <?= htmlspecialchars($order['customer_name']) ?><br>
                        <?= htmlspecialchars($shippingAddress['address'] ?? '') ?><br>
                        <?= htmlspecialchars($shippingAddress['city'] ?? '') ?>, 
                        <?= htmlspecialchars($shippingAddress['state'] ?? '') ?> 
                        <?= htmlspecialchars($shippingAddress['zip'] ?? '') ?>
                    </p>
                </div>
                
                <p class="text-muted" style="margin-bottom: 2rem;">
                    A confirmation email has been sent to <strong><?= htmlspecialchars($order['customer_email']) ?></strong>.
                    You will receive tracking information once your order ships.
                </p>
                
                <div style="display: flex; gap: 1rem; justify-content: center;">
                    <a href="/shop.php" class="btn btn-primary btn-lg">
                        Continue Shopping
                    </a>
                    <?php if (CustomerAuth::isLoggedIn()): ?>
                        <a href="/account/orders.php" class="btn btn-secondary btn-lg">
                            View Orders
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</section>

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