<?php
$r= '../';
$titlepage='PERMISO DE PERFILES';
require_once($r."inc/funciones.php");
validNotSession($r);
$mysqli = connectDB($r);
$spe = $_SESSION['perfil'];
$idusuario = $_SESSION['userid'];
//roleaccess(1);
?>
<!DOCTYPE html>
<html lang="es">
  <?php include $r."inc/head.php"; ?>
  <style>
      .tr-btn{
          color:#4898ff!important;
      }
      .tr-act{
          color:#ff4848!important;
      }
      .tr-pgn{
          font-weight:bold;
      }
  </style>
<body class="fixed-nav sticky-footer sidenav-toggled bg-dark" id="page-top">
  <?php include $r."inc/nav.php"; ?>
 
  <div class="content-wrapper">
    <div class="container-fluid">
          <!-- Example DataTables Card-->
          <div class="card mb-3">
            <div class="h3 card-header">
              <i class="fa fa-table"></i> <?= $titlepage; ?>
               <!--<a class="btn text-primary float-right p-0 ml-3" title="Nuevo Perfil" onclick="nuevo()"><i class="fa fa-plus-circle fa-2x"></i></a>-->
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table class="table table-bordered table-striped" id="dataTable" width="100%" cellspacing="0">
                  <thead>
                    <tr>
                      <th>Ruta</th>
                      <th>Tipo</th>
                      <? $qry1 = $mysqli->query("SELECT * FROM z_perfiles WHERE administrables=1");
                        while($row1 = $qry1->fetch_assoc()){ ?>
                        <th title="<?= $row1['nombre']; ?>"><?= $row1['nombre']; ?></th>
                      <? } ?> 
                    </tr>
                  </thead>
                  <tfoot>
                    <tr>
                      <th>Ruta</th>
                      <th>Tipo</th>
                      <? $arrayperfiles = array();
                      $qry1 = $mysqli->query("SELECT * FROM z_perfiles WHERE administrables=1");
                        while($row1 = $qry1->fetch_assoc()){ array_push($arrayperfiles, $row1['sigla']); ?>
                        <th title="<?= $row1['nombre']; ?>"><?= $row1['nombre']; ?></th>
                      <? } ?> 
                    </tr>
                  </tfoot>
                  <tbody>
                      <?
                      $qry1 = $mysqli->query("SELECT * FROM z_roles  ORDER BY id");
                        while($row1 = $qry1->fetch_assoc()){ ?>
                        <? if($row1['tipo'] == 'nav'){ ?>
                        <tr>
                            <td class="text-center bg-secondary text-white" colspan="<?= count($arrayperfiles)+2; ?>"><?= $row1['pagina']; ?></td>
                        </tr>
                            <? }else{ ?>
                        <tr class="tr-<?= $row1['tipo']; ?>">
                                <td title="<?= $row1['id']; ?>"><?= $row1['pagina']; ?></td>
                                <td><?= $row1['tipo']; ?></td>
                                <? foreach($arrayperfiles as $value){ ?>
                                    <td><?if($row1[$value]){echo '<i class="text-success fa fa-check  fa-lg mr-1" style="cursor:pointer" onclick="permiso(this, \'' . $value . '\',' . $row1['id'] . ')"></i>';}else{ echo '<i class="text-secondary fa fa-times fa-lg mr-1" style="cursor:pointer" onclick="permiso(this, \'' . $value . '\',' . $row1['id'] . ')"></i>';} ?></td>
                                <? } ?>
                            <? } ?>
                        </tr>
                      <? } ?> 
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
    </div>
  
    <!-- Modal Nuevo-->
    <!--<div class="modal fade" id="modal-perfil" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="modal-title-perfil"></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 id="form-perfil">
              <div class="form-group">
                <label for="exampleInputEmail1">Nombre del Perfil</label>
                <input type="text" class="form-control" id="perfil" aria-describedby="emailHelp" placeholder="Nombre del Perfil">
              </div>
              <div class="form-group">
                <label for="exampleInputPassword1">Sigla</label>
                <input type="password" class="form-control" id="sigla Unica" placeholder="Sigla unica">
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">CERRAR</button>
            <button type="submit" form="form-perfil" class="btn btn-primary">GUARDAR</button>
          </div>
        </div>
      </div>
    </div>-->
    
    <!-- Modal Modificar Permisos-->
    <div class="modal fade" id="modificar" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="modal-title-modificar">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">CERRAR</button>
            <button type="button" class="btn btn-primary">GUARDAR</button>
          </div>
        </div>
      </div>
    </div>
  <?php include $r."inc/foot.php"; ?>
  <script>
    $('#dataTable').DataTable({
        
      "paging": false,
      "ordering": false,
      "searching": true,
      // Otras configuraciones...
    });
    function nuevo(){
        $('#modal-perfil').modal('show');
    }
    function editar(){
        $('#modal-perfil').modal('show');
    }
    function permiso(element, perfil, id){
        $.post( "acciones.php", { permiso: perfil,  id: id  })
          .done(function( response ) {
            if (response.success) {
                
                $(element).attr('class', response.class);
                swal(response.message);
            } else {
                // Mostrar un mensaje de error en algún lugar del formulario
                swal(response.message);
            }
        });
        
    }
  </script>
</body>
</html>   