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

$cart = $_SESSION['cart'] ?? [];
$subtotal = 0;
foreach ($cart as $item) {
    $subtotal += $item['price'] * $item['quantity'];
}

$pageTitle = 'Your Cart | ' . SITE_NAME;
$metaDescription = 'Review your ' . SITE_NAME . ' shopping cart.';
require_once __DIR__ . '/includes/header.php';
?>

<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
    <h1 class="text-4xl md:text-5xl font-black text-white uppercase tracking-tighter mb-8 border-b-2 border-brand-orange inline-block pb-2">Your Cart</h1>

    <?php if (!empty($cart)): ?>
        <div class="lg:grid lg:grid-cols-12 lg:gap-12">
            <div class="lg:col-span-8">
                <div class="bg-black border border-gray-800 rounded-2xl overflow-hidden shadow-2xl">
                    <ul role="list" class="divide-y divide-gray-800">
                        <?php foreach ($cart as $id => $details): ?>
                            <li class="p-6 flex flex-col sm:flex-row gap-6">
                                <div class="h-32 w-32 flex-shrink-0 overflow-hidden rounded-xl border border-gray-700 bg-gray-900 flex items-center justify-center p-2">
                                    <?php if ($details['image']): ?>
                                        <img src="<?= e($details['image']) ?>" alt="<?= e($details['name']) ?>" class="h-full w-full object-contain">
                                    <?php endif; ?>
                                </div>
                                <div class="flex flex-1 flex-col justify-between">
                                    <div class="flex justify-between text-base font-medium text-white">
                                        <h3 class="text-xl font-bold truncate pr-4"><a href="/product.php?slug=<?= urlencode($details['slug'] ?? '') ?>"><?= e($details['name']) ?></a></h3>
                                        <p class="ml-4 text-brand-orange font-black">$<?= number_format($details['price'], 2) ?></p>
                                    </div>
                                    <div class="flex flex-1 items-end justify-between text-sm mt-4">
                                        <p class="text-gray-400 font-semibold bg-gray-900 px-3 py-1 rounded-md">Qty <?= $details['quantity'] ?></p>
                                        <form action="/cart-action.php" method="POST">
                                            <input type="hidden" name="action" value="remove">
                                            <input type="hidden" name="id" value="<?= $id ?>">
                                            <button type="submit" class="font-bold text-red-500 hover:text-red-400 py-1 px-3 border border-red-500/30 hover:bg-red-500/10 rounded transition-all flex items-center gap-2">
                                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                                                Remove
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>

            <div class="lg:col-span-4 mt-10 lg:mt-0">
                <div class="bg-gray-900 border border-gray-800 rounded-2xl p-8 sticky top-24 shadow-2xl">
                    <h2 class="text-2xl font-black text-white uppercase tracking-wider border-b border-gray-700 pb-4 mb-6">Order Summary</h2>
                    <dl class="space-y-4 text-sm text-gray-300 font-semibold">
                        <div class="flex items-center justify-between"><dt>Subtotal</dt><dd class="text-white">$<?= number_format($subtotal, 2) ?></dd></div>
                        <div class="flex items-center justify-between"><dt>Shipping</dt><dd class="text-brand-orange font-bold">Calculated at checkout</dd></div>
                        <div class="flex items-center justify-between"><dt>Taxes</dt><dd class="text-white">$0.00</dd></div>
                        <div class="flex items-center justify-between border-t border-gray-700 pt-4 mt-4"><dt class="text-xl font-bold text-white uppercase tracking-tighter">Total</dt><dd class="text-3xl font-black text-brand-orange">$<?= number_format($subtotal, 2) ?></dd></div>
                    </dl>
                    <div class="mt-8">
                        <a href="/checkout.php" class="w-full bg-brand-orange hover:bg-[#ff5511] text-white font-black text-lg py-4 px-6 rounded-xl transition-all shadow-[0_0_20px_rgba(255,69,0,0.3)] hover:shadow-[0_0_30px_rgba(255,69,0,0.6)] uppercase tracking-widest flex justify-center items-center gap-3">
                            SECURE CHECKOUT <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14 5l7 7m0 0l-7 7m7-7H3"></path></svg>
                        </a>
                    </div>
                    <div class="mt-6 text-center text-xs text-gray-500 flex justify-center items-center gap-2">
                         <svg class="w-4 h-4 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path></svg>
                         256-bit SSL Encrypted Transaction
                    </div>
                </div>
            </div>
        </div>
    <?php else: ?>
        <div class="text-center py-24 bg-black border border-gray-800 rounded-3xl">
            <div class="bg-gray-900 inline-flex p-6 rounded-full mb-6">
                <svg class="h-16 w-16 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
            </div>
            <h3 class="text-3xl font-black text-white mb-4 uppercase tracking-tighter">Your cart is empty</h3>
            <p class="text-gray-400 font-semibold max-w-sm mx-auto mb-8">Looks like you haven't added any premium parts to your build yet.</p>
            <a href="/" class="inline-flex items-center text-white bg-brand-orange hover:bg-[#ff5511] font-bold text-lg py-3 px-8 rounded-lg transition-all shadow-[0_0_15px_rgba(255,69,0,0.3)]">
                RETURN TO STORE <svg class="ml-2 w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14 5l7 7m0 0l-7 7m7-7H3"></path></svg>
            </a>
        </div>
    <?php endif; ?>
</div>

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