<?php
require_once ("../conexion.php");
session_start();
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)));
    $tipo = mysqli_real_escape_string($con,(strip_tags($_REQUEST['tipo'], ENT_QUOTES)));
    $buscar = mb_strtoupper($q);

    include '../include/pagination.php';
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 5; 
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;
?>
    <table class="table">
        <tr class="warning">
            <th width="10%">CODIGO</th>
            <th >NOMBRE</th>
            <th width="5%"><span class="pull-center">STOCK</span></th>
            <th width="10%"><span class="pull-right">CANTIDAD</span></th>
            <th width="15%"><span class="pull-center">PRECIO</span></th>
            <th width="9.5%"></th>
        </tr>
<?php
if($tipo=='articulos')
{
    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM articulos a, articulos_has_bodegas b
    where a.art_id=b.articulos_art_id and a.empresa='$empresa' and b.sucursal='$sucursal' and a.art_estado='activo' and a.art_tipo='articulo'");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './index.php';
    $sql=mysqli_query($con,"SELECT *, a.art_id as art_id ,b.art_id as art_id_bod FROM articulos a, articulos_has_bodegas b
    where a.art_id=b.articulos_art_id and a.empresa='$empresa' and b.sucursal='$sucursal' and a.art_estado='activo' and a.art_tipo='articulo' and (a.art_referencia like '%$q%' OR a.art_nombre like '%$q%' OR a.art_barras like '%$q%' OR a.art_referencia2 like '%$q%') order by b.art_cantidad DESC, a.art_referencia ASC LIMIT $offset,$per_page");
    if ($numrows>0){
?>
        <?php
        while ($row=mysqli_fetch_array($sql)){
            $art_id_bod=$row['art_id_bod'];
            $art_id=$row['art_id'];
            $art_referencia=$row['art_referencia'];
            $art_nombre=$row['art_nombre'];
            $art_precio_costo=$row['art_precio_costo'];
            $art_pvp=$row['art_pvp'];
            $art_cantidad=$row['art_cantidad'];

        ?>
            <tr>
                <td>
                    <input type="hidden" id="art_id<?php echo $art_id_bod; ?>" value="<?php echo $art_id; ?>">
                    <?php echo $art_referencia; ?>
                    <input type="hidden" id="referencia<?php echo $art_id_bod; ?>" value="<?php echo $art_referencia; ?>">
                </td>
                <td>
                    <input type="hidden" id="nombre<?php echo $art_id_bod; ?>" value="<?php echo $art_nombre; ?>">
                    <?php echo $art_nombre; ?>
                    <span style="color: red;">
                    <?php if($row["art_atributo_estado"]=='si') 
                        {  
                            $atributo1=$row['atributo1'];
                            $atri1=mysqli_query($con,"SELECT * FROM atributos1 where atri_art_id='$art_id' and atri_id='$atributo1'");
                            $fila1 = mysqli_fetch_array($atri1);
                            if (isset($fila1['atri_nombre'])) { echo $atri_nombre1=$fila1['atri_nombre']; }

                            $atributo2=$row['atributo2'];
                            $atri2=mysqli_query($con,"SELECT * FROM atributos2 where atri_art_id2='$art_id' and atri_id2='$atributo2'");
                            $fila2 = mysqli_fetch_array($atri2);
                            if (isset($fila2['atri_nombre2'])) { echo ' / '; echo $atri_nombre2=$fila2['atri_nombre2']; }
                        }
                    ?>
                    </span>
                </td>
                <td><?php echo $art_cantidad; ?></td>
                <td>
                    <input type="text" class="form-control" style="text-align:left" id="cantidad_<?php echo $art_id_bod; ?>" value="1">
                </td>
                <td>
                    <input type="text" class="form-control" style="text-align:left" id="precio_<?php echo $art_id_bod; ?>" value="<?php echo $art_pvp; ?>">
                </td>
                <td>
                    <span class="pull-right"><a href="#" class="btn btn-xs btn-info" onclick="agregar('<?php echo $art_id_bod ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
                    <a data-id="<?php echo $art_id;?>" class="btn btn-edit btn-xs btn-warning" title="Tarifas de Precio"> <i class="btn-icon-only icon-usd"></i></a>
                </td>
            </tr>
            <?php } ?>
            <tr>
                <td colspan=6>
                    <span class="pull-right">
                        <?php echo paginate($reload, $page, $total_pages, $adjacents);?>
                    </span>
                </td>
            </tr>
      </table>
<?php }  }



if($tipo=='servicio' OR $tipo=='producto')
{
    $tip=$tipo;
    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM articulos 
    where empresa='$empresa' and art_tipo='$tip' and (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%') ");
    $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='$tip' and (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%') order by art_nombre desc LIMIT $offset,$per_page");
    if ($numrows>0)
    {
        while ($row=mysqli_fetch_array($sql))
        {
            $art_id=$row['art_id'];
            $art_tipo=$row['art_tipo'];
            $art_referencia=$row['art_referencia'];
            $art_nombre=mb_strtoupper($row['art_nombre']);
            $proveedor_pro_id=$row['proveedor_pro_id'];
            $pre_precio=$row['art_pvp'];
            $art_iva=$row['art_iva'];
            $art_tipo=$row['art_tipo'];
            $art_precio_costo=$row['art_precio_costo'];
            $art_cantidad=0;
?>
        <tr>
            <td>
                <input type="hidden" id="tipo<?php echo $art_id;?>" value="<?php echo $art_tipo; ?>" >
                <input type="hidden" id="art_referencia<?php echo $art_id;?>" value="<?php echo $art_referencia; ?>" >
                <?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_referencia ));?>
            </td>
            <td>
                <input type="hidden" id="nombre<?php echo $art_id;?>" value="<?php echo $art_nombre; ?>"><?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_nombre ));?>
            </td>
             <td><?php //echo $art_cantidad; ?></td>
            <td>
                <input type="text" class="form-control input-sm" style="text-align:left" id="cantidad<?php echo $art_id;?>"  value="1" >
            </td>
          
            <td>
                <input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta<?php echo $art_id;?>" value="<?php echo $pre_precio;?>" >
            </td>
            <td>
                <span class="pull-right"><a href="#" title="Agregar" class="btn btn-xs btn-info" onclick="agregar_producto('<?php echo $art_id ?>')"><i class="btn-icon-only icon-ok"></i></a></span>
            </td>
        </tr>
<?php }  } ?>
    <tr>
        <td colspan="7"><span class="pull-right">
            <?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
        </td>
    </tr>
<?php  }  }  ?>




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