<?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)));
    $busc_tarifa = mysqli_real_escape_string($con,(strip_tags(($_REQUEST['busc_tarifa']), ENT_QUOTES)));
    $buscar = mb_strtoupper($q);
    include '../include/pagination.php'; //include pagination file
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = $conf_pagi_cliente; //how much records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;
    if($busc_tarifa==null)
    {
        $count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM cliente where empresa='$empresa' and (clie_nombre like '%$q%' OR clie_cc like '%$q%' OR clie_empresa like '%$q%') ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './P_pos.php';
        $sql="SELECT * FROM  cliente  where  empresa='$empresa' and (clie_nombre like '%$q%' OR clie_cc like '%$q%'  OR clie_empresa like '%$q%'  OR clie_direccion like '%$q%' OR clie_barrio like '%$q%' ) order by clie_nombre,clie_direccion,clie_barrio asc LIMIT $offset,$per_page";
        $query = mysqli_query($con, $sql);
    }
    if($busc_tarifa<>null)
    {
        $count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM cliente where tarifas_tar_id='$busc_tarifa' and empresa='$empresa' and (clie_nombre like '%$q%' OR clie_cc like '%$q%' OR clie_empresa like '%$q%') ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './P_pos.php';
        $sql="SELECT * FROM  cliente  where tarifas_tar_id='$busc_tarifa' and  empresa='$empresa' and (clie_nombre like '%$q%' OR clie_cc like '%$q%'  OR clie_empresa like '%$q%'  OR clie_direccion like '%$q%' OR clie_barrio like '%$q%' ) order by clie_nombre,clie_direccion,clie_barrio 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>
            <?php if($negocio_neg_id=='7') { ?>
             <tr>
                <th width="12%">N° C.C</th>
                <th>NOMBRE</th>
                <th>DIRECCION</th>
                <th>BARRIO</th>
                <th>TELÉFONO</th>
                <th width="8%">NOTA_CREDITO</th>
                <th width="18%"></th>
            </tr>
            <?php } else { ?>   
            <tr>
                <th width="12%">N° C.C</th>
                <th>NOMBRE</th>
                <th>REPRESENTANTE</th>
                <th>CORREO</th>
                <th>TELÉFONO</th>
                <th width="8%">NOTA_CREDITO</th>
                <th width="18%"></th>
            </tr>
            <?php } ?>
        </thead>
        <tbody  class="buscar">
        <?php
            while ($reg=mysqli_fetch_array($query)){
            $clie_nombre=mb_strtoupper($reg['clie_nombre']);
        ?>
        <tr>
            <td><?php  echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $reg['clie_cc'])); ?></td>
            <td>
            <?php  
                echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $clie_nombre));  
                $tarifas_tar_id=$reg['tarifas_tar_id'];
                $datos=mysqli_query($con,"SELECT * FROM tarifas where tar_id='$tarifas_tar_id'") ;
                $fila=mysqli_fetch_array($datos);
                $tar_nombre=$fila['tar_nombre'];
                if($tar_nombre=='ASESOR')
                {
                    echo $icon= '<i class="btn-icon-only fa fa-check-circle alert-info" aria-hidden="true"></i>';
                }
               
                if($reg['facturador']=='si')
                {
                    echo $facturador= '<i class="btn-icon-only fa fa-desktop alert-danger" aria-hidden="true"></i>';
                }
            ?>
            </td>

            <?php if($negocio_neg_id=='7') { ?>
            <td><?php echo $reg['clie_direccion']; ?></td>
            <td><?php echo $reg['clie_barrio']; ?></td>
            <td><?php echo $reg['clie_telefono']; ?></td>
            <?php } else { ?>
            <td><?php echo $reg['clie_empresa']; ?></td>
            <td><?php echo $reg['clie_correo']; ?></td>
            <td><?php echo $reg['clie_telefono']; ?></td>
            <?php } ?>

            <td><span class="pull-right"><?php echo number_format($reg['clie_nota_credito'],0); ?></span></td>
            <td>
                <a href="U_clientes_historial.php?clie_id=<?php echo $reg['clie_id']; ?>" title="Historial" class="btn btn-xs btn-success"> <i class="btn-icon-only fa fa-history"></i></a>
          
                <a data-id="<?php echo $reg['clie_id'];?>" class="btn btn-xs  btn-edit_cli btn-info" title="Editar Cliente"><i class="btn-icon-only icon-edit"></i></a> 
     
                <?php if ($reg['clie_nombre'] <> 'CLIENTE MOSTRADOR' && $reg['clie_nombre']<> 'CONSUMIDOR FINAL')  { ?>
                <a href="libraries/tcpdf/documentos/rotulo_cliente.php?clie_id=<?php echo $reg['clie_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-warning" title='Enviar email' onclick="cliente_email('<?php echo $reg['clie_id']; ?>','<?php //echo $reg['ven_correo']; ?>')"><i class="btn-icon-only icon-envelope"> </i></a>
                
                <a href="#" class='btn btn-xs btn-danger' title='Eliminar usuario' onclick="eliminar('<?php echo $reg['clie_id'];?>');" ><i class="btn-icon-only icon-remove"></i></a> 
                <?php } ?>
            </td>
        </tr>
        <?php }?>
        <tr>
            <td colspan=7>
                <span class="pull-right"><?php  echo paginate($reload, $page, $total_pages, $adjacents); ?></span>
            </td>
        </tr>
    </tbody>
    </table>
</div>
<?php  } } ?>
<script type="text/javascript">

    $(".btn-edit_cli").click(function(){
        id = $(this).data("id");
        $.get("./php_clientes/form_modificar_cliente_ven.php","id="+id,function(data){
            $("#cli-edit").html(data);
        });
        $('#edit_cliente').modal('show');
    });
</script>

<div class="modal fade" id="edit_cliente" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Modificar Cliente</h4>
            </div>
            <div id="cli-edit"></div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
