<!DOCTYPE html>
<html>
<head>
    <title>Registros Editables</title>
    <style>
	
        table {
            border-collapse: collapse;
            width: 100%;
        }

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

        th {
            background-color: #f2f2f2;
        }

        .editable {
            position: relative;
			background-color: #99CCCC;
            border: 1px solid #ccc;
            padding: 3px;
        }

        .editable input {
            width: 100%;
            background-color: #f9f9f9;
            border: 1px solid #ccc;
            padding: 3px;
        }

        /* Estilo para sombrear el texto editable */
        .editable.editing {
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
        }
	      td {
            padding: 5px;
        }

        th {
            background-color: #f2f2f2;
            padding: 5px;
        }

        input {
            width: 50%;
            background-color: #99CAABB;
            border: 2px solid #ccc;
            padding: 3px;
        }
        td {
            padding: 5px;
        }

        input {
            width: 50%;
        }
		input[type="submit"] {
        background-color: #ff9900; /* Cambia el color de fondo a tu elección */
        color: white;
        border: none;
        padding: 5px 10px;
        cursor: pointer;
    }

    input[type="submit"]:hover {
        background-color: #ffcc00; /* Cambia el color de fondo al pasar el mouse */
    }
	
    </style>
	
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	
    <script>
	
	  function openPopup(url) {
            var width = 800;
            var height = 600;
            var left = (window.innerWidth - width) / 2;
            var top = (window.innerHeight - height) / 2;

            var popupOptions = 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top;
            var newWindow = window.open(url, '_blank', popupOptions);
            newWindow.focus();
        }
	
        $(document).ready(function() {
            $("td.editable").on("click", function() {
                var originalContent = $(this).text();
                $(this).html("<input type='text' value='" + originalContent + "' />");
                $(this).children().first().focus();

                $(this).children().first().keypress(function(e) {
                    if (e.which == 13) {
                        var newContent = $(this).val();
                        var column = $(this).parent().data("column");
                        var id = $(this).parent().data("id");

                        $.post("update_entrega.php", {
                            newContent: newContent,
                            column: column,
                            id: id
                        }, function(data) {
                            $("td.editable[data-id='" + id + "'][data-column='" + column + "']").text(newContent);
                            if (column === "item_quantity" || column === "item_price") {
                                updateImporte(id);
                            }
                        });
                    }
                });

                $(this).children().first().blur(function() {
                    $(this).parent().text(originalContent);
                });
            });

            function updateImporte(id) {
                var itemQuantity = parseFloat($("td[data-id='" + id + "'][data-column='item_quantity']").text());
                var itemPrice = parseFloat($("td[data-id='" + id + "'][data-column='item_price']").text());
                var importe = itemQuantity * itemPrice;
                $("td[data-id='" + id + "'][data-column='importe']").text(importe.toFixed(2));
                
                var column = "item_total";
                var newContent = importe.toFixed(2);
                $.post("update_entrega.php", {
                    newContent: newContent,
                    column: column,
                    id: id
                }, function(data) {
                    $("td[data-id='" + id + "'][data-column='" + column + "']").text(newContent);
                    updateSubtotal();
                });
            }

            
        });
		
		
    </script>
	
</head>
<body>
    <p>
      <?php
    require_once "conexion.php";

    if (isset($_GET["condicion"])) {
        $condicion = $_GET["condicion"];
    } else {
        $condicion = $invoice['invoice_id']; // Condición por defecto si no se ha enviado nada por GET
    }
 $sql = "SELECT si.*, pts.sell_price AS current_price FROM selling_item si
            LEFT JOIN product_to_store pts ON si.item_id = pts.product_id
			WHERE invoice_id = $condicion"; // Cambia "selling_item" y "product_to_store" según tus necesidades

    //$sql = "SELECT * FROM selling_item WHERE invoice_id = $condicion"; // Cambia "tabla" según tus necesidades
    $result = $conn->query($sql);
 
    if ($result->num_rows > 0) {
        echo "<table>";
		  echo "<thead>";
        echo "<tr>";
		echo "<tr><th colspan='7'>REFERENCIA: (0 No controlado) - (1 Despachado ) - (2  Entregado) - (3 Pendiente) </th></tr>";

		echo "</tr>";
		echo "<tr>";
		echo "<th>Id Item</th>";
		echo "<th>cod Prod</th>";
        echo "<th>Nombre del articulo</th>";
		
        echo "<th>Cantidad</th>";
        
		echo "<th>Precio Actual</th>";
		echo "<th>Importe</th>";
		echo "<th>estado</th>";
		

		//echo "<th>Importe Act</th>";
        // Agrega más encabezados aquí si es necesario
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";
        while ($row = $result->fetch_assoc()) {
		
		$taxrate = get_the_taxrate($row['taxrate_id']);
            echo "<tr>";
			echo "<td data-column='item_name' data-id='" . $row["id"] . "'>" . $row["id"] . "</td>";
			echo "<td data-column='item_name' data-id='" . $row["id"] . "'>" . $row["item_id"] . "</td>";
			echo "<td data-column='item_name' data-id='" . $row["id"] . "'>" . $row["item_name"] . "</td>";
			
           
		   echo "<td data-column='item_quantity' data-id='" . $row["id"] . "'>" . number_format($row["item_quantity"]) . "</td>";
		  
           
            echo "<td>" . ((($row["current_price"] * $taxrate['taxrate'])/100) + $row["current_price"]) . "</td>";
            echo "<td data-column='importe' data-id='" . $row["id"] . "'>" . ($row["item_quantity"] * $row["item_price"]) . "</td>";
			 echo "<td class='editable' data-column='tipo' data-id='" . $row["id"] . "'>" . $row["tipo"] . "</td>";
			
            //echo "<td data-column='item_total' data-id='" . $row["id"] . "'>" . $row["item_total"] . "</td>";
            // Agrega más columnas aquí si es necesario
            echo "</tr>";
        }
		echo "</tbody>";
        echo "<tfoot>";
        echo "<tr>";
        echo "<td class='result' colspan='3'></td>";
        echo "<td class='result' colspan='3'></td>";
		
        echo "</tr>";
        echo "</tfoot>";
        echo "</table>";
    } else {
        echo "No se encontraron registros.";
    }

    $conn->close();
    ?>
	



	
</body>
</html>

</html>
