<?php
declare(strict_types=0);
// session_start();
foreach($_GET  as $key => $value){${$key}=trim($value);} 
foreach($_POST as $key => $value){${$key}=trim($value);} 
include_once('lib_main.php');

if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}
$user_codigo=$_SESSION['user_codigo'];
$user0=data_maes('user',$user_codigo);
$empr0=data_maes('empr',$cfg_empr);
// if(empty($user_codigo)){include('user_login0.php');return;}

$xtitle='Reg.EAN-13';


$ztabla='item';
include('ele_datalist.php');



$zid='item';
$zlabel='ITEM';
$ztabla='item';
$zvalue=$item;
$zsize=2;
include('ele_input.php');

$zobj=1;     
include('ele_break.php');

$zid='ean13';
$zlabel='EAN-13';
$zmin='1000000000000';
$zmin=0;
$zmax='9999999999999';
$zclass='numeric-int';
$zsize=2.5;
include('ele_input.php');

$zobj=1;     
include('ele_break.php');




echo ' <video id="scanner" style="width: 100%; max-width: 600px;"></video> ';
echo '    <p><strong>Código detectado:</strong> <span id="codigo"></span></p>';

// echo '    <form method="POST">';
echo '        <input type="hidden" name="barcode" id="barcode">';
echo '        <button type="submit">Enviar</button>';
// echo '    </form>';




?>

<script>document.title="<?php echo $xtitle;?>"</script>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            Quagga.init({
                inputStream: {
                    name: "Live",
                    type: "LiveStream",
                    target: document.querySelector("scanner"),
                    constraints: {
                        width: 480,
                        height: 320,
                        facingMode: "environment" // Usa la cámara trasera
                    }
                },
                decoder: {
                    readers: ["ean_reader", "code_128_reader"] // Soporta múltiples tipos de códigos
                }
            }, function(err) {
                if (err) {
                    console.error("Error al iniciar Quagga:", err);
                    return;
                }
                Quagga.start();
            });

            Quagga.onDetected(function(result) {
                let codigo = result.codeResult.code;
                document.getElementById("codigo").innerText = codigo;
                document.getElementById("barcode").value = codigo;
//                 document.getElementById("ean13").value = codigo;
//                 Quagga.stop(); // Detiene la cámara tras escanear
            });
        });
    </script>


