<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
?>
<?php
  $action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';

if($action == 'ajax'){
    // escaping, additionally removing everything that could be (html/javascript-) code
    $q = mysqli_real_escape_string($con,(strip_tags(utf8_decode($_REQUEST['q']), ENT_QUOTES)));
    include '../include/pagination.php'; //include pagination file
    //pagination variables
    $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 the total number of row in your table*/
    $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM dis_cliente  where  distribuidor='$distribuidor'  and  (clie_nombre like '%$q%' OR clie_cc like '%$q%') ");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './P_pos.php';
    //main query to fetch the data
    $sql="SELECT * FROM  dis_cliente  where  distribuidor='$distribuidor'  and  (clie_nombre like '%$q%' OR clie_cc like '%$q%'  ) order by clie_nombre asc LIMIT $offset,$per_page";
    $query = mysqli_query($con, $sql);
    //loop through fetched data
    if ($numrows>0){
      
      ?>
      <div class="table-responsive">
      <table class="table table-striped table-bordered">
            <thead>
              <tr>
                <th width="12%">N° C.C</th>
                <th>NOMBRE</th>
                <th>CORREO</th>
                <th>TELÉFONO</th>
                <th width="12%"></th>
              </tr>
            </thead>
            <tbody  class="buscar">
            <?php
                   while ($reg=mysqli_fetch_array($query)){
                    $clie_nombre=utf8_encode($reg['clie_nombre']);
                    $clie_direccion=utf8_encode($reg['clie_direccion']);
            ?>
          <input type="hidden" value="<?php echo $reg['clie_tipo_doc'];?>" id="tipo<?php echo $reg['clie_id'];?>">
          <input type="hidden" value="<?php echo $reg['clie_cc'];?>" id="cc<?php echo $reg['clie_id'];?>">
          <input type="hidden" value="<?php echo utf8_encode($reg['clie_nombre']);?>" id="nombre<?php echo $reg['clie_id'];?>">
          <input type="hidden" value="<?php echo $reg['clie_direccion'];?>" id="direccion<?php echo $reg['clie_id'];?>">
          <input type="hidden" value="<?php echo $reg['clie_telefono'];?>" id="telefono<?php echo $reg['clie_id'];?>">
          <input type="hidden" value="<?php echo $reg['clie_correo'];?>" id="correo<?php echo $reg['clie_id'];?>">
        
              <tr>
                <td><?php  echo $reg['clie_cc']; ?></td>
                <td><?php  echo $clie_nombre; ?></td>
                <td><?php  echo $reg['clie_correo']; ?></td>
                <td><?php  echo $reg['clie_telefono']; ?></td>
                <td>

                <a href="#" class='btn btn-xs btn-info' title='Editar Cliente' onclick="obtener_datos('<?php echo $reg['clie_id'];?>');" data-toggle="modal" data-target="#modificarcliente"><i class="btn-icon-only icon-edit"></i></a> 
                <a href="#" class='btn btn-xs btn-danger' title='Editar usuario' onclick="eliminar('<?php echo $reg['clie_id'];?>');" ><i class="btn-icon-only icon-remove"></i></a> 
                 
                </td>
              </tr>
            <?php }?>
           <tr>
          <td colspan=12><span class="pull-right">
          <?php
           echo paginate($reload, $page, $total_pages, $adjacents);
          ?></span></td>
        </tr>
        </table>
      </div>
      <?php
    }
  }
?>