<?php

	/*-------------------------
	Autor: Obed Alvarado
	Web: obedalvarado.pw
	Mail: info@obedalvarado.pw
	---------------------------*/
	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
	
	
	
			
			
	$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)));
	
		 $aColumns = array('codigo_producto', 'nombre_producto');//Columnas de busqueda
		 $sTable = "products";
		 $sWhere = "where id_producto not in (14,15,16,17,18)";

		
		if ( $_GET['q'] != "" )
		{
			$sWhere = "WHERE (  ";
			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			{
				$sWhere .= $aColumns[$i]." LIKE '%".$q."%' OR";
			}
			$sWhere = substr_replace( $sWhere, "", -3 );
			
			$sWhere .= ')';
			
		}


		
		
		
		$sWhere.="order by nombre_producto asc	";
		include 'pagination.php'; //include pagination file
		//pagination variables
		$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
		$per_page = 5; //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){
			
			?>
			<div class="table-responsive">
			  <table class="table">
				<tr  class="warning">
					<th>Código</th>
					<th>Producto</th>
					<th><span class="pull-right">Piezas</span></th>
					<th><span class="pull-right">Costo Unitario</span></th>
					<th><span class="pull-right">Precio Mayoreos</span></th>
					<th><span class="pull-right">%</span></th>
					<th><span class="pull-right">Bonificacion</span></th>
					<th class='text-center' style="width: 36px;">Agregar</th>
				</tr>
				<?php
				while ($row=mysqli_fetch_array($query)){
					$id_producto=$row['id_producto'];
					$codigo_producto=$row['codigo_producto'];
					$nombre_producto=$row['nombre_producto'];
					$precio_venta_producto=$row["precio_producto"];
					
					//$precio_venta=number_format($precio_venta,2,'.','');
					//$costo_producto=$row["costo_producto"];
					//$costototal = ($precio_venta * $costo_producto); // Piezas
					?>
					<tr>
						<td><?php echo $codigo_producto; ?></td>
						<td><?php echo $nombre_producto; ?></td>
						<td class='col-xs-1'>
						<div class="pull-right">
						<input type="text" class="form-control" style="text-align:right" id="cantidad_<?php echo $id_producto; ?>"  value=" " >
						</div></td>
						
						<td class='col-xs-2'>
						<div class="pull-right">
						<input type="text" class="form-control" style="text-align:right" id="costocliente_<?php echo $id_producto; ?>"  value=" <?php echo $precio_venta_producto;?> " readonly >
						</div></td>	
								
						
						<td class='col-xs-2'>
						<div class="pull-right">
						<input type="text" class="form-control" style="text-align:right" id="precio_venta_producto_<?php echo $id_producto; ?>"  value="<?php echo $precio_venta_producto;?> " readonly >
						</div></td>
						
						<td class='col-xs-1'><div class="pull-right">
						<select class="form-control input-sm" id="precio_venta_<?php echo $id_producto; ?>" required>
						     <option value> </option>
											   <?php
									            $sql_porcentaje = mysqli_query($con, "select * from porcentaje where status_porcentaje = 1 ");
									            while ($rw = mysqli_fetch_array($sql_porcentaje)) {
										     
										        $porcentaje = $rw["porcentaje"];
									            ?>
										        <option value="<?php echo $porcentaje?>"><?php echo $porcentaje?></option>
									            <?php
									            }
									            ?>
												
											
							                    <!--<option value=""> % </option>
												<option value="	0	"	>	0	</option>
												<option value="	4	"	>	4	</option>
												<option value="	5	"	>	5	</option>
												<option value="	7	"	>	7	</option>
												<option value="	8	"	>	8	</option>
												<option value="	10	"	>	10	</option>
												<option value="	12	"	>	12	</option>
												<option value="	15	"	>	15	</option>
												<option value="	20	"	>	20	</option>
												<option value="	25	"	>	25	</option>
												<option value="	30	"	>	30	</option>-->>
												
												
							</select>
						</div></td>
						
						   <td class='col-xs-2'><div class="pull-right">
								
								<select class="form-control input-sm" id="id_producto_bonifica_<?php echo $id_producto; ?>" required>
							                    <option value> </option>
											   <?php
									            $sql_productos = mysqli_query($con, "select * from products ");
									            while ($rw = mysqli_fetch_array($sql_productos)) {
										        $id_producto_bon = $rw["id_producto"];
										        $nombre_producto_bon = $rw["nombre_producto"];
									            ?>
										        <option value="<?php echo $id_producto_bon ?>"><?php echo $nombre_producto_bon?></option>
									            <?php
									            }
									            ?>
												
												</select>
						
				<!--		<?php
						if ($id_producto == 1) {
							?>
								<td class='col-xs-2'><div class="pull-right">
								
								<select class="form-control input-sm" id="id_producto_bonifica_<?php echo $id_producto; ?>" required>
							                    <option value="">  </option>
												<option value="1"	>TAB 750 ml</option>
												<option value="2"	>TAR 750 ml</option>
												<option value="3"	>TAA 750 ml</option>
												<option value="4"	>TAC 750 ml</option>
												<option value="5"	>Brindis 1LT</option>
												<option value="<?php echo $id_producto?>"><?php echo $nombre_producto?>
												
												</select>
							<?php
						} 
						
						elseif ($id_producto == 2) {							
									?>
									
									<td class='col-xs-2'><div class="pull-right">
								
								<select class="form-control input-sm" id="id_producto_bonifica_<?php echo $id_producto; ?>" required>
							                   <option value="">  </option>
												<option value="1"	>TAB 750 ml</option>
												<option value="2"	>TAR 750 ml</option>
												<option value="3"	>TAA 750 ml</option>
												<option value="4"	>TAC 750 ml</option>
												<option value="5"	>Brindis 1LT</option>
												</select>
									
						<?php
						} 
						
						elseif ($id_producto == 3) {							
									?>
									
									<td class='col-xs-2'><div class="pull-right">
								
								<select class="form-control input-sm" id="id_producto_bonifica_<?php echo $id_producto; ?>" required>
							                  <option value="">  </option>
												<option value="1"	>TAB 750 ml</option>
												<option value="2"	>TAR 750 ml</option>
												<option value="3"	>TAA 750 ml</option>
												<option value="4"	>TAC 750 ml</option>
												<option value="5"	>Brindis 1LT</option>
											
												</select>
												
					     <?php
						} 
						
						elseif ($id_producto == 4) {							
									?>
									
									<td class='col-xs-2'><div class="pull-right">
								
								<select class="form-control input-sm" id="id_producto_bonifica_<?php echo $id_producto; ?>" required>
							                  <option value="">  </option>
												<option value="1"	>TAB 750 ml</option>
												<option value="2"	>TAR 750 ml</option>
												<option value="3"	>TAA 750 ml</option>
												<option value="4"	>TAC 750 ml</option>
												<option value="5"	>Brindis 1LT</option>
											
												</select>
						
						<?php
						}
						
						elseif ($id_producto == 5) {							
									?>
									
									<td class='col-xs-2'><div class="pull-right">
								
								<select class="form-control input-sm" id="id_producto_bonifica_<?php echo $id_producto; ?>" required>
							           <option value="">  </option>
												<option value="1"	>TAB 750 ml</option>
												<option value="2"	>TAR 750 ml</option>
												<option value="3"	>TAA 750 ml</option>
												<option value="4"	>TAC 750 ml</option>
												<option value="5"	>Brindis 1LT</option>
												</select>
												
					     <?php
						} 
						
										
									?>-->

							</div>
						</td>
						
					
						<td class='text-center'><a class='btn btn-info'href="#" onclick="agregar('<?php echo $id_producto ?>')"><i class="glyphicon glyphicon-ok"></i></a></td>
					</tr>
					<?php
				}
				?>
				<tr>
					<td colspan=5><span class="pull-right">
					<?php
					 echo paginate($reload, $page, $total_pages, $adjacents);
					?></span></td>
				</tr>
			  </table>
			</div>
			<?php
		}
	}
?>