<?php

	/*-------------------------
	Autor: Obed Alvarado
	Web: obedalvarado.pw
	Mail: info@obedalvarado.pw
	---------------------------*/
	/* 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
	require_once ("../libraries/inventory.php");//Contiene funcion que conecta a la base de datos
	$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)));
        $category_id= mysqli_real_escape_string($con,(strip_tags($_REQUEST["categoria"],ENT_QUOTES)));
		 $aColumns = array('product_code', 'product_name');//Columnas de busqueda
		 $sTable = "products, inventory";
		 $sWhere = "";
		 $sWhere .= " where products.product_id=inventory.product_id";
		 if ( $_GET['q'] != "" )
		{
			$sWhere .= " and (products.product_code LIKE '%".$q."%' OR products.product_name LIKE '%".$q."%')";
		}

		if ( $_GET['categoria'] != "" )
		{
			$sWhere .= " and products.category_id=".$category_id."";
		}
		$sWhere .=" and products.status=1";
		$sWhere .=" order by products.product_name";
		include 'pagination.php'; //include pagination file
		//pagination variables
		$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
		$per_page = 200; //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");
		$row= mysqli_fetch_array($count_query);
		$numrows = $row['numrows'];
		$total_pages = ceil($numrows/$per_page);
		$reload = './index.php';
		//main query to fetch the data
		$sql="SELECT * FROM  $sTable $sWhere LIMIT $offset,$per_page";
		$query = mysqli_query($con, $sql);
		//loop through fetched data
		if ($numrows>0){
			
			?>
			
			  
				<?php
				/*Datos de la empresa*/
				$sql_empresa=mysqli_query($con,"SELECT * FROM  business_profile, currencies where business_profile.currency_id=currencies.id and business_profile.id=1");
				$rw_empresa=mysqli_fetch_array($sql_empresa);
				$moneda=$rw_empresa["symbol"];
				/*Fin datos empresa*/
				while ($row=mysqli_fetch_array($query)){
					$id_producto=$row['product_id'];
					$product_code=$row['product_code'];
					$product_name=$row['product_name'];
					$image_path=$row['image_path'];
					$note=$row['note'];
					$category_id=$row['category_id'];
					$sql_marca=mysqli_query($con, "select name from categories where id='$category_id'");
					$rw_marca=mysqli_fetch_array($sql_marca);
					$nombre_marca=$rw_marca['name'];
					$selling_price=$row["selling_price"];
					$selling_price=number_format($selling_price,$currency_format['precision_currency'],'.','');
					?>
					 <div class="col-lg-2 box Oil">
						<div class="widget-panel widget-style-2 ">
                            <center>
                                <img src="<?php echo$image_path;?>" onclick="agregar('<?php echo $id_producto ?>')"
                                     height="100px" width="100px" alt="">
                            </center>
                        <div class="text-muted m-t-5 text-center" style="height: 40px"><span
                                class="name"><?php echo $product_name; ?></span> <br>
                                <input type="hidden" value="1" id="cantidad_<?php echo $id_producto; ?>" name="">
	                                </div>
                                <h4 class="text-success text-center">
                                	<input type="hidden" value="<?php echo $selling_price;?>"  id="precio_venta_<?php echo $id_producto; ?>" name="">
                                	<b data-plugin="counterup"><?php echo $moneda;?> <?php echo $selling_price;?> </b>
                                </h4>
                        </div>
					</div>
					<?php
				}
				?>		
			<?php
		}
	}
?>


