<?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 = 20; 
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;
    $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM proveedor where empresa='$empresa' and (pro_nombre like '%$q%' OR pro_nit like '%$q%') ");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './P_pos.php';
    $sql="SELECT * FROM proveedor where empresa='$empresa' and (pro_nombre like '%$q%' OR pro_nit like '%$q%') order by pro_nombre asc LIMIT $offset,$per_page";
    $query = mysqli_query($con, $sql);
    if ($numrows>0){
?>
<div class="table-responsive">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th width="12%">NIT</th>
                <th>NOMBRE</th>
                <th>TELEFONO</th>
                <th>DIRECCION</th>
                <th>CIUDAD</th>
                <th width="18%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
        <?php
            while ($reg=mysqli_fetch_array($query)){
        ?>
      <input type="hidden" value="<?php echo $reg['pro_id'];?>" id="id<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_tipo_doc'];?>" id="tipo_doc2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_nit'];?>" id="nit2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_dv'];?>" id="pro_dv2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_nombre'];?>" id="nombre2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_nombre_repre'];?>" id="nombre_repre2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_telefono'];?>" id="telefono2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_correo'];?>" id="correo2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_direccion'];?>" id="direccion1<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_direccion2'];?>" id="direccion2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_departamento'];?>" id="departamento2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_ciudad'];?>" id="ciudad2<?php echo $reg['pro_id'];?>">
      <input type="hidden" value="<?php echo $reg['pro_codigo_postal'];?>" id="pro_codigo_postal2<?php echo $reg['pro_id'];?>">
       
            <tr>
                <td><?php  echo $reg['pro_nit']; ?></td>
                <td><?php  echo $reg['pro_nombre']; ?></td>
                <td><?php  echo $reg['pro_telefono']; ?></td>
                <td><?php  echo $reg['pro_direccion']; ?> </br> <?php  echo $reg['pro_direccion2']; ?></td>
                <td><?php  
                    $pro_ciudad = $reg['pro_ciudad']; 
                    if (!empty($pro_ciudad)) {
                        $ciu = mysqli_query($api, "SELECT name FROM municipalities WHERE id = '$pro_ciudad'");
                        if ($ciu && $ciud = mysqli_fetch_array($ciu)) {
                            echo $name = mb_strtoupper($ciud['name']);
                        }
                    }
                    ?>
                </td>
                <td>
                <?php
                if ($reg['pro_nombre']=='PROVEEDOR INICIAL'){ 
                ?>
                    <a href="U_proveedores_historial.php?pro_id=<?php echo $reg['pro_id']; ?>" title="Historial Compras"  class="btn btn-xs btn-success" data-toggle="modal"> <i class="btn-icon-only fa fa-history"></i></a>
                <?php } else { ?>
                    <a href="U_proveedores_historial.php?pro_id=<?php echo $reg['pro_id']; ?>" title="Historial Compras"  class="btn btn-xs btn-success" data-toggle="modal"> <i class="btn-icon-only fa fa-history"></i></a>
                    <a href="libraries/tcpdf/documentos/rotulo_proveedor.php?pro_id=<?php echo $reg['pro_id']; ?>" target="_blank" title="Datos de Envio" class="btn btn-xs  btn-primary" data-toggle="modal"> <i class="btn-icon-only icon-truck"></i></a>
                    <a href="#" class='btn btn-xs btn-info' title='Editar Proveedor' onclick="obtener_datos('<?php echo $reg['pro_id'];?>');" data-toggle="modal" data-target="#modificarproveedor"><i class="btn-icon-only icon-edit"></i></a> 
                    <a href="#" class='btn btn-xs btn-danger' title='Eliminar Proveedor' onclick="eliminar('<?php echo $reg['pro_id'];?>');" ><i class="btn-icon-only icon-remove"></i></a> 
                <?php }?>
                </td>
            </tr>
        <?php }?>
            <tr>
                <td colspan=6><span class="pull-right">
                <?php
                    echo paginate($reload, $page, $total_pages, $adjacents);
                ?></span>
            </td>
        </tr>
    </table>
</div>
<?php } } ?>
<script>
function eliminar (id)
{
    var q= $("#q").val();
    if (confirm("Realmente deseas eliminar el proveedor")){ 
    $.ajax({
        type: "GET",
        url: "php_proveedores/eliminar.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>