<?php
require_once 'config.php';

$cart_items = getCartItems($pdo);
$cart_total = getCartTotal($pdo);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shopping Cart - EliteEquip</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="style.css" rel="stylesheet">
    <style>
        .cart-page {
            margin-top: 100px;
            margin-bottom: 50px;
            min-height: 60vh;
        }
        
        .cart-container {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: var(--shadow-md);
        }
        
        .cart-header {
            display: grid;
            grid-template-columns: 3fr 1fr 1fr 0.5fr;
            gap: 15px;
            padding: 15px 0;
            border-bottom: 2px solid #e2e8f0;
            font-weight: 700;
            color: var(--text-dark);
            margin-bottom: 20px;
        }
        
        .cart-item {
            display: grid;
            grid-template-columns: 3fr 1fr 1fr 0.5fr;
            gap: 15px;
            align-items: center;
            padding: 20px 0;
            border-bottom: 1px solid #e2e8f0;
        }
        
        .cart-item-product {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .cart-item-image {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            flex-shrink: 0;
        }
        
        .cart-item-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .cart-item-image i {
            font-size: 2rem;
            color: white;
        }
        
        .cart-item-info h4 {
            font-size: 1rem;
            font-weight: 700;
            margin-bottom: 5px;
            color: var(--text-dark);
        }
        
        .cart-item-info p {
            font-size: 0.85rem;
            color: var(--text-gray);
            margin: 0;
        }
        
        .quantity-control {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .quantity-btn {
            width: 32px;
            height: 32px;
            border: 1px solid var(--secondary);
            background: white;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .quantity-btn:hover {
            background: var(--secondary);
            color: white;
        }
        
        .cart-item-price,
        .cart-item-subtotal {
            font-weight: 600;
            color: var(--text-dark);
        }
        
        .cart-item-price {
            color: var(--secondary);
        }
        
        .remove-btn {
            background: none;
            border: none;
            color: var(--danger);
            cursor: pointer;
            font-size: 1.2rem;
            transition: all 0.3s;
        }
        
        .remove-btn:hover {
            color: #dc2626;
            transform: scale(1.1);
        }
        
        .cart-summary {
            background: var(--bg-light);
            border-radius: 20px;
            padding: 25px;
            position: sticky;
            top: 100px;
        }
        
        .cart-summary h4 {
            font-size: 1.3rem;
            font-weight: 700;
            margin-bottom: 20px;
            color: var(--text-dark);
        }
        
        .summary-row {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
        }
        
        .summary-total {
            border-top: 2px solid #e2e8f0;
            padding-top: 15px;
            margin-top: 10px;
            font-size: 1.2rem;
            font-weight: 800;
            color: var(--secondary);
        }
        
        .empty-cart {
            text-align: center;
            padding: 60px 20px;
        }
        
        .empty-cart i {
            font-size: 4rem;
            color: var(--text-gray);
            margin-bottom: 20px;
        }
        
        .empty-cart h3 {
            font-size: 1.5rem;
            margin-bottom: 10px;
        }
        
        @media (max-width: 768px) {
            .cart-header {
                display: none;
            }
            
            .cart-item {
                grid-template-columns: 1fr;
                gap: 15px;
                padding: 20px;
                background: white;
                border-radius: 15px;
                margin-bottom: 15px;
                box-shadow: var(--shadow-sm);
            }
            
            .cart-item-product {
                flex-direction: column;
                text-align: center;
            }
            
            .quantity-control {
                justify-content: center;
            }
            
            .cart-item-price,
            .cart-item-subtotal {
                text-align: center;
                padding: 5px 0;
            }
            
            .remove-btn {
                position: absolute;
                top: 10px;
                right: 10px;
            }
            
            .cart-item {
                position: relative;
            }
        }
        
        @media (max-width: 480px) {
            .cart-container {
                padding: 15px;
            }
        }
    </style>
</head>
<body>

<div id="loadingOverlay" class="loading-overlay">
    <div class="loader-container">
        <div class="loader"></div>
        <p>Loading...</p>
    </div>
</div>

<div id="toastContainer" class="toast-container"></div>

<!-- Navigation -->
<nav class="navbar navbar-expand-lg fixed-top">
    <div class="container">
        <a class="navbar-brand" href="index.php">
            <div class="logo-icon">
                <i class="fas fa-microchip"></i>
            </div>
            <span>Elite<span class="text-warning">Equip</span></span>
        </a>
        <div class="cart-icon-wrapper">
            <a href="cart.php" class="cart-icon">
                <i class="fas fa-shopping-cart"></i>
                <span class="cart-count" id="cartCount"><?php echo getCartCount($pdo); ?></span>
            </a>
        </div>
    </div>
</nav>

<div class="container cart-page">
    <div class="row">
        <div class="col-lg-8">
            <div class="cart-container">
                <h2 class="mb-4">Shopping Cart</h2>
                
                <?php if (empty($cart_items)): ?>
                    <div class="empty-cart">
                        <i class="fas fa-shopping-cart"></i>
                        <h3>Your cart is empty</h3>
                        <p>Looks like you haven't added any items to your cart yet.</p>
                        <a href="index.php#products" class="btn btn-primary mt-3">Browse Products</a>
                    </div>
                <?php else: ?>
                    <!-- Desktop Header -->
                    <div class="cart-header">
                        <div>Product</div>
                        <div>Quantity</div>
                        <div>Price</div>
                        <div>Total</div>
                    </div>
                    
                    <!-- Cart Items -->
                    <?php foreach ($cart_items as $item): 
                        $imagePath = !empty($item['image_url']) ? $item['image_url'] : null;
                        $hasImage = $imagePath && file_exists($imagePath);
                    ?>
                    <div class="cart-item">
                        <div class="cart-item-product">
                            <div class="cart-item-image">
                                <?php if ($hasImage): ?>
                                    <img src="<?php echo htmlspecialchars($imagePath); ?>" alt="<?php echo htmlspecialchars($item['name']); ?>">
                                <?php else: ?>
                                    <i class="fas fa-cogs"></i>
                                <?php endif; ?>
                            </div>
                            <div class="cart-item-info">
                                <h4><?php echo htmlspecialchars($item['name']); ?></h4>
                                <p><?php echo substr(htmlspecialchars($item['description']), 0, 50); ?>...</p>
                            </div>
                        </div>
                        
                        <div class="quantity-control">
                            <button class="quantity-btn" onclick="updateQuantity(<?php echo $item['id']; ?>, -1)">
                                <i class="fas fa-minus"></i>
                            </button>
                            <span style="min-width: 30px; text-align: center;"><?php echo $item['quantity']; ?></span>
                            <button class="quantity-btn" onclick="updateQuantity(<?php echo $item['id']; ?>, 1)">
                                <i class="fas fa-plus"></i>
                            </button>
                        </div>
                        
                        <div class="cart-item-price">
                            $<?php echo number_format($item['price'], 2); ?>
                        </div>
                        
                        <div class="cart-item-subtotal">
                            $<?php echo number_format($item['price'] * $item['quantity'], 2); ?>
                        </div>
                        
                        <button class="remove-btn" onclick="removeFromCart(<?php echo $item['id']; ?>)">
                            <i class="fas fa-trash-alt"></i>
                        </button>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
        
        <?php if (!empty($cart_items)): ?>
        <div class="col-lg-4">
            <div class="cart-summary">
                <h4>Order Summary</h4>
                
                <div class="summary-row">
                    <span>Subtotal</span>
                    <span>$<?php echo number_format($cart_total, 2); ?></span>
                </div>
                
                <div class="summary-row">
                    <span>Shipping</span>
                    <span>Free</span>
                </div>
                
                <div class="summary-row">
                    <span>Tax</span>
                    <span>$0.00</span>
                </div>
                
                <div class="summary-total">
                    <span>Total</span>
                    <span>$<?php echo number_format($cart_total, 2); ?></span>
                </div>
                
                <a href="checkout.php" class="btn btn-primary w-100 mt-4">
                    <i class="fas fa-lock"></i> Proceed to Checkout
                </a>
                
                <a href="index.php#products" class="btn btn-outline-secondary w-100 mt-3">
                    <i class="fas fa-arrow-left"></i> Continue Shopping
                </a>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Footer -->
<footer class="footer mt-5">
    <div class="container">
        <div class="row">
            <div class="col-lg-4">
                <div class="footer-logo">
                    <i class="fas fa-microchip"></i>
                    <span>Elite<span class="text-warning">Equip</span></span>
                </div>
                <p>Providing premium industrial equipment solutions since 2024.</p>
            </div>
            <div class="col-lg-4">
                <h4>Quick Links</h4>
                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="index.php#products">Products</a></li>
                    <li><a href="cart.php">Cart</a></li>
                </ul>
            </div>
            <div class="col-lg-4">
                <h4>Contact</h4>
                <p>
                    <i class="fas fa-envelope"></i> sales@eliteequip.com<br>
                    <i class="fas fa-phone"></i> +1 (555) 123-4567
                </p>
            </div>
        </div>
        <div class="footer-bottom">
            <p>&copy; 2024 EliteEquip. All rights reserved.</p>
        </div>
    </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="script.js"></script>
</body>
</html>