<?php
$codigo = $_POST['codigo'];
$nombre = $_POST['nombre'];
$precio = $_POST['precio'];
$totalCopias = $_POST['totalCopias'] ?? 9;
$usuario=$_POST["usuario"] ?? null;
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Imprimir Códigos de Barras</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.6/dist/JsBarcode.all.min.js"></script>
    <style>
        @page {
            size: 100mm 15mm;
            margin: 0;
        }

        body {
            margin: 0;
            padding: 0;
            font-family: "Roboto", sans-serif; /* 🔹 Fuente moderna */
        }

        .fila {
            width: 100mm;
            height: 15mm;
            display: flex;
            justify-content: space-between;
        }

        .sticker {
            width: 33.3mm;
            height: 15mm;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-align: center;
            overflow: hidden;
            font-family: "Roboto", sans-serif;
        }

        .sticker svg {
            max-height: 4.5mm;
            width: 85%;
            margin-top: 0.2mm;
        }

        .sticker p {
            margin: 0;
            padding: 0;
            line-height: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .sticker p.nombre {
            font-size: 6px;
            font-weight: 700; /* Roboto Bold */
            letter-spacing: 0.2px;
        }

        .sticker p.precio {
            font-size: 12px;
            font-weight: 700; /* Precio bien marcado */
            letter-spacing: 0.3px;
        }

        .sticker p.codigo {
            font-size: 6px;
            font-weight: 400;
            letter-spacing: 0.3px;
        }
    </style>
</head>
<body>
    <?php 
    $filas = ceil($totalCopias / 3);
    $contador = 0;
    for ($f = 0; $f < $filas; $f++): ?>
        <div class="fila">
            <?php for ($c = 0; $c < 3; $c++): ?>
                <?php if ($contador < $totalCopias): ?>
                    <div class="sticker">
                        <p class="nombre"><?= $nombre ?></p>
                       <?php if ($usuario != "planet") { ?>
                         <p class="precio"><?= number_format($precio, 0, ',', '.') ?></p>
                        <?php } ?>
                        <svg id="barcode<?= $contador ?>"></svg>
                        <p class="codigo"><?= $codigo ?></p>
                    </div>
                    <?php $contador++; ?>
                <?php else: ?>
                    <div class="sticker"></div>
                <?php endif; ?>
            <?php endfor; ?>
        </div>
    <?php endfor; ?>

    <script>
        for (let i = 0; i < <?= $totalCopias ?>; i++) {
            JsBarcode("#barcode" + i, "<?= $codigo ?>", {
                format: "CODE128",
                lineColor: "#000",
                width: 1,
                height: 16,
                margin: 0,
                displayValue: false
            });
        }
        setTimeout(() => window.print(), 300);
        window.onafterprint = () => window.close();
    </script>
</body>
</html>

