<?php

    session_start();

    header("Content-Type: text/html; charset=UTF-8"); 

    require_once('../motormobil/motormobil.php');


require_once 'tokenService.php';

// Inicializar variables
$registro = null;
$error = null;

try {
    if(!isset($_GET['t'])) {
        throw new Exception('Token requerido');
    }

    // Validar token
    $payload = TokenService::validar($_GET['t']);

    $id = $payload['id'];
    $title_id = $payload['title_id'];
    $modulo = $payload['modulo'];

} catch(Exception $e) {
    $error = $e->getMessage();
}
?>
<!DOCTYPE html>
<?php
    require_once('../bussines/fix_characters.php');

    $Obj_fix_characters = new fix_characters();

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

    $Titulo_App='';

    $Descripcion_Login_App='';

    $Logo_APP='';

    $Logo_Fav_Icon='';

    $Habilitar_Formulario_Registro='';

    try{    



        $Odbj_conexion = new odbconexion;

        $conexion=$Odbj_conexion->openconection();

        $consulta="SELECT * FROM configuracion_app_temp5";

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

        $conexion->close();

        while($Field_config=$Array_adapter_config->fetch_assoc())

        {   

            $Titulo_App=$Field_config['Titulo_App'];

            $Descripcion_Login_App=$Field_config['Descripcion_Login_App'];

            $Logo_APP=$Field_config['Logo_APP'];

            $Logo_Fav_Icon=$Field_config['Logo_Fav_Icon'];

            $Habilitar_Formulario_Registro=$Field_config['Habilitar_Formulario_Registro'];

        }

    }catch (Exception $e) {

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

                        ";

    }



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

$time_log_assets=time();

/*inicializamos valores para la configuracion del template*/

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

$variables=[];

$_Color='';

$_Template='';

$_Theme_Template='';

$_Css='';

$_Js='';

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";

    }

}

/*terminan valores de configuracion pára el template*/

?>

<!DOCTYPE html>

<html lang="es" class="light <?php if($_Color!=''){ echo 'theme-user'; }else{ echo $_Theme_Template; }?>">

    <!-- BEGIN: Head -->

    <head>

        <meta charset="utf-8">

        <?php
            $_Protocol_ = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
            $_Host_ = $_SERVER['HTTP_HOST'];
            $_Path_ = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
            $_Current_Path_ = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
            // Subir un nivel
            $_Parent_Path_ = rtrim(dirname($_Current_Path_), '/');

            $_Base_Url_ = $_Protocol_ . $_Host_ . $_Parent_Path_;
            if($Logo_Fav_Icon==''){

                echo "

                <link href=\"../dist/images/logo.svg\" rel=\"shortcut icon\" type=\"image/x-icon\" style=\"color:black;\">
                <meta property=\"og:image\" content=\"".$_Base_Url_."/dist/images/logo.svg\" />
                ";

            }else{

                try {

                    if (file_exists("../".str_replace('../','',$Logo_Fav_Icon))){

                        $time_icon=time();

                        $time_icon=filectime("../".str_replace('../','',$Logo_Fav_Icon));

                        echo "

                        <link href=\"../".str_replace('../','',$Logo_Fav_Icon)."?dummy=".$time_icon."\" rel=\"shortcut icon\">
                        <meta property=\"og:image\" content=\"".$_Base_Url_."/".str_replace('../','',$Logo_Fav_Icon)."?dummy=".$time_icon."\" />
                        ";

                    }else{

                        echo "

                        <link href=\"../dist/images/logo.svg\" rel=\"shortcut icon\" type=\"image/x-icon\" style=\"color:black;\">
                        <meta property=\"og:image\" content=\"".$_Base_Url_."/dist/images/logo.svg\" />
                        ";  

                    }

                } catch (Exception $e) {

                    //echo "Ha ocurrido un error: " . $e->getMessage();

                }

            }

        ?>
        <meta property="og:title" content="<?=$Titulo_App?>" />
        <meta property="og:description" content="Desarrollado por SINAPSYS" />
        <meta property="og:url" content="<?=$_Base_Url_;?>" />
        <meta property="og:type" content="website" />

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

        <meta name="description" content="Desarrollado por SINAPSYS DEVOPS SA DE CV">

        <meta name="keywords" content="admin template, web app">

        <meta name="author" content="SINAPSYS">

        <title> <?=$Titulo_App;?> | Vista Compartida del Registro</title>

        <!-- BEGIN: CSS Assets-->

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

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

        <!-- END: CSS Assets-->

        <style>
            body { font-family: Arial, sans-serif; padding: 20px; background: #f9f9f9; }
            .card { background: #fff; padding: 20px; border-radius: 8px; max-width: 600px; margin: auto; box-shadow: 0 0 10px rgba(0,0,0,0.1);}
            h1 { font-size: 1.5em; margin-bottom: 10px; }
            p { margin: 5px 0; }
            .error { color: red; text-align: center; }
            .share-btn { margin-top: 15px; display: inline-block; padding: 10px 15px; background: #007bff; color: #fff; border-radius: 5px; cursor: pointer; text-decoration: none;}
        </style>
    </head>
<body>

<?php if($error): ?>
    <div class="error">
        <h2>Acceso no autorizado</h2>
        <p><?= htmlspecialchars($error) ?></p>
    </div>
<?php else: ?>
    <div class="card">
        <h1>Registro #<?= htmlspecialchars($id) ?></h1>
        <p><strong>Módulo:</strong> <?= htmlspecialchars($modulo) ?></p>
        <!-- Mostrar campos relevantes del registro -->
         <!-- Formulario oculto para enviar POST al iframe -->
        <form id="form-ver-datos" action="../home/<?= $modulo;?>/bussines/datos.php" method="post" target="seecampotest">
            <input type="hidden" name="action" value="editar">
            <input type="hidden" name="<?=$title_id;?>" value="<?= htmlspecialchars($id) ?>">
        </form>

        <!-- Iframe donde se carga el registro -->
        <iframe name="seecampotest" id="seecampotest" style="width:100%;min-height: 590px;"></iframe>
    </div>

     <script>
            // Autoenviar el formulario al cargar la página
            document.getElementById('form-ver-datos').submit();
        </script>
<?php endif; ?>

</body>
</html>
