<?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';
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 13; 
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;

    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM traslado where empresa='$empresa' and (tra_n_traslado like '%$q%')");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = '../I_bodegas.php';
    $sql = mysqli_query($con,"SELECT * FROM traslado where empresa='$empresa' and (tra_n_traslado like '%$q%') order by tra_id desc LIMIT $offset,$per_page");
    if ($numrows>0){
?>
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th width="4%"></th>
                <th width="4%"> N°</th>
                <th width="10%">FECHA</th>
                <th width="8%">HORA</th>
                <th >SUCURSAL DE ENVIO</th>
                <th >SUCURSAL DE DESTINO</th>
                <th width="20%">USUARIO</th>
                <th width="10%"></th>
            </tr>
        </thead>
        <tbody class="buscar">
            <?php while ($reg=mysqli_fetch_array($sql)){?>
            <tr>
               <td>
                <?php if($reg['tra_sucursal_d']<>$sucursal){ ?><i class="btn-icon-only icon-long-arrow-left alert-danger fa-2x"></i><?php } else { ?><i class="btn-icon-only icon-long-arrow-right alert-success fa-2x"></i><?php } ?>
                </td>  
                <td><?php  echo $reg['tra_n_traslado']; ?></td>
                <td><?php  echo $reg['tra_fecha']; ?></td>
                <td><?php  echo $reg['tra_hora']; ?></td>
                <td><?php  
                    $sql_tar=mysqli_query($con, "SELECT * from sucursal where suc_id='".$reg['tra_sucursal_o']."'");
                    $bo=mysqli_fetch_array($sql_tar);
                    echo $nom=$bo['suc_nombre'];?></td>
                    
                      <td><?php  
                    $sql_tar=mysqli_query($con, "SELECT * from sucursal where suc_id='".$reg['tra_sucursal_d']."'");
                    $bo=mysqli_fetch_array($sql_tar);
                    echo $nom=$bo['suc_nombre'];?></td>
                    <td>
                    <?php  
                    $sql_tar=mysqli_query($con, "SELECT * from usuario where usu_id='".$reg['tra_usuario']."'");
                    $bo=mysqli_fetch_array($sql_tar);
                    echo $usu_n=$bo['usu_nombre'];?>
                </td>
                <td >
                    <a href="#verdetalle<?php echo $reg["tra_id"];?>" data-toggle="modal"  title="Ver Traslado"  class="btn btn-xs btn-info"><i class="btn-icon-only icon-eye-open"></i> </a>
                 <?php if($reg['tra_sucursal_o']<>$sucursal){ ?>
                    <!--<a href="#" class='btn btn-xs btn-danger' title='Eliminar Traslado' onclick="eliminar('<?php echo $reg['tra_id'];?>');" ><i class="btn-icon-only icon-remove"></i></a>-->
                <?php } ?>
                <?php include ('form_detalle.php'); ?>
                    <a href="libraries/tcpdf/documentos/traslado_suc.php?tra_id=<?php echo $reg['tra_id'];?>" target="_blank" class="btn btn-success btn-xs" ><span class="btn-icon-only icon-print"></span></a>
                </td>
            </tr>
            <?php }?>
            <tr>
                <td colspan=8><span class="pull-right">
                <?php  echo paginate($reload, $page, $total_pages, $adjacents); ?></span>
                </td>
            </tr>
        </tbody>
    </table>
</div>    
<?php } } ?>
<script>
function eliminar (id)
{
    var q= $("#q").val();
    if (confirm("Realmente deseas eliminar el Traslado")){ 
    $.ajax({
        type: "GET",
        url: "php_traslados/eliminar_traslado.php",
        data: "id="+id,"q":q,
        beforeSend: function(objeto){
            $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
            $("#resultados").html(datos);
            load(1);
        }
    });
    }
}
</script>