<?php
include('is_logged.php');//Archivo verifica que el usario que intenta acceder a la URL esta logueado
	/* Connect To Database*/
	require_once ("../config/db.php");//Contiene las variables de configuracion para conectar a la base de datos
	require_once ("../config/conexion.php");//Contiene funcion que conecta a la base de datos
	/*$tienda=$_SESSION['estado'];
	if($tienda==""){$tienda="Almacen Central";}*/
	$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
	if($action == 'ajax'){
		// escaping, additionally removing everything that could be (html/javascript-) code
	$q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
	$tienda = mysqli_real_escape_string($con,(strip_tags($_REQUEST['origen'], ENT_QUOTES)));
	$aColumns = array('marca', 'nombre_producto');//Columnas de busqueda
	$sTable = "products";
	$sWhere = "WHERE almacen='".$tienda."'";
	if ( $_GET['q'] != "" )
	{
		$sWhere .= " AND (";
		for ( $i=0 ; $i<count($aColumns) ; $i++ )
		{
			$sWhere .= $aColumns[$i]." LIKE '%".$q."%' OR ";
		}
		$sWhere = substr_replace( $sWhere, "", -3 );
		$sWhere .= ')';
	}
	include 'pagination.php'; //include pagination file
	//pagination variables
	$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
	$per_page = 6; //how much records you want to show
	$adjacents  = 4; //gap between pages after number of adjacents
	$offset = ($page - 1) * $per_page;
	//Count the total number of row in your table*/
	$count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM $sTable $sWhere") or die(mysqli_error($con));
	$row= mysqli_fetch_array($count_query) or die(mysqli_error($con));
	$numrows = $row['numrows'];
	$total_pages = ceil($numrows/$per_page);
	$reload = '../nuevo_movimiento.php';
	//main query to fetch the data
	$sql="SELECT * FROM  $sTable $sWhere order by nombre_producto asc LIMIT $offset,$per_page";
	$query = mysqli_query($con, $sql);
	
	?>
	<div class="table-responsive">
		<table class="table" style="font-size:11px;">
			<tr class="warning">
				<th>Producto</th>
				<th>Presentacion</th>
				<th>Existencia</th>
       		    <th><span class="pull-right">Cant</span></th>
				
       		    <th class='text-center'>Agregar</th>
			</tr>	
			<?php 
			if ($numrows>0)
			{
				while ($row=mysqli_fetch_array($query))
				{
					$id_producto=$row['id_producto'];
					$codigo_producto=$row['codigo_producto'];
					$presen_producto=$row['status_producto'];
					$nombre_producto=$row['nombre_producto'];
					$marca_producto=$row['marca'];
					$existencia_producto=$row['existencia'];
                   			
					?>
					<tr>
						<td><?php echo $nombre_producto; ?></td>
						<td><?php echo $presen_producto; ?></td>
						<td><?php echo $existencia_producto; ?></td>
						<td class='col-xs-2'>
							<input type="text" class="form-control" style="text-align:right" id="cantidad_<?php echo $id_producto; ?>"  value="1" required />
    	            	<input type="text" class="form-control" style="text-align:right; display:none;" id="precio_venta_<?php echo $id_producto; ?>" value="<?php echo $precio_venta; ?>">
						<input type="text" class="form-control" style="text-align:right; display:none;" id="precio_costo_<?php echo $id_producto; ?>" value="<?php echo $precio_costo; ?>">
					
	                    <td class='text-center'><a class='btn btn-primary'href="#" onclick="agregar('<?php echo $id_producto ?>')"><i class="glyphicon glyphicon-plus"></i></a></td>
					</tr>
				<?php
				}
			}
			else
			{
			?>
				<tr>
				<td colspan="6">No se encontraron resultados <?php echo "en tienda $tienda" ?></td>
			</tr>
            <?php 
			} 
			?>
            	<tr>
				
					<td colspan=5>
                    	<span class="pull-right">
						<?php
					 		echo paginate($reload, $page, $total_pages, $adjacents);
						?>
                        </span>
                    </td>
				</tr>
			</table>
<?php 
} 
?>