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

$pageTitle = 'Shopping Cart';
$cart = new Cart();

// Handle cart actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'update':
                if (isset($_POST['key']) && isset($_POST['quantity'])) {
                    $cart->update($_POST['key'], (int)$_POST['quantity']);
                }
                break;
            case 'remove':
                if (isset($_POST['key'])) {
                    $cart->remove($_POST['key']);
                }
                break;
            case 'clear':
                $cart->clear();
                break;
        }
    }
    // Prevent form resubmission
    header('Location: cart.php');
    exit;
}

$cartItems = $cart->getItems();
$cartTotal = $cart->getTotal();

require_once 'includes/header.php';
?>

<!-- Cart Page -->
<section class="cart-page">
    <div class="container">
        <h1 class="section-title" style="margin-bottom: var(--spacing-xl);">Shopping Cart</h1>
        
        <?php if (empty($cartItems)): ?>
            <div class="cart-empty">
                <p>Your cart is empty.</p>
                <a href="<?php echo SITE_URL; ?>/products.php" class="btn btn-primary">Continue Shopping</a>
            </div>
        <?php else: ?>
            <table class="cart-table">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Price</th>
                        <th>Quantity</th>
                        <th>Total</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($cartItems as $key => $item): ?>
                        <tr>
                            <td data-label="Product">
                                <div class="cart-product-info">
                                    <img src="<?php echo htmlspecialchars($item['image_url']); ?>" alt="<?php echo htmlspecialchars($item['name']); ?>" class="cart-product-image">
                                    <div>
                                        <h4><?php echo htmlspecialchars($item['name']); ?></h4>
                                        <?php if ($item['size']): ?>
                                            <p style="color: var(--medium-gray); font-size: 0.875rem;">Size: <?php echo htmlspecialchars($item['size']); ?></p>
                                        <?php endif; ?>
                                        <?php if ($item['color']): ?>
                                            <p style="color: var(--medium-gray); font-size: 0.875rem;">Color: <?php echo htmlspecialchars($item['color']); ?></p>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </td>
                            <td data-label="Price"><?php echo formatPrice($item['price']); ?></td>
                            <td data-label="Quantity">
                                <form method="POST" style="display: flex; align-items: center; gap: var(--spacing-sm);">
                                    <input type="hidden" name="action" value="update">
                                    <input type="hidden" name="key" value="<?php echo htmlspecialchars($key); ?>">
                                    <input type="number" name="quantity" value="<?php echo $item['quantity']; ?>" min="1" max="99" style="width: 60px; padding: var(--spacing-sm);">
                                    <button type="submit" class="btn btn-small">Update</button>
                                </form>
                            </td>
                            <td data-label="Total"><?php echo formatPrice($item['price'] * $item['quantity']); ?></td>
                            <td>
                                <form method="POST">
                                    <input type="hidden" name="action" value="remove">
                                    <input type="hidden" name="key" value="<?php echo htmlspecialchars($key); ?>">
                                    <button type="submit" class="btn btn-danger btn-small">&times;</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--spacing-xl);">
                <form method="POST">
                    <input type="hidden" name="action" value="clear">
                    <button type="submit" class="btn btn-danger">Clear Cart</button>
                </form>
                <a href="<?php echo SITE_URL; ?>/products.php" class="btn btn-secondary">Continue Shopping</a>
            </div>
            
            <div class="cart-summary">
                <h3 style="margin-bottom: var(--spacing-lg);">Order Summary</h3>
                <div class="cart-summary-row">
                    <span>Subtotal</span>
                    <span><?php echo formatPrice($cartTotal); ?></span>
                </div>
                <div class="cart-summary-row">
                    <span>Shipping</span>
                    <span>Calculated at checkout</span>
                </div>
                <div class="cart-summary-row">
                    <span><strong>Total</strong></span>
                    <span><strong><?php echo formatPrice($cartTotal); ?></strong></span>
                </div>
                <a href="<?php echo SITE_URL; ?>/checkout.php" class="btn btn-primary btn-block" style="margin-top: var(--spacing-lg);">Proceed to Checkout</a>
            </div>
        <?php endif; ?>
    </div>
</section>

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