<?php
// Archivo: obtener_items_factura.php
include 'conexion.php';

$invoice_id = $_GET['invoice_id'] ?? '';
if (!$invoice_id) {
    echo "<div class='alert alert-warning'>Factura no especificada.</div>";
    exit;
}

$sql = "SELECT item_name, item_quantity, item_price, item_total, item_discount, item_tax FROM selling_item WHERE invoice_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $invoice_id);
$stmt->execute();
$res = $stmt->get_result();

if ($res->num_rows == 0) {
    echo "<div class='alert alert-info'>No se encontraron ítems para esta factura.</div>";
    exit;
}

echo "<table class='table table-bordered'>
    <thead>
        <tr>
            <th>Ítem</th>
            <th>Cantidad</th>
            <th>Precio Unitario</th>
            <th>Descuento</th>
            <th>Impuesto</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>";
while ($row = $res->fetch_assoc()) {
    echo "<tr>
        <td>".htmlspecialchars($row['item_name'])."</td>
        <td>".number_format($row['item_quantity'], 2)."</td>
        <td>".number_format($row['item_price'], 2)."</td>
        <td>".number_format($row['item_discount'], 2)."</td>
        <td>".number_format($row['item_tax'], 2)."</td>
        <td>".number_format($row['item_total'], 2)."</td>
    </tr>";
}
echo "</tbody></table>";
