<?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 = 6; 
	$adjacents  = 4; 
	$offset = ($page - 1) * $per_page;
	$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){
?>
<table class="table">
	<tr class="warning">
		<th width="10%">REFERENCIA</th>
		<th >NOMBRE</th>
		<th width="6%"><span class="pull-right">STOCK</span></th>
		<th width="15%"><span class="pull-left">CANTIDAD</span></th>
		<th width="5%"></th>
	</tr>
	<?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_cantidad=$row['art_cantidad'];
	?>
	<tr>
		<td>
			<input type="hidden" id="art_cantidad<?php echo $art_id_bod; ?>"  value="<?php echo $art_cantidad; ?>" >
			<input type="hidden" id="art_id_<?php echo $art_id_bod; ?>"  value="<?php echo $art_id; ?>" >
			<input type="hidden" id="art_referencia<?php echo $art_id_bod; ?>"  value="<?php echo $art_referencia; ?>" >
			<?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" >
			<input type="hidden" class="form-control" style="text-align:right" id="precio_venta_<?php echo $art_id_bod; ?>"  value="<?php echo $art_precio_costo;?>" >
		</td>
		<td >
			<span class="pull-left"><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>
		</td>
	</tr>
	<?php } ?>
	<tr>
		<td colspan=5>
			<span class="pull-right"><?php echo paginate($reload, $page, $total_pages, $adjacents); ?></span>
		</td>
	</tr>
</table>
<?php } } ?>