<?php
// 1. Iniciar compresión de buffer para enviar paquetes pequeños al switch Huawei
if (!ob_start("ob_gzhandler")) ob_start();

if (isset($_GET['token']) && $_GET['token'] !== '') {
    session_id($_GET['token']);
}

session_start();

// 2. Cookie compatible con iFrames (SameSite=None + Partitioned para navegadores modernos)
header('Set-Cookie: ' . session_name() . '=' . session_id() . '; path=/; SameSite=None; Secure; HttpOnly; Partitioned');
header("Connection: close"); // Evita que los sockets queden colgados en el switch

require_once('config/config.php');
include('functions/PHPFunctions.php');
include('functions/funcionesDeIntervenciones.php');

$conexionDB = new mysqli($host, $user, $pass, $bd);
if ($conexionDB->connect_errno) {
    echo "Falló la conexión: ".$conexionDB->connect_error;
    exit();
}

mysqli_set_charset($conexionDB,"utf8");

sessionDestroyOrRedirect($_SESSION['exp_datos']['pers_id'], "https://ucampus.uchile.cl");

/**************************************Redirección del sitio según cuenta*****************************************************/
include('html/revisionUsuario.php');

/**************************************Revisión de que el usuario no cambie los links*******************************************/
if ($_SESSION['exp_unidad_IT'] <> "00I00I") { // Se revisa que no sea de informática
    if ($_SESSION['exp_unidad_IT'] <> $_GET['perfilDepartamento']) { // Se comprueba que no haya cambiado su departamento
        redirectSiteJS("https://ucampus.uchile.cl/m/facso_expedientes/");
    }
}
?>
<!DOCTYPE html>
<html lang="es-CL">
    <head>
        <?php require('html/header.php'); ?>
        <script>            
            $(document).ready(function(){
                $('#app').attr('class','sel');
                $('#perfilDepartamento').change(function() {
                    window.location.href='?rut='+$_GET('rut')+'&perfilDepartamento='+$('#perfilDepartamento').val();
                });
                
                <?php include('functions/funciones_JavaScript.php'); ?>
                
                $("#gifLoading").empty();
                
                // Popover cargado en cliente sin bloquear renderizado
                $('#infoSys').hover(function() { 
                    var novedades = "<table style='font-size:13px;'><thead><tr></tr></thead><tbody>";
                    <?php
                    $queryNovedades = "SELECT DAT_NOVEDADES.fecha_novedad, DAT_NOVEDADES.numero_version, DAT_NOVEDADES.detalle_novedad FROM DAT_NOVEDADES WHERE DAT_NOVEDADES.estado='1' AND DAT_NOVEDADES.fecha_novedad='2018-08-21';";
                    if ($res = $conexionDB->query($queryNovedades)) {
                        while ($EresultNovedades = $res->fetch_array()) {
                            echo 'novedades += "<tr><td>-</td><td>' . addslashes($EresultNovedades['detalle_novedad']) . '</td></tr>";';
                        }
                        $res->free();
                    }
                    ?>
                    novedades += "</tbody></table></br><hr><p>Puedes ver/descargar el instructivo <a target='_blank' href='issues/Instructivo_Expediente_Extudiantil.pdf'>aquí</a><p>";
                    $('#infoSys').webuiPopover({content:novedades, placement:'bottom-left', animation:'pop', width:600});
                });
            });
        </script>
    </head>
    <body>
        <div id="body" class="facso_bia">
            <?php 
            if ($_SESSION['exp_unidad_IT'] == "00I00I") {
                include('html/seleccionUnidadParaIntervencion.php');
            }
            include('html/pestanas.php');
            if (isset($_GET['idNotFound']) && $_GET['idNotFound'] == 1 && !(isset($_POST['q']))){
                echo "<div class='notificacion_info'>La Intervención solicitada no existe.</div>";
            }
            if (!(isset($_POST['q']))){
                echo "<br><br><br>";
            }
            ?>
            
            <form class="buscar" method="POST" action="">
                <input type="hidden" name="tab" value="">
                <input type="hidden" name="s" value="0">
                <input type="text" class="autofocus" name="q" placeholder="Alumno" title="Buscar un alumno por nombre, apellido o rut">
                <input type="submit" class="buscar" value="Buscar"> 
            </form>
            <br><br>
            <form class="buscar" method="POST" action="busqueda_Id_detalle.php?perfilDepartamento=<?php echo $_GET['perfilDepartamento']; ?>">
                <input type="hidden" name="tab" value="">
                <input type="hidden" name="s" value="0">
                <input type="text" name="q" placeholder="Id Expediente" title="Buscar una intervención por Id" onkeypress='validate(event)'>
                <input type="submit" class="buscar" value="Buscar">
            </form>
            <br>
            <div id='gifLoading'>
                <img border='0' src='images/loadingCircle.gif' style='position:fixed; margin-top:10%; margin-left:40%;' />
            </div>
            <?php
            if ((isset($_POST['q']) && $_POST['q'] !== "") || (isset($_GET['q']) && $_GET['q'] !== "")) {
                include('resultado_personas.php');
            }
            ?>
        </div>
        <br><br><br><br><br>
        <?php require('html/footer.php'); ?>
    </body>
</html>