<style>

*{
    font-weight: 550;
    font-size: 14px;
    font-family: Arial, sans-serif;
    box-sizing: border-box;
}

#impresion{
    width: 210mm; /* A bit less than 216mm to account for printer margins */
    margin: 0 auto;
    padding: 20px;
    max-width: 100%;
    overflow-wrap: break-word;
}

#titulo{
    margin-bottom: 20px;
    text-align: center;
}

.header-info {
    width: 100%;
    margin-bottom: 20px;
    table-layout: auto;
}

.header-info td {
    vertical-align: top;
}

.empresa-info {
    text-align: center;
}

.bill-info {
    text-align: right;
}

h6{
    margin: 5px 0;
    font-size: 18px;
    padding: 0;
}

.item{
    margin-right: 1px;
}

p {
    margin: 2px 0;
    padding: 0;
}

.produ-table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    table-layout: auto;
    max-width: 100%;
}

.produ-table th, .produ-table td {
    border: 1px solid #000;
    padding: 8px;
    text-align: left;
}

.produ-table th {
    background-color: #f2f2f2;
}

/* Side-by-side container for IVA and Totals */
.summary-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    width: 100%;
    margin-top: 10px;
    gap: 20px;
}

.iva-section {
    flex: 1;
    font-size: 9px;
}

#totalll {
    width: 250px;
    display: block;
}

.iva-section .produ-table {
    margin: 5px 0;
    width: 100%;
}

.iva-section .produ-table th, 
.iva-section .produ-table td {
    padding: 2px 5px;
    font-size: 9px;
}

.iva-section h6 {
    font-size: 11px;
    margin: 2px 0;
    text-align: left;
}

#totalll table {
    width: 100%;
}

#totalll p {
    font-size: 14px;
}

#totalll h6 {
    font-size: 16px;
}

.total-section {
    clear: both;
    width: 100%;
}

@media print {
    body {
        margin: 0;
        padding: 0;
    }
    #impresion {
        width: 100%;
        margin: 0;
        padding: 10mm;
    }
}

</style>
<?php
$data = json_decode(file_get_contents("php://input"), true);
$bodega=$data["bodega"];
include("datosimpresion.php");

$idc=$data["idc"];
$nombrec=$data["nombrec"];
$fecha=$data["fecha"];
$factura=$data["numf"];
$tipodetalle=$data["tipodetalle"];
$produc=$data["factura"];
$fila = json_decode($produc, true);
$total=$data["total"];

//regimen comun
$iva5=(int)$data["iva5"];
$iva19=(int)$data["iva19"];
$base5=(int)$data["base5"];
$base19=(int)$data["base19"];
$excento=(int)$data["excento"];
$excluido=(int)$data["excluido"];
$ivaotro=(int)$data["ivaotro"];
$baseotro=(int)$data["baseotro"];
$totaliva=(int)$iva5+$iva19+$ivaotro;
$subtotal=$base19+$base5+$excento+$excluido+$baseotro;
?>

<div id="impresion">

    <table class="header-info">
        <tr>
            <td class="empresa-info">
                <h6><?php echo $nombreNegocio; ?></h6>
                <p><?php echo $direccion; ?></p>
                <p><?php echo $representante; ?></p>
                <p>Nit: <?php echo $nit; ?></p>
                <p><?php echo $regimen; ?></p>
                <p>Tel: <?php echo $tel; ?></p>
            </td>
        </tr>
    </table>

    <hr>

    <table class="header-info">
        <tr>
            <td>
                <p><strong>Cliente:</strong> <?php echo $idc; ?></p>
                <p><strong>Nombre:</strong> <?php echo $nombrec; ?></p>
            </td>
            <td class="bill-info">
                <p><strong>Factura Compra:</strong> <?php echo $factura; ?></p>
                <p><strong>Fecha:</strong> <?php echo $fecha; ?></p>
            </td>
        </tr>
    </table>

    <hr>

    <div id="titulo">
        <h6>Detalle de Compra</h6>
        <p><?php echo $tipodetalle; ?></p>
    </div>

    <?php
    $showBase = false;
    $showIva = false;
    $showTipo = false;

    foreach ($fila as $item) {
        if (isset($item['Base']) && $item['Base'] != 0) $showBase = true;
        if (isset($item['Iva']) && $item['Iva'] != 0) $showIva = true;
        if (isset($item['Tipo']) && !empty($item['Tipo'])) $showTipo = true;
    }
    ?>

    <table class="produ-table">
        <thead>
            <tr>
                <th>Código</th>
                <th>Descripción</th>
                <th class="text-right">Caja</th>
                <th class="text-right">Frcc</th>
                <?php if ($showBase) { ?><th class="text-right">Base</th><?php } ?>
                <?php if ($showIva) { ?><th class="text-right">IVA</th><?php } ?>
                <?php if ($showTipo) { ?><th class="text-right">Tipo</th><?php } ?>
                <th class="text-right">Precio</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($fila as $valor) { ?>
                <tr>
                    <td><?php echo $valor["Codigo"]; ?></td>
                    <td><?php echo $valor["Nombre"]; ?></td>
                    <td class="text-right"><?php echo $valor["Cantida_caja"]; ?></td>
                    <td class="text-right"><?php echo $valor["Cantida_frcc"]; ?></td>
                    <?php if ($showBase) { ?><td class="text-right"><?php echo isset($valor['Base']) ? number_format($valor['Base']) : '0'; ?></td><?php } ?>
                    <?php if ($showIva) { ?><td class="text-right"><?php echo isset($valor['Iva']) ? number_format($valor['Iva']) : '0'; ?></td><?php } ?>
                    <?php if ($showTipo) { ?><td class="text-right"><?php echo isset($valor['Tipo']) ? $valor['Tipo'] : ''; ?></td><?php } ?>
                    <td class="text-right"><?php echo number_format($valor["total"]); ?></td>
                </tr>
            <?php } ?>
        </tbody>
    </table>

    <div class="summary-wrapper">
        <div class="iva-section">
            <h6>Detalle IVA</h6>
            <?php
            $total5=$base5+$iva5;
            $totall9=$base19+$iva19;
            $totalotro=$ivaotro+$baseotro;
            ?>
            <table class="produ-table">
                <thead>
                    <tr>
                        <th>Tipo</th>
                        <th class="text-right">Base</th>
                        <th class="text-right">IVA</th>
                        <th class="text-right">Total</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($excento > 0) { ?>
                    <tr>
                        <td>Exento (0%)</td>
                        <td class="text-right"><?php echo number_format($excento); ?></td>
                        <td class="text-right">0</td>
                        <td class="text-right"><?php echo number_format($excento); ?></td>
                    </tr>
                    <?php } ?>
                    <?php if ($excluido > 0) { ?>
                    <tr>
                        <td>Excluido</td>
                        <td class="text-right"><?php echo number_format($excluido); ?></td>
                        <td class="text-right">0</td>
                        <td class="text-right"><?php echo number_format($excluido); ?></td>
                    </tr>
                    <?php } ?>
                    <?php if ($total5 > 0) { ?>
                    <tr>
                        <td>IVA 5%</td>
                        <td class="text-right"><?php echo number_format($base5); ?></td>
                        <td class="text-right"><?php echo number_format($iva5); ?></td>
                        <td class="text-right"><?php echo number_format($total5); ?></td>
                    </tr>
                    <?php } ?>
                    <?php if ($totall9 > 0) { ?>
                    <tr>
                        <td>IVA 19%</td>
                        <td class="text-right"><?php echo number_format($base19); ?></td>
                        <td class="text-right"><?php echo number_format($iva19); ?></td>
                        <td class="text-right"><?php echo number_format($totall9); ?></td>
                    </tr>
                    <?php } ?>
                    <?php if ($totalotro > 0) { ?>
                    <tr>
                        <td>Otros</td>
                        <td class="text-right"><?php echo number_format($baseotro); ?></td>
                        <td class="text-right"><?php echo number_format($ivaotro); ?></td>
                        <td class="text-right"><?php echo number_format($totalotro); ?></td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>

        <div id="totalll">
            <table>
                <tr>
                    <td><strong>Subtotal:</strong></td>
                    <td class="text-right"><?php echo number_format($subtotal); ?></td>
                </tr>
                <tr>
                    <td><strong>IVA:</strong></td>
                    <td class="text-right"><?php echo number_format($totaliva); ?></td>
                </tr>
                <tr>
                    <td><h6>Total:</h6></td>
                    <td class="text-right"><h6><?php echo number_format($total); ?></h6></td>
                </tr>
            </table>
        </div>
    </div>

    <hr>

    <div id="titulo">
        <!-- <h6>Muchas gracias por su compra</h6> -->
    </div>

</div>


<script type="text/javascript">
function imprimir(){
    window.print();
    let navegador = navigator.userAgent;
    if (!(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i))) {
        setTimeout(window.close, 300);
    }
}
setTimeout(imprimir, 300);
</script>
