<?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('Roles');
}
$fac = $user->facultad['facultad_id'] ?? -1;
if($user->admin){
    $fac=null;
}
echo $fac;
?>
<!DOCTYPE html>
<html lang="en">

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

<body>
    <?php
    include "import/html_header.php";
    html_header(
        "ROLES",
        "Gestión de Checador "
    );
    $user->access();
    $fs_roles = $db->orderBy('rol_titulo')->get('rol');
    ?>
    <main class="content marco">
        <?php if($user->acceso == 'w'){?>
            <div class="col-12 text-right">
                <button type="button" class="btn btn-outline-secondary" data-toggle="modal" data-target="#modal" data-tipo="1"><span class="ing-mas ing-fw"></span> Crear Rol</button>
            </div>
        <?php } ?>
        <!-- Filtro -->
        

        <!-- Tabla -->
        <div class="row mt-3">
            <div class="col-12 table-responsive">
                <table class="table table-sm table-striped table-white">
                    <thead class="thead-dark">
                        <th>Título</th>
                        <?php if($user->acceso == 'w'){?>
                        <th>Acciones</th>
                        <?php }?>
                    </thead>
                    <tbody>
                        <?php foreach($fs_roles as $rol){ ?>
                        <tr data-id="<?= $rol['rol_id'] ?>" id="<?= $rol['rol_id'] ?>">
                            <td class="text-center"><?= $rol['rol_titulo'] ?></td>
                            <?php if($user->acceso == 'w'){?>
                                <td class="text-center icono-acciones">
                                    <a href="#" data-toggle="modal" data-target="#modal" data-tipo="2" title="Editar"><span class="ing-editar ing-fw"></span></a>
                                </td>
                            <?php }?>
                        </tr>
                        <?php }?>
                    </tbody>
                </table>
            </div>
        </div>
    </main>
    <!-- Footer -->
    <?php
    include "import/html_footer.php";
    ?>
    <!-- Modal -->
    <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="col-12 modal-title text-center">
                        <span id="modalLabel">
                            Editar datos del Rol
                        </span>
                        <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </h4>
                </div>
                <div class="modal-body">
                    <form action="" method="post" id="formaModal" onsubmit="return valida_campos()">
                        <input type="hidden" name="id" id="id">
                        <input type="hidden" name="mfacultad" id="mfacultad">
                        <div class="form-box">
                            <div class="form-group row">
                                <label for="mtitulo" class="col-4 col-form-label">Titulo *</label>
                                <div class="col-8">
                                    <input type="text" id="mtitulo" name="mtitulo" class="form-control" maxlength="75">
                                    <div class="invalid-feedback" id="mtitulo-error">
                                        Campo obligatorio
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="offset-4 col-8">
                                <button type="submit" class="btn btn-outline-primary" id="submitBtn" data-tipo="1">
                                    <span class="ing-aceptar ing-fw"></span> Guardar
                                </button>
                                <button type="reset" class="btn btn-outline-danger" data-dismiss="modal">
                                    <span class="ing-cancelar ing-fw"></span> Cancelar
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>
    <?php
        require_once 'js/messages.php';
    ?>
    <script>
        $('#modal').on('show.bs.modal', function (event){
            var button = $(event.relatedTarget);
            var tipo = button.data('tipo');
            var modal = $(this);
            $('#mtitulo').removeClass("is-invalid");
            if(tipo==1){//crear
                $('#modalLabel').html("Crear Rol");
                $('#mtitulo').val("");
                $('#submitBtn').data('tipo', 1);
                $('#id').val("");
                $('#mfacultad').val("");
            }
            else{//editar
                var id= $(event.relatedTarget).parents('tr').data('id');
                var fac=$(event.relatedTarget).parents('tr').data('facultad');
                $('#modalLabel').html("Editar Rol");
                $('#id').val(id);
                $('#mfacultad').val(fac);
                $('#submitBtn').data('tipo', 2);
                $("#mtitulo").removeClass("is-invalid");
                $.ajax({
                    url:"action/action_roles_select.php",
                    type:"post",
                    dataType:"json",
                    data: {rol: id},
                    success:function(result){
                        console.log(result);
                        $('#mtitulo').val(result['rol_titulo']);
                    },
                    error: function(){
                        console.log("Error");
                    }
                });
            }
        });

        function valida_campos(){
            var error=false;
            if($('#mtitulo').val()==""){
                $('#mtitulo').addClass("is-invalid");
                $('#mtitulo-error').html("Campo obligatorio");
                error=true;
            }else{
                $('#mtitulo').removeClass("is-invalid");
            }
            if($('#mtitulo').val()[0]==" "){
                $('#mtitulo').addClass("is-invalid");
                $('#mtitulo-error').html("No puede tener espacios al inicio");
                error=true;
            }
            if(error==true){
                return false;
            }else{
                var btn = $('#submitBtn');
                if(btn.data('tipo')==2)//update
                    $("#formaModal").prop("action", "./action/action_roles_update.php");
                else//insert
                    $('#formaModal').prop('action', './action/action_roles_insert.php');
            }
        }
    </script>
</body>

</html>