<?php
require_once '../includes/header.php';

// Get cart items from database if user is logged in
$cart_items = [];
if (isLoggedIn()) {
    $cart_items = getCartItems($pdo, $_SESSION['user_id']);
}
?>

<div class="container">
    <div class="section-title">
        <h2>Shopping Cart</h2>
    </div>
    
    <div class="cart-content">
        <table class="cart-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Name</th>
                    <th>Price</th>
                    <th>Quantity</th>
                    <th>Total</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <!-- Cart items will be populated by JavaScript -->
            </tbody>
        </table>
        
        <div class="cart-total">
            <div class="cart-actions">
                <a href="<?php echo SITE_URL; ?>/products/index.php" class="btn btn-outline">Continue Shopping</a>
                <div>
                    <strong>Total: <span class="cart-total-amount">$0.00</span></strong>
                    <a href="<?php echo SITE_URL; ?>/cart/checkout.php" class="btn btn-primary" style="margin-left: 15px;">Proceed to Checkout</a>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// Pass PHP cart data and site URL to JavaScript
window.serverCartItems = <?php echo json_encode($cart_items); ?>;
window.siteUrl = '<?php echo SITE_URL; ?>';
</script>

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