<?php
require_once 'class/c_login.php';
require_once 'include/bd_pdo.php';

$user = Login::get_user();

$user->access();
if($user->acceso == null){
    header('Location: main.php?error=1');
}else{
    $user->print_to_log('Permisos');
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Permisos</title>
    <?php
    include 'import/html_css_files.php';
    ?>
</head>

<body>
    <?php
    include "import/html_header.php";
    html_header(
        "PERMISOS",
        "Gestión de Checador "
    );
    $user->access();

    $fs_roles=query("SELECT * FROM rol ORDER BY rol_titulo", null, false);
    $fs_paginas=query("SELECT * FROM pagina p, grupo g WHERE p.grupo_id = g.grupo_id ORDER BY g.grupo_nombre, p.pagina_ruta", null, false);
    $fs_permisos = query("SELECT * FROM permiso ORDER BY pagina_id", null, false);
    ?>
    <main class="content marco">
        <div class="tab-content" id="rolesTabContent">
            <div class="row">
                <div class="col-12 table-responsive">
                    <form id="permisos" action="./action/action_permisos_update.php" method="post">
                        <table class="table table-sm table-striped table-white">
                            <thead class="thead-dark">
                                <tr>
                                    <th colspan="2" style="vertical-align:middle">Checador</th>
                                    <?php foreach($fs_roles as $rol) {?>
                                    <th class="pt-3" style="width:80px;">
                                        <div class="d-lg-block">
                                            <span class="rotate-text"><?= $rol['rol_titulo'] ?></span>
                                        </div>
                                    </th>
                                    <?php }?>
                                </tr>
                                <tr>
                                    <th>Módulo</th>
                                    <th>Página</th>
                                    <?php foreach($fs_roles as $rol) {?>
                                    <th></th>
                                    <?php }?>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $check=0; foreach($fs_paginas as $pagina) {?>
                                    <tr class="font-weight-light">
                                        <th class="font-weight-light"><span class="<?= $pagina['grupo_icon'] ?> ing-fw"></span> <?= $pagina['grupo_nombre'] ?></th>
                                        <th class="font-weight-light"><?= $pagina['pagina_ruta'] ?></th>
                                        <?php foreach($fs_roles as $rol) {?>
                                        <td class="text-center">
                                            <div class="custom-control custom-checkbox checkbox-ver">
                                                <input type="checkbox" class="custom-control-input" name="lectura[]" id="costumCheck_<?= $pagina['pagina_id'] ?>_<?= $rol['rol_id'] ?>" value="<?= $pagina['pagina_id']?>_<?= $rol['rol_id'] ?>_r">
                                                <label class="custom-control-label" for="costumCheck_<?= $pagina['pagina_id'] ?>_<?= $rol['rol_id'] ?>" title="Ver"><span class="ing-ojo ing-fw"></span></label>
                                            </div>
                                            <div class="custom-control custom-checkbox checkbox-editar">
                                                <input type="checkbox" class="custom-control-input" name="editar[]" id="costumCheck2_<?= $pagina['pagina_id'] ?>_<?= $rol['rol_id'] ?>" value="<?= $pagina['pagina_id']?>_<?= $rol['rol_id'] ?>_w">
                                                <label class="custom-control-label" for="costumCheck2_<?= $pagina['pagina_id'] ?>_<?= $rol['rol_id'] ?>" title="Editar"><span class="ing-editar ing-fw"></span></label>
                                            </div>  
                                        </td>
                                        <?php $check++;
                                        }?> 
                                    </tr>    
                                <?php }?>
                            </tbody>
                        </table>
                        <div class="form-group row">
                            <div class="col-12 text-center">
                                <button type="submit" class="btn btn-outline-primary" id="submitBtn" data-tipo="1"> <span class="ing-aceptar ing-fw"></span> Guardar permisos</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
    <!-- Footer -->
    <?php
    include "import/html_footer.php";
    ?>
    <!-- Modal -->
    
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>
    <script src="js/datalist.js"></script>
    <?php
        require_once 'js/messages.php';
    ?>
    <script>
        $(document).ready(function(){
            <?php
                foreach($fs_permisos as $permiso){
                    $posicion="costumCheck_".$permiso['pagina_id']."_".$permiso['rol_id'];
                    $posicion2="costumCheck2_".$permiso['pagina_id']."_".$permiso['rol_id'];
                    if($permiso['permiso_tipo']=="w"){ ?>
                        $('#<?= $posicion ?>').prop("checked", true);
                        $('#<?= $posicion2 ?>').prop("checked", true);
                    <?php }else{ ?>
                        $('#<?= $posicion ?>').prop("checked", true);
                    <?php }
                }
            ?>
            $(".checkbox-ver").change(function(){
                var check_edita = $(this).parents("td").find(".checkbox-editar input[type=checkbox]");
                if(!$(this).find("input[type=checkbox]").prop("disabled")){
                    if(!$(this).find("input[type=checkbox]").prop("checked")){
                        check_edita.prop("checked",false).prop("disabled", true);
                    }else
                        check_edita.prop("disabled", false);
                }
            }).change();
        });
    </script>
</body>

</html>