<?php
session_start();
$usuarioSession = $_SESSION['USUARIO'];
$grupo = $_SESSION['GRUPO'];
$contacto = $_SESSION['CONTACTO'];
$awa_session_id = $_SESSION['AWA_SESION_ID'];
include('includes/conex.php');
include('includes/eat.php');
include('includes/historial.php');
include('lg/config_vo.php');
if(!isset($usuarioSession) or $awa_session_id != $awa_id)
{
    eatCookies();
    header('Location:index.php');
}
if(isset($_COOKIE['usuario']) AND $_COOKIE['usuario'] != $staticUsu )
{
    eatCookies();
    header('Loction:index.php');
}
if(isset($_COOKIE['idioma']))
{
    include('lg/LG_' . $_COOKIE['idioma'] . '.php');
}
else
{
    include('lg/LG_SP.php');
}
$ctrl = htmlspecialchars(mysqli_real_escape_string($con,$_GET['ctrl']));
if($ctrl == 1)
{
    $_SESSION['CTRL_PERMISOS'] = 1; 
}
else
{
    $_SESSION['CTRL_PERMISOS'] = 2;
}
if($_SESSION['CTRL_PERMISOS'] == 1)
{
    $sqlper = "SELECT U.USUARIO AS DESCRIPCION_PERMISO, U.GRUPO, U.CONTACTO AS CODIGO_PERMISO FROM USUARIOS U WHERE U.ACTIVO = 1 ORDER BY U.USUARIO ASC";
    $nameCheck = 'nameA[]';
    $title_pag = '<i class="fas fa-user mr-2"></i>'.$permisos_usuarios_lg;
}
elseif($_SESSION['CTRL_PERMISOS'] == 2)
{
    $sqlper = "SELECT G.CODIGO AS CODIGO_PERMISO, G.DESCRIPCION AS DESCRIPCION_PERMISO FROM USUARIOS_GRUPOS G WHERE G.ACTIVO = 1 ORDER BY G.DESCRIPCION ASC";
    $nameCheck = 'nameB[]';
    $title_pag = '<i class="fas fa-users mr-2"></i>'.$permisos_grupos_lg;
}
?>
<!doctype html>
<html lang="es">
<head>
  <?php include('includes/head.php');?>
  <title><?php echo $favicon_title_lg?></title>
  <style>
        html, body
        {
            width: 100%;
            height: 100%;
        }
        .full-height 
        {
            height: 950px;
        }
        .c-pointer
        {
            cursor: pointer;
        }
        .h-x200
        {
            height: 200px;
        }
        @media (max-width: 770px) 
        {
            .full-height 
            {
                height: 200%;
            }
        }
  </style>
</head>

<body class='bg-light'>

  <!-- //! Iincludes Top Bar & Menu & Datos Usuario 
  -->
  <?php
    $page = "ajustes";
    include("includes/datosUsuario.php"); 
    include('includes/menu-dashboard.php');
    include("includes/functions.php");
    include('includes/nav.php');
  ?>

  <header class='d-none'>
    <h1>UVEO</h1>
  </header>

    <div class="col-12 mt-3"></div>
    <section class="container-fluid">
        <div class="row">


            <?php include('includes/nav-ajustes.php');?>




            <div id="main" class="col-md-9 ml-sm-auto col-lg-10 bg-white border-left">
                <div class="col-12 mt-4"></div>
                <h4 class='col-12 text-dark mb-sm-4 border-bottom pb-2'><?php echo $title_pag;?></h4>
                <table class="table table-sm table-hover table-striped" id="permisos">
                    <thead class="bg-secondary text-light">
                        <tr>
                            <th scope="col"><?php echo $usuario_lg?></th>
                            <th class="text-center" scope="col"><?php echo $accion_lg?></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                        $nrModal = 0; 
                        $res = $con->query($sqlper);
                        while($fila = $res->fetch_array()) {
                        $nrModal++;
                    ?>
                        <tr>
                            <td><?php echo utf8_encode($fila['DESCRIPCION_PERMISO'])?></td>
                            <td class="text-center">
                                <a data-toggle="modal" data-target=".permisos<?php echo $nrModal?>">
                                    <i class="fas fa-key fa-lg text-secondary c-pointer"></i>
                                </a>
                            </td>
                        </tr>
                        <div class="modal fade permisos<?php echo $nrModal?>" tabindex="-1" role="dialog" aria-labelledby="myExtraLargeModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="exampleModalCenterTitle"><?php echo $tipos_permisos_lg . ": " . utf8_encode($fila['DESCRIPCION_PERMISO'])?></h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                    <form class="needs-validation" method="post" novalidate>
                                        
                                            <?php
                                                $nr = 0;
                                                $sqlper2 = "SELECT L.CODIGO, L.DESCRIPCION_$lg AS DESCRIPCION_LG, P.USUARIO
                                                FROM  VEHICULOS_PERMISOS P
                                                RIGHT JOIN VEHICULOS_LITERALES L ON P.ACCION = L.CODIGO AND P.USUARIO = " . $fila['CODIGO_PERMISO'] . "
                                                WHERE L.TIPO = 'TIP_ACC'";
                                                $resper2 = $con->query($sqlper2);
                                                while($filaper2 = $resper2->fetch_array())
                                                {
                                                    $nr++;
                                            ?>
                                                <div class="custom-control custom-switch mb-3">
                                                    <input type="checkbox" class="custom-control-input" id="customSwitch<?php echo $nrModal.$nr?>" name="<?php echo $nameCheck?>" value="<?php echo $filaper2['CODIGO']?>"
                                                        <?php
                                                            if($filaper2['USUARIO'] != NULL)
                                                            {
                                                                echo "checked";
                                                            }  
                                                        ?>
                                                    >
                                                    <label class="custom-control-label" for="customSwitch<?php echo $nrModal.$nr?>"><?php echo utf8_encode($filaper2['DESCRIPCION_LG']) ?></label>
                                                </div>
                                            <?php } ?>    
                                        <input type="hidden" name="usuario" value="<?php echo $fila['CODIGO_PERMISO']?>">
                                        </div>
                                        <div class="modal-footer">
                                            <button class="btn btn-primary btn-block" formaction="php_o/ajustes-permisos-o.php" type="submit"><?php echo $actualizar_lg?></button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    <?php } ?>
                    </tbody>
                </table>




            </div>


        </div>

    </section>


  <div class="clearfix"></div>

  <!-- //! Footer & Script Links 
  -->
  <?php 
    include('includes/footer.php');
    include('includes/footer-script.php');
  ?>
    <script>
        $(function () {
            <?php 
                if($lg == 'UK')
                {
                ?>
                $('#permisos').dataTable();
                <?php } else { ?>
                    $("#permisos").dataTable({
                        "language": {
                        "lengthMenu": "Mostrar _MENU_",
                        "zeroRecords": "No se encontraron resultados",
                        "info": "Registros del _START_ al _END_",
                        "infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
                        "infoFiltered": "(filtrado de un total de _MAX_ registros)",
                        "sSearch": "Buscar:",
                        "searchPlaceholder": "Buscar por los campos",
                        "oPaginate": {
                        "sFirst": "Primero",
                        "sLast": "Último",
                        "sNext": "Siguiente",
                        "sPrevious": "Anterior"
                        },
                        "sProcessing": "Procesando...",
                        }
                    })
            <?php } ?>
        })
    </script>
  </body>
</html>
