<?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 where empresa='$empresa' and art_tipo='articulo' and art_estado='activo'");
$row=mysqli_fetch_array($count_query);
$numrows=$row['numrows'];
$total_pages=ceil($numrows/$per_page);
$reload='./index.php';
$sql="SELECT * FROM  articulos where empresa='$empresa' and art_estado='activo' and art_tipo='articulo' and (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%') LIMIT $offset,$per_page";
$query = mysqli_query($con, $sql);
if ($numrows>0){
?>
<table class="table">
	<tr class="warning">
		<th width="10%">CODIGO</th>
		<th >DESCRIPCIÓN</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($query)){
	$art_id=$row['art_id'];
	$art_referencia=$row['art_referencia'];
	$art_nombre=$row['art_nombre'];
	$art_precio_costo=$row['art_precio_costo'];

	$sql_tar=mysqli_query($con,"SELECT art_cantidad from articulos_has_bodegas where articulos_art_id='$art_id' and empresa='$empresa' and bodegas='$bod_id' and sucursal='$sucursal'");
	$rw_tar=mysqli_fetch_array($sql_tar);
	if($rw_tar==null){
		$art_cantidad=0;
	}else{
		$art_cantidad=$rw_tar['art_cantidad'];
    }
?>
	<tr>
		<td>
			<input type="hidden" id="art_cantidad<?php echo $art_referencia; ?>" value="<?php echo $art_cantidad; ?>" >
			<input type="hidden" id="art_id_<?php echo $art_referencia; ?>" value="<?php echo $art_id; ?>"><?php echo $art_referencia;?>
		</td>
		<td>
			<input type="hidden" id="nombre_<?php echo $art_referencia; ?>" value="<?php echo $art_nombre; ?>"><?php echo $art_nombre;?>
		</td>
		<td>
			<?php echo $art_cantidad; ?>
		</td>
		<td>
			<input type="text" class="form-control" style="text-align:left" id="cantidad_<?php echo $art_referencia; ?>" value="1" >
		</td>
			<input type="hidden" class="form-control" style="text-align:right" id="precio_venta_<?php echo $art_referencia; ?>" value="<?php echo $art_precio_costo;?>">
		<td>
			<span class="pull-right"><a href="#" class="btn btn-xs btn-info" onclick="agregar('<?php echo $art_referencia; ?>')"><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 } } ?>	