<?php

include('is_logged.php');
require_once ("../config/db.php");
require_once ("../config/conexion.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)));
	$est=mysqli_real_escape_string($con,(strip_tags($_REQUEST['est'], ENT_QUOTES)));
	$tienda=$_SESSION['estado'];
    $sTable="products";
	$sWhere="";
	
	if(empty($tienda))
	{
		if(empty($est)) $sWhere="WHERE almacen='nada'";
		if(!empty($est) && empty($q)) $sWhere="WHERE almacen='".$est."'";
		if(!empty($est) && !empty($q)) $sWhere="WHERE almacen='".$est."' AND nombre_producto LIKE '%".$q."%'";
		$_SESSION['tie']=$est;
	}
	if(!empty($tienda))	
	{
		if(empty($q)) $sWhere="WHERE almacen='".$tienda."'";
		if(!empty($q)) $sWhere="WHERE almacen='".$tienda."' AND nombre_producto LIKE '%".$q."%'";
		$_SESSION['tie']=$tienda;
	}
	
	$query2=mysqli_query($con, "select * from tasa where id_tasa='1'");
	while ($row2=mysqli_fetch_array($query2))
	{
		$id_tasa=$row2['id_tasa'];
		$tasa_actual=$row2['tasa'];
		$ult_act=$row2['fecha'];
	}
	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");
	$row= mysqli_fetch_array($count_query);
	$numrows = $row['numrows'];
	$total_pages = ceil($numrows/$per_page);
	$reload = '../nueva_proforma.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);
	//loop through fetched data
	?>
	<div class="table-responsive">
		<table class="table" style="font-size:11px;">
			<tr class="warning">
				<th>Productos <?php echo $sql; ?></th>
				<th>Presentacion</th>
				<th>Marca</th>
	            <th>Existencia</th>
       		    <th><span class="pull-right">Cant</span></th>
				<th><span class="pull-right">Precio Venta con IVA</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'];
                    $precio_venta=$row['precio_venta'];
					$precio_costo=$row['precio_producto']; 
					$dolar_a_bolivar=$precio_venta*$tasa_actual;
					$dolar_a_bolivarf=number_format($dolar_a_bolivar,1,",", ".");					
					?>
					<tr>
						<td><?php echo $nombre_producto; ?></td>
						<td><?php echo $presen_producto; ?></td>
						<td><?php echo $marca_producto; ?></td>
						<td><?php echo $existencia_producto; ?></td>
						<td class='col-xs-1'>
							<input type="text" class="form-control" style="text-align:right" id="cantidad_<?php echo $id_producto; ?>"  value="1" required />
    	            	</td>
						<td class='col-xs-1'>$<?php echo $precio_venta ." / ".$dolar_a_bolivarf."BsD"; ?>
						<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>
	                    <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</td>
			</tr>
            <?php 
			} 
			?>
            	<tr>
				
					<td colspan=5>
                    	<span class="pull-right">
						<?php
					 		echo paginate($reload, $page, $total_pages, $adjacents);
						?>
                        </span>
                    </td>
				</tr>
			</table>
<?php 
} 
?>