<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Carrito de Compras</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #212733;
            color: #ffffff;
        }

        header {
            background-color: #ffffff;
            color: #ffffff;
            padding: 15px 30px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            position: relative;
            z-index: 1;
        }

        .divheader {
            display: flex;
            align-items: center;
            transition: filter 0.3s;
            margin-left: 30px;
            margin-right: 30px;
        }

        .divheader:hover {
            filter: brightness(1.2);
        }

        #germain {
            display: flex;
            align-items: center;
            text-decoration: none;
        }

        .logo-img {
            width: 50px;
            margin-right: 10px;
        }

        .logo-text {
            height: 40px;
        }


        .left-header {
            display: flex;
            align-items: center;
        }

        h1 {
            margin: 0;
            font-size: 1.5em;
        }

        main {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 80vh;
            padding: 20px;
            box-sizing: border-box;
        }

        .cart-container {
            background-color: #ffffff;
            color: #212733;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 20px;
            max-width: 800px;
            width: 100%;
            box-sizing: border-box;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        th,
        td {
            padding: 10px;
            border-bottom: 1px solid #ddd;
            text-align: left;
        }

        th {
            background-color: #3270E3;
            color: #ffffff;
        }

        td input {
            width: 50px;
            text-align: center;
        }

        .cart-total {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 20px;
        }

        .checkout-btn {
            padding: 10px 20px;
            background-color: #3270E3;
            border: none;
            border-radius: 5px;
            color: #ffffff;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.3s;
        }

        .checkout-btn:hover {
            background-color: #301c41;
            transform: translateY(-2px);
        }

        .remove-btn {
            padding: 5px 10px;
            background-color: #3270E3;
            border: none;
            border-radius: 5px;
            color: #ffffff;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.3s;
        }

        .remove-btn:hover {
            background-color: #3270E3;
            transform: translateY(-2px);
        }
    </style>
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-lg navbar-light bg-glass fixed-top">
            <div class="container-fluid">
                <a href="/agregar-producto"><button class="btn btn-primary">Vende tu producto</button></a>
                <a href="/carrito-compra"><button class="btn btn-primary">Carrito<i class="fas fa-shopping-cart"></i></button></a>
                <a href="/conocenos"><img src="/uploads/logo.jpeg" alt="Logo" class="logo"></a>
            </div>
        </nav>
    </header>

    <main>
        <div class="cart-container">
            <table>
                <thead>
                    <tr>
                        <th>Producto</th>
                        <th>Precio</th>
                        <th>Cantidad</th>
                        <th>Subtotal</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody id='tabla_productos'>
                </tbody>
            </table>

            <div id='cart-total' class="cart-total">
            </div>
        </div>
    </main>
    <script>

        async function fetchAndShowData() {

            const results = await fetch('http://localhost:3001/productos-carrito-compra');
            const productosEnCarrito = await results.json();
            console.log(productosEnCarrito);

            productos = productosEnCarrito[0].productos_comprados;

            console.log(productos, 'typeof: ', typeof productos);

            productosJSON = JSON.parse(productos);

            console.log(productosJSON);

            const tablaProductos = document.getElementById('tabla_productos');

            for(it in productosJSON){
                console.log(productosJSON[it]);

                console.log(productosJSON[it].nombre);

                const tr = document.createElement('tr');

                let labelForTd = '';

                const tdNombre = document.createElement('td');
                const labelNombre = document.createTextNode(productosJSON[it].nombre);
                tdNombre.appendChild(labelNombre);

                const tdPrecio = document.createElement('td');
                const labelPrecio = document.createTextNode('$'+productosJSON[it].precio);
                tdPrecio.appendChild(labelPrecio);

                const tdCantidad = document.createElement('td');
                const labelCantidad = document.createTextNode(productosJSON[it].cantidad);
                tdCantidad.appendChild(labelCantidad);

                const tdSubtotal = document.createElement('td');
                const labelSubtotal = document.createTextNode('$' + (productosJSON[it].precio * productosJSON[it].cantidad));
                tdSubtotal.appendChild(labelSubtotal);

                tr.appendChild(tdNombre);
                tr.appendChild(tdPrecio);
                tr.appendChild(tdCantidad);
                tr.appendChild(tdSubtotal);

                tablaProductos.appendChild(tr);

            }

            const div_total = document.getElementById('cart-total');

            const h3 = document.createElement('h3');
            let label = 'Total: $' + productosEnCarrito[0].precio_total;
            let labelTotal = document.createTextNode(label);
            h3.appendChild(labelTotal);
            
            const button = document.createElement('button');
            button.setAttribute("class", "checkout-btn")
            labelTotal = document.createTextNode('Pagar');
            button.append(labelTotal);
            
            div_total.appendChild(h3);
            div_total.appendChild(button);

        }
        
        fetchAndShowData();

    </script>
</body>
</html>
