<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
    $q = mysqli_real_escape_string($con,(strip_tags(($_REQUEST['q']), ENT_QUOTES)));

    include '../include/pagination.php'; //include pagination file
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 20; //how much records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;
    $count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM usuario where  empresa_emp_id='$empresa' and eliminado<>'si' and (usu_cc like '%$q%' OR usu_username like '%$q%' OR usu_nombre like '%$q%')");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './P_pos.php';
    $sql="SELECT * FROM usuario where empresa_emp_id='$empresa' and eliminado<>'si' and (usu_cc like '%$q%' OR usu_username like '%$q%' OR usu_nombre like '%$q%') order by usu_nombre asc";
    $query = mysqli_query($con, $sql);
    if ($numrows>0){
?>
<div class="table-responsive">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th width="5%"></th>
                <th>NOMBRE</th>
                <th>USUARIO</th>
                <th>SUCURSAL</th>
                <th>CARGO</th>
                <th>ESTADO</th>
                <th width="14%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
    <?php
        while ($reg=mysqli_fetch_array($query)){
          $regi=mysqli_query($con,"SELECT * from rol where rol_id='".$reg['rol_rol_id']."'") or die(mysqli_error($con));
            $rol=mysqli_fetch_array($regi);
            $rol['rol_id'];
            $rol['rol_nombre'];
          $regi=mysqli_query($con,"SELECT * from sucursal where suc_id='".$reg['sucursal']."'") or die(mysqli_error($con));
            $suc=mysqli_fetch_array($regi);
            $suc['suc_id'];
            $suc['suc_nombre'];
          $regi=mysqli_query($con,"SELECT * from bodegas where bod_id='".$reg['bodega']."'") or die(mysqli_error($con));
            $bod=mysqli_fetch_array($regi);
            $bod['bod_id'];
            $bod['bod_nombre'];

            $usu_id=$reg['usu_id'];
    ?>
        <input type="hidden" value="<?php echo $reg['usu_cc'];?>" id="cc<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $reg['usu_username'];?>" id="username<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo mb_strtoupper($reg['usu_nombre']);?>" id="nombre<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $reg['usu_correo'];?>" id="correo<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $reg['usu_tel'];?>" id="tel<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $reg['usu_dir'];?>" id="dir<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $reg['usu_estado'];?>" id="estado<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $rol['rol_id'];?>" id="rol_id<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $suc['suc_id'];?>" id="suc_id<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $bod['bod_id'];?>" id="bod_id<?php echo $reg['usu_id'];?>">
        <input type="hidden" value="<?php echo $reg['tarifa'];?>" id="tarifa<?php echo $reg['usu_id'];?>">
        <tr>
            <td>  
                <?php
                    $files = glob("../img/usuarios/$usu_id.jpg");
                    if(count($files) > 0) 
                    { 
                        foreach($files as $file) 
                        { 
                    ?>
                   
                        <?php $nu=rand(1,1000); $nu; ?>
                        <a href="img/usuarios\<?php echo $usu_id.'.jpg?' .$nu .''; ?>" class="ui-lightbox">
                         <center><div class="contenedor"> <img  class="categorias" src="img/usuarios\<?php echo $usu_id.'.jpg?' .$nu .''; ?>" alt=""/></div></center>
                        </a>
                   
                    <?php } } else { ?>
                    
                        <a href="img/usuarios/sin_imagen.jpg" class="ui-lightbox">
                       <center>     <div class="contenedor"><img class="categorias" src="img/usuarios/sin_imagen.jpg" alt=""/></div></center>
                        </a>
                   
                <?php } ?>
            </td>
            <td><?php echo $reg['usu_username']; ?></td>
            <td><?php echo $reg['usu_nombre']; ?></td>
            <td><?php echo $suc['suc_nombre']; ?></td>
            <td><?php echo $rol['rol_nombre']; ?></td>
            <td><?php
                if($reg['usu_estado']=="Activo")
                    {
                    echo $estados='<a href="#" onclick="estado('.$reg['usu_id'].')"> <span class="badgeazul" title="Desactivar">Activo</span></a>';
                    }
                    else  
                    {
                    echo $estados='<a href="#" onclick="estado('.$reg['usu_id'].')"> <span class="badgenaranja" title="Activar"> Desactivo</span></a>';
                    }
                ?></td>
            <td>
                <?php if ($tipo_soft<>'2'){ ?>
                    <a href="U_permisos.php?user=<?php echo $reg['usu_id']; ?>" title="Cambiar Permisos"  class="btn btn-xs btn-success"><i class="btn-icon-only fas fa-user-lock"></i></a>
                <?php } ?>
                <a href="#" class='btn btn-xs btn-info' title='Editar usuario' onclick="obtener_datos('<?php echo $reg['usu_id'];?>');" data-toggle="modal" data-target="#modificar_user"><i class="btn-icon-only icon-edit"></i></a> 
                <a href="#" class='btn btn-xs btn-danger' title='Eliminar usuario' onclick="eliminar('<?php echo $reg['usu_id'];?>');" ><i class="btn-icon-only icon-remove"></i></a> 
                
            </td>
        </tr>
        <?php } ?>
        <tr>
            <td colspan=7>
                <span class="pull-right"><?php  echo paginate($reload, $page, $total_pages, $adjacents); ?></span>
            </td>
        </tr>
    </table>
</div>
<?php  } } ?>
<script>
function estado(usu_id)
{
    $.ajax
    ({
        type: "POST",
        url: "php_usuarios/cambiar_estado_usuario.php",
        data: "usu_id="+usu_id,
        success: function(datos){
    $("#resultados").html(datos);
    load(1);
    }
    });
} 

function eliminar (id)
{
    var q= $("#q").val();
    if (confirm("Realmente deseas eliminar el Usuario")){ 
    $.ajax({
        type: "GET",
        url: "php_usuarios/eliminar_user.php",
        data: "id="+id,"q":q,
    beforeSend: function(objeto){
         $('#loader').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
      },
    success: function(datos){
    $("#resultados").html(datos);
    load(1);
    }
    });
    }
}
</script>


<style type="text/css">
/*Supongamos que nuestro contenedor mide 150px x 150px*/
.contenedor{
    background-color: white;
    width:60px;
    height:30px;
}
/*Aplicamos la propiedad object-fit cover, ajustar su tamaño y no perder la proporcion de nuestra imagen*/
.categorias{
    object-fit: scale-down;
    width:100%;
    height:100%;
}

</style>