<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Generador de Código de Barras</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        @media print {
            body * {
                visibility: hidden;
            }
            #codigoBarras, #codigoBarras * {
                visibility: visible;
            }
            #codigoBarras {
                position: absolute;
                left: 0;
                top: 0;
            }
        }
    </style>
</head>
<body class="bg-light">
    <div class="container mt-5">
        <h1 class="text-center mb-4">Generador de Código de Barras</h1>
        <form method="POST" action="" class="bg-white p-4 rounded shadow-sm">
            <div class="form-group">
                <label for="text">Texto (separar por comas):</label>
                <input type="text" id="text" name="text" class="form-control" required value="testing, ejemplo">
            </div>

            <div class="form-group">
                <label for="size">Tamaño:</label>
                <input type="number" id="size" name="size" class="form-control" value="20">
            </div>

            <div class="form-group">
                <label for="orientation">Orientación:</label>
                <select id="orientation" name="orientation" class="form-control">
                    <option value="horizontal">Horizontal</option>
                    <option value="vertical">Vertical</option>
                </select>
            </div>

            <div class="form-group">
                <label for="codetype">Tipo de Código:</label>
                <select id="codetype" name="codetype" class="form-control">
                    <option value="code128">Code 128</option>
                    <option value="code39">Code 39</option>
                    <option value="codabar">Codabar</option>
                </select>
            </div>

            <button type="submit" class="btn btn-primary">Generar Códigos de Barras</button>
        </form>

        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Obtener los parámetros del formulario
            $text = isset($_POST['text']) ? $_POST['text'] : "0";
            $size = isset($_POST['size']) ? $_POST['size'] : "20";
            $orientation = isset($_POST['orientation']) ? $_POST['orientation'] : "horizontal";
            $codetype = isset($_POST['codetype']) ? $_POST['codetype'] : "code128";

            // Separar los textos por comas
            $texts = array_map('trim', explode(',', $text));

            echo '<h2 class="text-center mt-4">Códigos de Barras Generados:</h2>';
            echo '<div id="codigoBarras" class="text-center">';
            
            foreach ($texts as $singleText) {
                $barcodeUrl = "barcode.php?text=" . urlencode($singleText) . "&size=" . $size . "&orientation=" . $orientation . "&codetype=" . $codetype;
                echo '<div class="mb-4">';
                echo '<img src="' . $barcodeUrl . '" alt="Código de Barras" class="img-fluid">';
                echo '<p>' . htmlspecialchars($singleText) . '</p>'; // Mostrar el texto debajo del código de barras
                echo '</div>';
            }

            echo '</div>';
            echo '<button onclick="window.print()" class="btn btn-success mt-3">Imprimir Códigos de Barras</button>';
        }
        ?>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
