<section class="contenedor">
            <div class="contenedor-items">      
                <?php 
                    $query= "SELECT * FROM productos";
                    $result= mysqli_query($conectado,$query);



                    while($row = mysqli_fetch_array($result)) {?>
                    
                
                    
                    <form method="POST" class="kk" action="carrito.php?id=<?=$row['id'] ?>">
               
                            <div class="item">  
                                <img src="img/<?=$row['imagen']?>" alt="" class="img-item">
                                <span class="titulo-item"><?=$row['nombre']?></span>
                                
                                <input type="hidden" name="nombre" value="<?= $row ['nombre']?>">
                                <input type="hidden" name="imagen" value="<?= $row ['imagen']?>">
                                <input type="hidden" name="precio" value="<?= $row ['precio']?>">
                                <input type="number" name="quantity" value="1" class="form-control">
                                <input type="submit" name="anadealcarrito" class="btn btn-primary bnt-block my-2 btn-lg " value="Añadir">
                                <a href="detalles_productos.php?mas=<?php echo $row["id"]?>" name="mas" class="btn btn-primary bnt-block my-2 btn-lg"  value="ver mas">Ver detalles</a>
                       </div> 

                    
                      </form>
                      
                
                     <?php }


                     ?> 
                            </div>
                        </div>
                     </div>
                    
                        <?php
                        $total = 0;

                        $output = "
                            <div class='carrito' id='carrito'>
                                <div class='header-carrito'>
                                    <h2 class='titele'>Tu Carrito</h2>
                                    <span class='aquis'><i class='fa-solid fa-x' onclick='hacerInVisibleCarrito()'></i></span>
                                </div>
                    
                                <div class='carrito-items'>
                        "; 
                    
                        if (!empty($_SESSION['cart'])) {
                            foreach ($_SESSION['cart'] as $key => $value) {
                                $output .= "
                                    <div class='carrito-item' >
                                        <img src='img/" . $value['imagen'] . "' width='80px' alt=''>
                                        <div class='carrito-item-detalles'>
                                            <span class='carrito-item-titulo'>" . $value['id'] . "</span>
                                            <span class='carrito-item-titulo' data-header>" . $value['nombre'] . "</span>
                                            <div class='selector-cantidad' data-body></div>
                                            <span class='carrito-item-precio'>" . $value['quantity']. ' x  ' . $value['precio'] . '$' . "</span>
                                        </div>
                                        <form method='post' action='carrito.php'>
                                            <input type='hidden' name='remove_id' value='" . $value['id'] . "'>
                                            <button type='submit' class='btn-eliminar' name='eliminar_item'>
                                                <i class='fa-solid fa-trash'></i>
                                            </button>
                                        </form>
                                    </div>
                                ";
                    
                                $total = $total + $value['quantity'] * $value['precio'];
                            }
                        }
                    
                        $output .= "
                                </div>
                                <div class='carrito-total'>
                                    <div class='fila'>
                                        <strong>Tu Total</strong>
                                        <span class='carrito-precio-total'>$" . number_format($total, 2) . "</span>
                                    </div>
                                    <form method='post' action='carrito.php'>
                                        <button type='submit' class='btn-pagar' name='borrar_todo'>borrar<i class='fa-solid fa-bag-shopping'> </i></button>
                                    </form>
                                </div>
                            </div>
                        ";
                    
                        echo $output;
                    
                        // Manejar las acciones de eliminar
                        if (isset($_POST['eliminar_item'])) {
                            $remove_id = $_POST['remove_id'];
                            // Implementa la lógica para eliminar el artículo con el ID $remove_id del carrito
                            foreach ($_SESSION['cart'] as $key => $value) {
                                if ($value['id'] == $remove_id) {
                                    unset($_SESSION['cart'][$key]);
                                }
                            }
                        }
                    
                        // Manejar la acción de borrar todo
                        if (isset($_POST['borrar_todo'])) {
                            // Implementa la lógica para borrar todo el carrito
                            unset($_SESSION['cart']);
                        }
                        ?>

                      

          </div>      
    </section>
    <?php
    
    if(isset($_GET['action'])){
          

        if($_GET['action']=="clearall"){
            unset($_SESSION['cart']);
        }

        if($_GET['action']=="remove"){
            foreach($_SESSION['cart'] as $key => $value){
                if($value['id']==$_GET['id']){
                    unset($_SESSION['cart'][$key]);

                }
            }

        }
    }
    
    ?>

          
    </section>  
      