<?php 
session_start(); 
include 'config.php';

if(!isset($_SESSION['mc_username'])) {
    header("Location: login.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Cart • Reckon Store</title>
    <link href="https://fonts.bunny.net/css?family=Space+Grotesk:wght@500&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --green: #00ff88;
            --purple: #e879f9;
            --bg: #0a0a1a;
            --card: #161625;
            --card2: #1e1e2e;
        }
        body { background: var(--bg); color: #e0e0e0; margin:0; font-family: 'Space Grotesk', sans-serif; }
        .container { max-width: 1100px; margin: 2rem auto; padding: 0 1rem; }
        .header { text-align: center; padding: 3rem 0; }
        .header h1 { color: var(--green); font-size: 3rem; margin: 0; }
        .user-info { text-align: center; margin: 1rem 0; font-size: 1.2rem; }
        .user-info a { color: var(--purple); text-decoration: underline; }
        .cart-table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 20px; overflow: hidden; box-shadow: 0 15px 50px rgba(0,0,0,0.8); }
        .cart-table th { background: #000; color: var(--green); padding: 1.5rem; text-align: left; font-size: 1.2rem; }
        .cart-table td { padding: 1.5rem; border-bottom: 1px solid #333; vertical-align: middle; }
        .item-name { font-weight: bold; color: white; font-size: 1.3rem; }
        .item-price { color: var(--green); font-size: 1.2rem; }
        .qty-input { width: 80px; padding: 10px; background: #000; border: 2px solid var(--green); color: white; text-align: center; border-radius: 10px; font-size: 1.1rem; }
        .remove-btn { background: #ff0044; color: white; border: none; padding: 10px 15px; border-radius: 10px; cursor: pointer; font-weight: bold; }
        .remove-btn:hover { background: #ff3366; }
        .total-row { background: #000; font-size: 1.6rem; font-weight: bold; color: var(--green); }
        .total-row td { padding: 2rem 1.5rem; }
        .checkout-section { text-align: center; margin: 3rem 0; }
        .checkout-btn { background: var(--green); color: black; padding-font: bold 1.8rem; padding: 1.5rem 4rem; border: none; border-radius: 20px; cursor: pointer; box-shadow: 0 10px 30px rgba(0,255,136,0.4); transition: 0.3s; }
        .checkout-btn:hover { transform: scale(1.05); box-shadow: 0 15px 40px rgba(0,255,136,0.6); }
        .empty-cart { text-align: center; padding: 6rem 2rem; font-size: 1.8rem; color: #666; }
        .back-to-store { color: var(--green); text-decoration: underline; font-size: 1.4rem; }
        @media (max-width: 768px) {
            .cart-table th, .cart-table td { padding: 1rem; font-size: 1rem; }
            .header h1 { font-size: 2.2rem; }
        }
    </style>
</head>
<body>

<div class="container">
    <div class="header">
        <h1><i class="fas fa-shopping-cart"></i> YOUR CART</h1>
    </div>
    <div class="user-info">
        Logged in as: <strong><?=htmlspecialchars($_SESSION['mc_username'])?></strong> 
        • <a href="index.php?logout=1">Logout</a>
    </div>

    <div id="cart-content">
        <!-- Cart items will be injected here by JS -->
    </div>

    <div class="checkout-section">
        <button class="checkout-btn" id="checkout-btn" style="display:none;">
            <i class="fas fa-credit-card"></i> PROCEED TO CHECKOUT → $0.00
        </button>
    </div>

    <div class="empty-cart" id="empty-cart">
        <i class="fas fa-box-open" style="font-size:5rem;color:#333;margin-bottom:2rem;"></i>
        <p>Your cart is empty!</p>
        <a href="index.php" class="back-to-store">← Back to Store & Add Some Ranks!</a>
    </div>
</div>

<script>
// Global cart (same as index.php)
window.reckonCart = JSON.parse(localStorage.getItem('reckon_cart') || '[]');

function renderCart() {
    const content = document.getElementById('cart-content');
    const empty = document.getElementById('empty-cart');
    const checkoutBtn = document.getElementById('checkout-btn');

    if (window.reckonCart.length === 0) {
        empty.style.display = 'block';
        checkoutBtn.style.display = 'none';
        return;
    }

    empty.style.display = 'none';
    checkoutBtn.style.display = 'inline-block';

    let total = 0;
    let rows = `<table class="cart-table">
        <tr>
            <th>Item</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Total</th>
            <th>Action</th>
        </tr>`;

    window.reckonCart.forEach((item, i) => {
        const itemTotal = item.price * item.qty;
        total += itemTotal;
        rows += `
            <tr>
                <td><div class="item-name">${item.name}</div></td>
                <td class="item-price">$${item.price.toFixed(2)}</td>
                <td><input type="number" min="1" value="${item.qty}" class="qty-input" onchange="updateQty(${i}, this.value)"></td>
                <td class="item-price">$${itemTotal.toFixed(2)}</td>
                <td><button class="remove-btn" onclick="removeItem(${i})"><i class="fas fa-trash"></i> Remove</button></td>
            </tr>`;
    });

    rows += `
        <tr class="total-row">
            <td colspan="4">GRAND TOTAL</td>
            <td>$${total.toFixed(2)}</td>
        </tr>
    </table>`;

    content.innerHTML = rows;
    checkoutBtn.innerHTML = `<i class="fas fa-credit-card"></i> PROCEED TO CHECKOUT → $${total.toFixed(2)}`;
    checkoutBtn.onclick = () => {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = 'checkout.php';
        const input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'cart';
        input.value = JSON.stringify(window.reckonCart);
        form.appendChild(input);
        document.body.appendChild(form);
        form.submit();
    };
}

function updateQty(i, qty) {
    qty = parseInt(qty) || 1;
    window.reckonCart[i].qty = qty;
    localStorage.setItem('reckon_cart', JSON.stringify(window.reckonCart));
    renderCart();
    updateCartDisplay(); // also update dropdown on index.php if open
}

function removeItem(i) {
    window.reckonCart.splice(i, 1);
    localStorage.setItem('reckon_cart', JSON.stringify(window.reckonCart));
    renderCart();
    updateCartDisplay();
}

// Load on start
renderCart();
</script>

<?php
if(isset($_GET['logout'])) {
    unset($_SESSION['mc_username']);
    echo "<script>localStorage.removeItem('reckon_cart'); location.href='index.php';</script>";
}
?>
</body>
</html>