<?php

    //Declaramos la variable time, para que al modificar un archivo, este no se guarde en cache y se vizualicen los cambios

    session_start();

    $time_log_assets=time();

    

    require_once('../odbconexion/config.php');

    $Rol_Usuario_Logued_App=$_SESSION['Rol_appquadrata'];

    $Registrar_En_Modulo_App="";

    $Leer_En_Modulo_App="";

    $Editar_En_Modulo_App="";

    $Exportar_En_Modulo_App="";

    $Eliminar_En_Modulo_App="";

    $Id_Modulo_Fk_En_Modulo_App=0;

    try{    



        $Odbj_conexion = new odbconexion;

        $conexion=$Odbj_conexion->openconection();

        $consulta="SELECT p.Id_Permiso_App,p.Id_Fk_Rol,p.Id_Modulo_Fk,p.Registrar,p.Leer,p.Editar,p.Eliminar,p.Exportar,m.Id_Modulo_App,m.Titulo_Modulo,m.Descripcion_Modulo,m.Tipo_Menu,m.Href_Modulo,m.Tipo_Enlace,m.Icon_Fa_Modulo FROM permiso_app_appquadrata p JOIN modulo_app_appquadrata m ON Id_Modulo_Fk=Id_Modulo_App WHERE Id_Fk_Rol='$Rol_Usuario_Logued_App' AND Leer='Si' ORDER BY Id_Permiso_App DESC ";

        $Array_adapter_permiso_modulo=$conexion->query($consulta);

        $conexion->close();

        while($Field_Permiso_Modulo=$Array_adapter_permiso_modulo->fetch_assoc())

        {

            $Id_Modulo_Fk_En_Modulo_App=$Field_Permiso_Modulo['Id_Modulo_Fk'];

            $Registrar_En_Modulo_App=$Field_Permiso_Modulo['Registrar'];

            $Leer_En_Modulo_App=$Field_Permiso_Modulo['Leer'];

            $Editar_En_Modulo_App=$Field_Permiso_Modulo['Editar'];

            $Exportar_En_Modulo_App=$Field_Permiso_Modulo['Exportar'];

            $Eliminar_En_Modulo_App=$Field_Permiso_Modulo['Eliminar'];

        }

    }catch (Exception $e) {

        echo 'Excepcion capturada: ',  $e->getMessage(), "";

    }





/*inicializamos valores para la configuracion del template*/

$setting_template='../settings_template.txt';

$variables=[];

$_Color='';

$_Template='';

$_Theme_Template='';

$_Css='';

$_Js='';

$_Css_Version='';

$_Js_Version='';



if (file_exists($setting_template)) {

    $lines = file($setting_template, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    foreach ($lines as $line) {

        list($nombre_variable, $valor) = explode('=', $line);

        $variables[$nombre_variable] = $valor;

    }

    /*recogemos los valores de las variables*/

    if (isset($variables['Color'])) {

        $_Color = $variables['Color'];



        //echo "El color es: $_Color";

    }

    if (isset($variables['Template'])) {

        $_Template = $variables['Template'];

        //echo "El Template es: $_template";

    }

    if (isset($variables['Theme_Template'])) {

        $_Theme_Template = $variables['Theme_Template'];

        //echo "El Theme_Template es: $_Theme_Template";

    }

    if (isset($variables['css'])) {

        $_Css = $variables['css'];

        //echo "El css es: $_Css";

    }

    if (isset($variables['js'])) {

        $_Js = $variables['js'];

        //echo "El js es: $_Js";

    }

    if (isset($variables['css_version'])) {

        $_Css_Version = $variables['css_version'];

        //echo "El css es: $_Css";

    }

    if (isset($variables['js_version'])) {

        $_Js_Version = $variables['js_version'];

        //echo "El js es: $_Js";

    }

}

/*terminan valores de configuracion para el template*/

?>

<!DOCTYPE html>

<html style="background-color: rgb(241, 245, 249);"  class="<?php if($_Color!=''){ echo 'theme-user'; }else{ echo $_Theme_Template; }?>">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="../dist/css/<?=$_Css;?>?time=<?=$_Css_Version?>" />

    <title>Lector QR</title>

    <style>

       

        #video {

            width: 100%;

            max-width: 100%;

            height: auto;

            border: 1px solid #ccc;

            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

        }

        #output {

            margin-top: 20px;

            font-size: 18px;

            color: #333;

        }

            /* Estilos básicos para el botón */

        #backButton,#goToLink {

            display: inline-block;

            padding: 10px 20px;

            /*font-size: 16px;

            color: #fff;

            background-color: #007BFF;*/

            border: none;

            border-radius: 5px;

            cursor: pointer;

        }



        #backButton:hover {

            /*background-color: #0056b3;*/

        }

    </style>

</head>

<body>

    

    <video id="video" autoplay></video>

    <CENTER>

    <div id="output"></div>

    <br>

    <button id="goToLink" style="display: none;" onclick="goToQRLink()">IR</button>

    <br> <br> 

    <button id="backButton" onclick="goBack()" class="btn-primary">Regresar</button>

    </CENTER>

    <script src="../dist/js/jsQR.js"></script>

    <script type="text/javascript">

         function goBack() {

            window.history.back();

        }



        async function startVideo() {

    const video = document.getElementById('video');

    const output = document.getElementById('output');

    

    try {

        const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } });

        video.srcObject = stream;



        video.onloadedmetadata = () => {

            video.play();

            scanQRCode();

        };

    } catch (err) {

        console.error("Error accessing camera: ", err);

        output.textContent = 'Error accessing camera';

    }

}



function scanQRCode() {

    const video = document.getElementById('video');

    const canvas = document.createElement('canvas');

    const context = canvas.getContext('2d');

    const goToLinkButton = document.getElementById('goToLink');

    const output = document.getElementById('output');



   



    function drawFrame() {

        canvas.width = video.videoWidth;

        canvas.height = video.videoHeight;

        context.drawImage(video, 0, 0, canvas.width, canvas.height);



        const imageData = context.getImageData(0, 0, canvas.width, canvas.height);

        const code = jsQR(imageData.data, imageData.width, imageData.height);



        if (code) {

            

            // output.textContent = `QR Code detected: ${code.data}`;

            // output.innerHTML = `<a href='${code.data}'><button id='backButton'>IR</button></a>`;

            // window.location.href = `${code.data}`;

            goToLinkButton.style.display = 'inline-block';

            window.location.href = `${code.data}`;

            exit();

            goToLinkButton.dataset.url = `${code.data}`;

        } else {

            output.textContent = 'LECTOR QR';

        }



        requestAnimationFrame(drawFrame);

    }



    drawFrame();

}



 function goToQRLink() {

    const url = document.getElementById('goToLink').dataset.url;

    if (url) {

        window.location.href = url;

        }

    }



startVideo();



    </script>

</body>

</html>