<?php
    require_once 'config.php'; 
    include 'session_check.php';

    // zistí či je používateľ prihlásený
    if (!$is_logged_in) {
        $_SESSION['error_message'] = 'You must be logged in to view your cart.';
        header('Location: account.php');
        exit;
    }

    // získa ID používateľa a pripraví premenné
    $user_id = $_SESSION['user_id'];
    $cart_items = [];
    $total_price = 0;

    try {
        // načítanie položiek košíka z databázy
        $stmt = $pdo->prepare("
            SELECT
                c.id as cart_id,
                c.product_id,
                c.quantity,
                c.size,
                p.name,
                p.price,
                p.product_gallery
            FROM cart c
            JOIN products p ON c.product_id = p.id
            WHERE c.user_id = ?
            ORDER BY c.created_at ASC
        ");
        $stmt->execute([$user_id]);
        $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);

        // výpočet celkovej ceny
        foreach ($cart_items as $item) {
            $total_price += $item['price'] * $item['quantity'];
        }

    } catch (PDOException $e) {
        error_log("Error fetching cart items: " . $e->getMessage());
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shopping Cart - ShoeBox</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,500;0,600;0,700;1,300&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="page-wrapper">
    <div class="header" style="background: #fff !important;">
        <div class="container">
            <div class="navbar">
                <div class="logo">
                    <a href="index.php"><img src="obrazky/logo.png" width="125px"></a>
                </div>
                <nav>
                    <ul id="MenuItems">
                        <li><a href="index.php">Home</a></li>
                        <li><a href="products.php">Product</a></li>
                        <li><a href="about.php">About</a></li>
                        <li><a href="contact.php">Contact</a></li>
                        <?php if ($is_logged_in): ?>
                            <li><a href="account.php"><?php echo htmlspecialchars($username); ?></a></li>
                            <?php if ($is_admin): ?><li><a href="admin/index.php">Admin</a></li><?php endif; ?>
                            <li><a href="logout.php">Logout</a></li>
                        <?php else: ?>
                            <li><a href="account.php">Login/Register</a></li>
                        <?php endif; ?>
                    </ul>
                </nav>
                <a href="cart.php"><img src="obrazky/cart.svg" class="cart-icon" width="30px" height="30px"></a>
                <img src="obrazky/menu.png" class="menu-icon" onclick="menutoggel()">
            </div>
        </div>
    </div>

    
    <div class="small-container" style="text-align: center; margin-top: 10px; margin-bottom: 10px;">
        <?php
        if (isset($_SESSION['success_message'])) {
            echo '<p style="color: green;">' . htmlspecialchars($_SESSION['success_message']) . '</p>';
            unset($_SESSION['success_message']);
        }
        if (isset($_SESSION['error_message'])) {
            echo '<p style="color: red;">' . htmlspecialchars($_SESSION['error_message']) . '</p>';
            unset($_SESSION['error_message']);
        }
        ?>
    </div>

    
    <div class="small-container cart-page">
        <?php if (empty($cart_items)): ?>
            <h2>Your cart is empty.</h2>
            <p><a href="products.php" class="btn">Continue Shopping</a></p>
        <?php else: ?>
            <table>
                <tr>
                    <th>Product</th>
                    <th>Details</th>
                    <th>Subtotal</th>
                </tr>
                <?php foreach ($cart_items as $item): ?>
                <tr>
                    <td>
                        <div class="cart-info">
                            <a href="product_detail.php?id=<?= htmlspecialchars($item['product_id']) ?>">
                                <img src="<?= image_url_src(product_main_image($item['product_gallery'] ?? null)) ?>" alt="<?= htmlspecialchars($item['name']) ?>" style="width: 80px; height: auto; margin-right: 10px;">
                            </a>
                            <div>
                                <p style="margin-bottom: 5px;"><a href="product_detail.php?id=<?= htmlspecialchars($item['product_id']) ?>"><?= htmlspecialchars($item['name']) ?></a></p>
                                <small>Price: €<?= htmlspecialchars(number_format($item['price'], 2)) ?></small><br>
                                <small>Size: <?= htmlspecialchars($item['size']) ?></small>
                                <br>
                                
                                <form action="remove_from_cart.php" method="POST" style="display: inline; margin-top: 5px;">
                                     <input type="hidden" name="product_id" value="<?= htmlspecialchars($item['product_id']) ?>">
                                     <input type="hidden" name="size" value="<?= htmlspecialchars($item['size']) ?>">
                                     <button type="submit" class="remove-btn" style="background: #ff523b; color: white; border: none; padding: 3px 8px; font-size: 12px; cursor: pointer;">Remove</button>
                                </form>
                            </div>
                        </div>
                    </td>
                    <td>
                        
                         <form action="update_cart.php" method="POST" class="update-cart-form">
                             <input type="hidden" name="cart_id" value="<?= htmlspecialchars($item['cart_id']) ?>">

                             <label for="quantity_<?= $item['cart_id'] ?>" style="display:block; margin-bottom: 5px;">Quantity:</label>
                             <input id="quantity_<?= $item['cart_id'] ?>" type="number" name="quantity" value="<?= htmlspecialchars($item['quantity']) ?>" min="1" max="<?= CART_MAX_QUANTITY_PER_ITEM ?>" class="cart-quantity-input" style="width: 50px; height: 30px; padding: 5px; margin-bottom: 10px;">
                             
                             <label for="size_<?= $item['cart_id'] ?>" style="display:block; margin-bottom: 5px;">Size (EU):</label>
                             <select id="size_<?= $item['cart_id'] ?>" name="size" required style="display: block; margin-bottom: 10px; padding: 5px; height: 30px;">
                                <option value="38" <?= ($item['size'] == '38') ? 'selected' : '' ?>>38</option>
                                <option value="39" <?= ($item['size'] == '39') ? 'selected' : '' ?>>39</option>
                                <option value="40" <?= ($item['size'] == '40') ? 'selected' : '' ?>>40</option>
                                <option value="41" <?= ($item['size'] == '41') ? 'selected' : '' ?>>41</option>
                                <option value="42" <?= ($item['size'] == '42') ? 'selected' : '' ?>>42</option>
                                <option value="43" <?= ($item['size'] == '43') ? 'selected' : '' ?>>43</option>
                                <option value="44" <?= ($item['size'] == '44') ? 'selected' : '' ?>>44</option>
                                <option value="45" <?= ($item['size'] == '45') ? 'selected' : '' ?>>45</option>
                                <option value="46" <?= ($item['size'] == '46') ? 'selected' : '' ?>>46</option>
                                <option value="47" <?= ($item['size'] == '47') ? 'selected' : '' ?>>47</option>
                                <option value="48" <?= ($item['size'] == '48') ? 'selected' : '' ?>>48</option> 
                                <option value="49" <?= ($item['size'] == '49') ? 'selected' : '' ?>>49</option>
                             </select>
                             
                             <button type="submit" class="btn-update" style="background: #5cb85c; color: white; border: none; padding: 5px 10px; cursor: pointer;">Update</button>
                         </form>
                    </td>
                    <td style="text-align: right;">€<?= htmlspecialchars(number_format($item['price'] * $item['quantity'], 2)) ?></td>
                </tr>
                <?php endforeach; ?>
            </table>

            <div class="total-price">
                <table>
                    <tr>
                        <td>Subtotal</td>
                        <td>€<?= htmlspecialchars(number_format($total_price, 2)) ?></td>
                    </tr>
                    <tr>
                        <td>Tax (2%)</td>
                        <td>€<?= htmlspecialchars(number_format($total_price * 0.02, 2)) ?></td>
                    </tr>
                    <tr>
                        <td>Delivery</td>
                        <td>€<?= htmlspecialchars(number_format(5.00, 2)) ?></td>
                    </tr>
                    <tr>
                        <td>Total</td>
                         <td>€<?= htmlspecialchars(number_format($total_price + ($total_price * 0.02) + 5.00, 2)) ?></td>
                    </tr>
                     <tr>
                        <td colspan="2"><form action="checkout_process.php" method="POST" style="display: inline;"><button type="submit" class="btn">Proceed to Checkout &rarr;</button></form></td>
                    </tr>
                </table>
            </div>
        <?php endif; ?>
    </div>
    </div>
    
     <div class="footer">
        <div class="container">
            <div class="row">
                 <div class="footer-col-1">
                    <h3>Download Our App</h3>
                    <p>Download App for Android and IOS Mobile phone.</p>
                    <br>
                    <div class="app-logo">
                        <img src="obrazky/play-store.png">
                        <img src="obrazky/app-store.png">
                    </div>
                </div>
                <div class="footer-col-2">
                    <img src="obrazky/logo-white.png">
                    <p>Our Purpose Is To Sustainably Make the Pleasure and Benefits of Sports Accessible to the Many</p>
                </div>
                <div class="footer-col-3">
                    <h3>Useful Links</h3>
                    <ul>
                        <li>Cupons</li>
                        <li>Blog Post</li>
                        <li>Return Policy</li>
                        <li>Join Affliate</li>
                    </ul>
                </div>
                <div class="footer-col-4">
                    <h3>Follow us</h3>
                    <ul>
                        <li>Facebook</li>
                        <li>Twitter</li>
                        <li>Instagram</li>
                        <li>YouTube</li>
                    </ul>
                </div>
            </div>
            <hr>
            <p class="copyright">Copyright 2025 Alex Melicher</p>
        </div>
    </div>

    
    <script>
        var MenuItems = document.getElementById("MenuItems");
        MenuItems.style.maxHeight = "0px";
        function menutoggel() {
            if(MenuItems.style.maxHeight == "0px") {
                MenuItems.style.maxHeight = "200px"
            } else {
                MenuItems.style.maxHeight = "0px"
            }
        }
    </script>
</body>
</html> 