<?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(mb_strtoupper($_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 empleados  where  empresa='$empresa'  and  (emp_nombre like '%$q%' OR emp_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  empleados  where  empresa='$empresa'  and  (emp_nombre like '%$q%' OR emp_cc like '%$q%'  ) order by emp_id desc 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)){
                    $emp_nombre=($reg['emp_nombre']);
                    $emp_direccion=($reg['emp_direccion']);
            ?>
          <input type="hidden" value="<?php echo $reg['emp_tipo_doc'];?>" id="tipo_doc<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_cc'];?>" id="cc<?php echo $reg['emp_id'];?>">

          <input type="hidden" value="<?php echo mb_strtoupper($reg['emp_nombre']);?>" id="nombre<?php echo $reg['emp_id'];?>">

          <input type="hidden" value="<?php echo mb_strtoupper($reg['emp_primer_apellido']);?>" id="primer_apellido<?php echo $reg['emp_id'];?>">

          <input type="hidden" value="<?php echo mb_strtoupper($reg['emp_segundo_apellido']);?>" id="segundo_apellido<?php echo $reg['emp_id'];?>">

          <input type="hidden" value="<?php echo mb_strtoupper($reg['emp_direccion']);?>" id="direccion<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_telefono'];?>" id="telefono<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo mb_strtoupper($reg['emp_correo']);?>" id="correo<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_municipio'];?>" id="ciudad<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_tipo_trabajador'];?>" id="tipo_trabajador<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_subtipo_trabajador'];?>" id="subtipo_trabajador<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_tipo_contrato'];?>" id="tipo_contrato<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_salario'];?>" id="salario<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_salario_integral'];?>" id="salario_integral<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_alto_riesgo'];?>" id="alto_riesgo<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_fecha_adminsion'];?>" id="fecha_adminsion<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_metodo_pago'];?>" id="metodo_pago<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo mb_strtoupper($reg['emp_banco']);?>" id="banco<?php echo $reg['emp_id'];?>">

          <input type="hidden" value="<?php echo $reg['emp_tipo_cuenta'];?>" id="tipo_cuenta<?php echo $reg['emp_id'];?>">
          <input type="hidden" value="<?php echo $reg['emp_numero_cuenta'];?>" id="numero_cuenta<?php echo $reg['emp_id'];?>">

      
              <tr>
                <td><?php  echo $reg['emp_cc']; ?></td>
                <td><?php  echo $reg['emp_nombre']; ?></td>
                <td><?php  echo $reg['emp_correo']; ?></td>
                <td><?php  echo $reg['emp_telefono']; ?></td>
                <td>
                  <a href="#" class='btn btn-xs btn-info' title='Editar Empleado' onclick="obtener_datos('<?php echo $reg['emp_id'];?>');" data-toggle="modal" data-target="#modificarempleado"><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['emp_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
        }
      } 
      ?>