<?php

   function resaltar_frase($frase, $word){
    $frase = preg_replace('#'. preg_quote($word) .'#i', '<strong><span style="color:#FF3700;font-weight: bold;">\0</span></strong>', $frase);
    return $frase;
   }
session_start();
require_once ("../conexion.php");
$empresa=1;
$sucursal=1;
$bod_id=1;
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax')
    {
        $q = mysqli_real_escape_string($con,utf8_decode((strip_tags($_REQUEST['q'], ENT_QUOTES))));
        $familia = mysqli_real_escape_string($con,(strip_tags($_REQUEST['familia'], ENT_QUOTES)));
        $proveedor = mysqli_real_escape_string($con,(strip_tags($_REQUEST['proveedor'], ENT_QUOTES)));
        $estado = mysqli_real_escape_string($con,(strip_tags($_REQUEST['estado'], ENT_QUOTES)));

    include '../include/pagination.php';
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 15; //how much records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;
    
    if($familia==null && $proveedor==null){
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos  
        where empresa='$empresa'  and art_tipo='articulo' and  (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' ) and  art_estado like '$estado'  ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = '../I_articulos.php';
        //main query to fetch the data
        $sql = mysqli_query($con,"SELECT * FROM  articulos 
        where  empresa='$empresa' and art_tipo='articulo' and  (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )  and  art_estado like '$estado' order by art_nombre asc LIMIT $offset,$per_page");
    }
    else if ($familia<>null && $proveedor==null)
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos  
        where empresa='$empresa'  and art_tipo='articulo' and familias_fam_id = '$familia' and (art_referencia like '%$q%' OR art_nombre like '%$q%'  OR art_barras like '%$q%' OR art_referencia2 like '%$q%' ) and  art_estado like '$estado'  ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = '../I_articulos.php';
        //main query to fetch the data
        $sql = mysqli_query($con,"SELECT * FROM  articulos 
        where  empresa='$empresa' and art_tipo='articulo' and  (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )  and familias_fam_id = '$familia' and  art_estado like '$estado' order by art_referencia asc LIMIT $offset,$per_page");
    }
    else  if ($familia==null && $proveedor<>null)
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos  
        where empresa='$empresa'  and art_tipo='articulo' and proveedor_pro_id = '$proveedor' and (art_referencia like '%$q%' OR art_nombre like '%$q%'  OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )  and  art_estado like '$estado' ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = '../I_articulos.php';
    //main query to fetch the data
        $sql = mysqli_query($con,"SELECT * FROM  articulos 
        where  empresa='$empresa' and art_tipo='articulo' and  (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )  and proveedor_pro_id = '$proveedor' and  art_estado like '$estado' order by art_referencia asc LIMIT $offset,$per_page");
    } 
    else  if ($familia<>null && $proveedor<>null)
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos  
        where empresa='$empresa'  and art_tipo='articulo' and proveedor_pro_id = '$proveedor'  and familias_fam_id = '$familia' and (art_referencia like '%$q%' OR art_nombre like '%$q%'  OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )  and  art_estado like '$estado' ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = '../I_articulos.php';

        $sql = mysqli_query($con,"SELECT * FROM  articulos 
        where  empresa='$empresa' and art_tipo='articulo' and  (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )  and proveedor_pro_id = '$proveedor' and  art_estado like '$estado' and familias_fam_id = '$familia'  order by art_referencia asc LIMIT $offset,$per_page");
    }  
    

    if ($numrows>0){ ?>
    <div class="table-responsive">
        <table class="table table-bordered table-hover table-striped" style="font-size: 8pt;">
            <tr>
                <th width="8%">CODIGO</th>
                <th width="8%">REFERENCIA</th>
                <th>NOMBRE</th>
                <th width="8%">TECHNOPOLIS</th>
                 <th width="8%">COMPUGAMER</th>
                 <th width="8%">PRECIO DISTRIBUIDOR</th>
                <th width="8%">PRECIO VENTA</th>
               
            </tr>
        <?php
        while ($reg=mysqli_fetch_array($sql)){
                $art_id=$reg['art_id'];
                $art_referenciac=$reg['art_referencia'];
                $id=$reg['art_id'];
                
                
              
        ?>
            <tr>
            <td>
            <?php 
                if($reg['art_referencia'] <>null)
                {   
                  $frase = $reg['art_referencia'];
                  echo resaltar_frase($frase,$q);
                }
                else
                {
                  $frase = $reg['art_barras'];
                   echo resaltar_frase($frase,$q);
                 
                } 
            ?>
            </td>
            <td><?php  $frase = $reg['art_referencia2']; echo resaltar_frase($frase,$q); ?> </td>
            <td><?php  $frase = utf8_encode($reg['art_nombre']); echo resaltar_frase($frase,$q); ?></td>
           
            <td ><p class="pull-right">
                <?php 
                    $can=mysqli_query($con,"SELECT * FROM articulos_has_bodegas WHERE articulos_art_id='$art_id' and empresa='$empresa' and sucursal='$sucursal' and bodegas='$bod_id' ") or die(mysqli_error($con));
                        $c=mysqli_fetch_array($can);
                        $art_cn=$c['art_id'];
                        echo $c['art_cantidad'];
                ?>
                </p><br>
            </td>
            
            
            <td ><p class="pull-right">
                <?php 
                $canc=mysqli_query($con,"SELECT * FROM articulos WHERE art_referencia='$art_referenciac' and empresa='3' ") or die(mysqli_error($con));
                $regc=mysqli_fetch_array($canc);
                 $art_idc=$regc['art_id'];
                
                    $canc=mysqli_query($con,"SELECT * FROM articulos_has_bodegas WHERE articulos_art_id='$art_idc' and empresa='3' and sucursal='3' and bodegas='3' ") or die(mysqli_error($con));
                        $co=mysqli_fetch_array($canc);
                      
                        echo $co['art_cantidad'];
                ?>
                </p><br>
            </td>
            <td > <?php
        $datos = mysqli_query($con,"SELECT * FROM precio p, tarifas t, articulos a where a.art_id=p.articulos_art_id and p.tarifas_tar_id=t.tar_id 
            and a.art_id='$art_id' and a.empresa='$empresa'  and p.empresa='$empresa' and t.empresa='$empresa' and tar_nombre = 'ASESOR' ") ;
            $fila = mysqli_fetch_array( $datos );
               
    echo number_format($fila['pre_precio'],0); 
   ?>
    
    </td>
                   
            <td ><p class="pull-right"><?php  echo number_format($reg['art_pvp'],0); ?></p></td>
          
        </tr>
        <?php } ?>
        <tr>
                <td colspan="7">
                    <span class="pull-right">
                        <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                    </span>
                </td>
        </tr>
        </table>
      </div>
      <?php
    }
  }
?>
<script>
    $(".btn-edit").click(function(){
      id = $(this).data("id");
      $.get("./php_articulo/tarifas_precio.php","id="+id,function(data){
        $("#form-edit").html(data);
      });
      $('#editModal').modal('show');
    });

    $(".btn-det").click(function(){
      id = $(this).data("id");
      $.get("./php_articulo/form_detallar.php","id="+id,function(data){
        $("#form-det").html(data);
      });
      $('#detallar').modal('show');
    }); 
</script>

<div class="modal fade" id="editModal" tabindex="-1" role="dialog"  >
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header" >
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Tarifas</h4>
        </div>
        <div class="modal-body">
          <div id="form-edit"></div>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

