<?php
session_start();
require_once('../connection.php');


$first_name = $_SESSION['first_name'];
$email = $_SESSION['email'];
$customerID = $_SESSION['userID'];

$cart_items = $_SESSION['cart'] ?? [];
$result = null;

if (!empty($cart_items)) {
    $ids = implode(',', array_keys($cart_items));
    
    $sql = "SELECT * FROM products WHERE productID IN ($ids)";
    $result = $conn->query($sql);
}
?>




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GLH | Your Basket</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
    
    <style>
  
        .navbar-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 5px; 
            border-bottom: 1px solid #ddd;
            background-color: #90d5ff;
        }
        
        .logo { 
            display: block;
            height: 40px;
            width: auto;
        }
        
        .navbar {
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap; 
            gap: 80px;           
        }
        
        .navbar a {
            text-decoration: none;
            color: black;
            position: relative;
            padding-bottom: 2px;
        }
        
        .navbar a::after{
            content: '';
            position: absolute;
            width: 100%;
            transform: scaleX(0);
            height: 2px;
            bottom: 0;
            left: 0;
            background-color: black;
            transition: transform 0.25s ease-out;
        }

        .navbar a:hover::after {
            transform: scaleX(1);
        }
        
        .account {
            justify-content: flex-end;
        }
        
        .account button {
            background: white;
            border: 0px solid #ccc;
            padding: 5px 15px;
            border-radius: 5px;
            cursor: pointer;
        }
        
        .account button:hover {
            background-color: #f0f0f0;
            transition: 0.3s;
        }
        
        
        .logo, .account {
            flex: 1; 
             display: flex;
        }
        

        @media (max-width: 576px) {
            .navbar a {
            font-size: 14px;
            }
            
            .navbar {
                gap: 20px
            }
            
            .logo img {
                width: 90px;
                height: auto;
            }
            
        }
        
        
        .rows {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 15px;
            width: 95%; 
            max-width: 1200px;
            margin: 0 auto;
            padding-top: 60px;
            padding-bottom: 30px;
        }
        

        @media (min-width: 768px) {
        .rows {
            grid-template-columns: repeat(3, 1fr);
            width: 50%;
        }
        }
        
        .add-to-cart {
            background-color: #90d5ff;
            width: 100%;
            border-color: transparent;
            border-radius: 3px;
        }
        
        .add-to-cart:hover {
            transform: scale(1.05);
            transition: transform .2s;
        }
        
        .card-price {
            color: #3b3b3b;   
            font-weight: bold;
        }
        
        .card-stock {
            font-size: 12px;
            color: #3b3b3b;
        }
        
        .basket-summary {
            display: flex;
            
        }
    
    
        
    </style>
</head>
    <body>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
        
        
        <header class="navbar-container">
            
            <div class="logo">
                <a href="index.php">
                    <img src="../glh.png" class="logo" alt="logo" width="50" height="15">
                </a>
            </div>
        
            <div class="navbar">
                <a class="market" href="/glh/market/market.php">Market</a>
                <a class="dashboard" href="/glh/dashboard/dashboard.php">Dashboard</a>
                <a class="basket" href="/glh/market/basket.php">Basket</a>
            </div>
            
            <div class="account">
                <a href="/glh/login/login-form.php">
                    <button>Login <i class="bi bi-person-circle"></i></button>
                </a>
            </div>
            
        </header>
        
        
        
        <main class="basket">
            <br><h2>&nbsp;&nbsp;Your Shopping Basket</h2>
            <div class="rows">
                <?php
                    $grand_total = 0;

                    if ($result && $result->num_rows > 0) {
                        while($row = $result->fetch_assoc()) {
                            $id = $row['productID'];
                            $quantity = $_SESSION['cart'][$id];
                            $subtotal = $row['product_price'] * $quantity;
                            $grand_total += $subtotal;
                ?>
                
                
                <div class="columns">
                    <div class="card basket-item">
                        <img src="<?php echo htmlspecialchars($row['product_image'] ?? 'placeholder.jpg'); ?>" class="card-img-top" alt="Product Image">
                        
                        <div class="card-body">
                            <h5 class="card-title"><?php echo htmlspecialchars($row['product_name']); ?></h5>
                            <p class="card-text">Price: £<?php echo number_format($row['product_price'], 2); ?></p>
                            <p class="card-text">Quantity: <strong><?php echo $quantity; ?></strong></p>
                            <h6 class="card-subtotal">Subtotal: £<?php echo number_format($subtotal, 2); ?></h6>
                        </div>

                        <div class="card-footer">
                            <a href="remove_from_cart.php?id=<?php echo $id; ?>" class="btn-remove">Remove</a>
                        </div>
                    </div>
                </div>
                
                
                
                <?php
                        }
                ?>
                
                <div class="basket-summary">
                    <form action="checkout.php" method="POST">
                        <hr>
                        <h2>Total to Pay: £<?php echo number_format($grand_total, 2); ?></h2>
                        <input type="hidden" name="productID" value="<?php echo $row['productID']; ?>">
                        <input type="hidden" name="product_name" value="<?php echo $row['product_name']; ?>">
                        <input type="hidden" name="subtotal" value="<?php echo $subtotal ?>">
                        <input type="hidden" name="quantity" value="<?php echo $quantity ?>">
                        <input type="hidden" name="grand_total" value="<?php echo $grand_total ?>">
                        <input type="hidden" name="customerID" value="<?php echo $customerID ?>">
                        
                        <button type="submit" class="checkout-btn">Proceed to Checkout</button>
                    </form>
                </div>
                <?php
                } else {
                    echo "<p>Your basket is currently empty.</p>";
                    echo "<a href='market.php'>Go back to shopping</a>";
                }
                ?>
            </div>
        </main>
            
    
    </body>
</html>