<?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=10; //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 sucursal where empresa='$empresa' and (suc_nombre like '%$q%' ) ");
    $row=mysqli_fetch_array($count_query);
    $numrows=$row['numrows'];
    $total_pages=ceil($numrows/$per_page);
    $reload='./U_usuarios.php';
    $sql="SELECT * FROM sucursal where empresa='$empresa' and  (suc_nombre like '%$q%') order by suc_nombre asc LIMIT $offset,$per_page";
    $query = mysqli_query($con, $sql);
    if ($numrows>0){
?>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>NOMBRE</th>
            <th>DIRECCIÓN</th>
            <th>TELÉFONOS</th>
            <th>CORREO</th>
            <th width="10%"></th>
        </tr>
    </thead>
        <?php  while ($reg=mysqli_fetch_array($query)){ ?>
            <input type="hidden" value="<?php echo $reg['suc_nombre'];?>" id="suc_nombre<?php echo $reg['suc_id'];?>">
            <input type="hidden" value="<?php echo $reg['suc_direccion'];?>" id="suc_direccion<?php echo $reg['suc_id'];?>">
            <input type="hidden" value="<?php echo $reg['suc_direccion_envio'];?>" id="suc_direccion_envio<?php echo $reg['suc_id'];?>">
            <input type="hidden" value="<?php echo $reg['suc_telefono'];?>" id="suc_telefono<?php echo $reg['suc_id'];?>">
            <input type="hidden" value="<?php echo $reg['suc_celular'];?>" id="suc_celular<?php echo $reg['suc_id'];?>">
            <input type="hidden" value="<?php echo $reg['suc_correo'];?>" id="suc_correo<?php echo $reg['suc_id'];?>">
        <tr>
            <td><?php  echo $reg['suc_nombre']; ?></td>
            <td><?php  echo $reg['suc_direccion']; ?></td>
            <td><?php  echo $reg['suc_telefono']; echo' - '; echo $reg['suc_celular']; ?></td>
            <td><?php  echo $reg['suc_correo']; ?></td>
            <td>
              <a href="#" class='btn btn-xs btn-success' title='Editar Sucursal' onclick="obtener_datos('<?php echo $reg['suc_id'];?>');" data-toggle="modal" data-target="#modificar_sucursal"><i class="btn-icon-only icon-edit"></i></a> 
             <!-- <a href="#" class='btn btn-xs btn-danger' title='Eliminar Sucursal' onclick="eliminar('<?php echo $reg['suc_id'];?>');" ><i class="btn-icon-only icon-remove"></i></a> -->
            </td>
        </tr>
    <?php } ?>
        <tr>
            <td colspan=5><span class="pull-right">
            <?php
                 echo paginate($reload, $page, $total_pages, $adjacents);
            ?></span></td>
        </tr>
    </table>
<?php } } ?>
<script>
function eliminar (id)
{
    var q= $("#q").val();
    if (confirm("Realmente deseas eliminar la sucursal")){ 
    $.ajax({
        type: "GET",
        url: "php_sucursal/eliminar_sucursal.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>