<?php
include('../../../configuracion.php');
include('config.php');
?>
<?php 


$pagina = isset($_GET['p']) ? $_GET['p'] : NULL;
$av = isset($_GET['av']) ? $_GET['av'] : NULL;
$id = isset($_GET['id']) ? $_GET['id'] : NULL;
$act = isset($_GET['act']) ? $_GET['act'] : NULL;
$id_orden = isset($_GET['id_orden']) ? $_GET['id_orden'] : NULL;

$ordenar = isset($_GET['ordenar']) ? $_GET['ordenar'] : NULL;

//=====================================//
if ($act=='eliminar'){

	$db->delete($tabla,"id='{$id}'");
}


if($ordenar){
	$orden = $ordenar;
}else{
	$orden = "orden desc";
}


//========================
// FILTRAR POR AREA
$ar = isset($_GET['area']) ? $_GET['area'] : NULL;
if (empty($ar) && $av!='1'){
  $_SESSION['area'] = '';
  $filtro_area = '';
}
if (!empty($ar) && $av!='1'){
  $_SESSION['area'] = $ar;
  $filtro_area .= "AND area=".$_SESSION['area']."";
}
if (empty($ar) && $av=='1'){
  if ($_SESSION['area']!=''){
    $filtro_area .= "AND area=".$_SESSION['area']."";
  } else {
    $filtro_area = '';
  }
}
//========================

//========================
// FILTRAR POR CATEGORIA
$filtro_cat = "";
$cat = isset($_GET['categoria']) ? $_GET['categoria'] : NULL;
if (empty($cat) && $av!='1'){
  $_SESSION['cat'] = '';
  $filtro_cat = '';
}
if (!empty($cat) && $av!='1'){
  $_SESSION['cat'] = $cat;
  $filtro_cat .= " AND FIND_IN_SET('$cat', categoria)  ";
}
if (empty($cat) && $av=='1'){
  if ($_SESSION['cat']!=''){
    $filtro_cat .= " AND FIND_IN_SET('$cat', categoria) ";
  } else {
    $filtro_cat = '';
  }
}
//========================



if (empty($pagina) && $av!='1'){
$_SESSION['pagina'] = '';
}

if (!empty($pagina) && $av!='1'){
$_SESSION['pagina'] = $pagina;
}

if (!empty($pagina) && $av=='1'){
$_SESSION['pagina'] = $pagina;
}

//=====================================//

$busqueda = isset($_POST['busqueda']) ? $_POST['busqueda'] : NULL;
if ($busqueda=='' && $av!='1'){
$_SESSION['busqueda'] = '';
$filtro_busqueda = '';
}

if ($busqueda!='' && $av!='1'){
$_SESSION['busqueda'] = $busqueda;
$filtro_busqueda = "AND (busqueda LIKE '%".$_SESSION['busqueda']."%' or codigo LIKE '%".$_SESSION['busqueda']."%') ";
}

if ($busqueda=='' && $av=='1'){

if ($_SESSION['busqueda']!=''){
$filtro_busqueda = "AND (busqueda LIKE '%".$_SESSION['busqueda']."%' or codigo LIKE '%".$_SESSION['busqueda']."%' )";
} else {
$filtro_busqueda = '';
}
}


$total_reg = '100';
if(empty($pagina)){ $pagina = '1'; } 

$limite = (($pagina-1)*$total_reg).','.$total_reg;

$resultados = $db->results(TABLA,'*',"id!='0' ".$filtro_busqueda."  $filtro_area $filtro_cat",$orden,$limite);

$todos = $db->results(TABLA,'id',"id!='0' ".$filtro_busqueda."  $filtro_area $filtro_cat",'');

$tr = (isset($todos)) ? count($todos) : 0;
$tp = ceil($tr / $total_reg);

?>
<!doctype html>
<html class="fixed">
	<head>
		<?php include("../../inc/head.php")?>
		<!-- ESPACIO HEAD ADICIONALES -->

		<!-- FIN ESPACIO HEAD ADICIONALES -->
	</head>
	<body>
		<section class="body">
			<!-- start: header -->
			<?php include("../../inc/header.php")?>
			<!-- end: header -->
			<div class="inner-wrapper">
				<!-- start: sidebar -->
				<?php include("../../inc/side-left.php")?>
				<!-- end: sidebar -->
				
                    

<!--

				<div class="row">
		

			
							<div class="col">
							<?php if(isset($_GET["cargados"])){?>
							<div class="alert alert-warning" role="alert">
				Fueron cargados <?=$_SESSION["conteototal"]?> Stock, <?=$_SESSION["conteoantiguos"]?> fueron actualizados y <?=$_SESSION["conteonuevos"] ?> fueron ingresados exitosamente.
				</div>
				<?php }?>
							<form action="<?php echo MAGO?>c/<?php echo MODULO?>/carga-excel-file.php" method="POST" enctype="multipart/form-data" id="carga">
								<h6 class="text-left"></h6>
								
								<div class="form-row">
										
										<div class="form-group col-lg-6">
											<label><strong>Adjuntar Listado <a href="<?php echo MAGO?>pages/<?php echo MODULO?>/carga_producto_ejemplo.xlsx">ver archivo de ejemplo</a></strong></label>
											<input type="file" name="fileload" value="" class="mb-md" required data-msg-required="requerido."/>
										</div>
									</div>
								
									<div class="form-row">
										<div class="form-group col-lg-6">
										<input type="submit" name="" value="carga masiva de productos (.xlsx)" class="form-control"/>
										</div>
									</div>
							</form>
							</div>
							</div>
							-->





							<div class="row">
							<div class="col-sm-4">
							
									<form action="<?php echo MAGO?>c/<?php echo MODULO?>" method="POST" enctype="multipart/form-data" id="carga">
										<h6 class="text-left"></h6>
										
											<div class="form-row">
												
												<div class="form-group col-lg-6">
													<input type="text" name="busqueda" value="<?php echo $busqueda?>" class="mb-md form-control" placeholder="" required data-msg-required="requerido."/>
												</div>
										
												<div class="form-group col-lg-6">
												<input type="submit" name="" value="BUSCAR" class="form-control"/>
												</div>
											</div>
									</form>
							</div>
							<div class="col-sm-4">
							
									<form action="<?php echo MAGO?>c/<?php echo MODULO?>" method="GET" enctype="multipart/form-data" id="carga">
										<h6 class="text-left"></h6>
										
											<div class="form-row">
												<input type="hidden" name="av" value="0"/>
												<input type="hidden" name="p" value="<?php echo $pagina?>"/>
												<input type="hidden" name="ordenar" value="<?php echo $orden?>"/>
												
												<div class="form-group col">
													<select name="categoria" class="form-control" id="categorias" placeholder="categorias">
														<?php $categorias = $db->results("categorias","id,nombre","estado=1");?>
														<option value="">categoria</option>
														<?php foreach($categorias as $categoria):?>
														<option value="<?php echo $categoria->id?>" <?php if($cat==$categoria->id){ echo "selected";}?>><?php echo $categoria->nombre?></option>
														<?php endforeach;?>
													</select>




													
												</div>
											
												
												
												
												
										
												<div class="form-group col">
												<input type="submit" name="" value="FILTRAR" class="form-control"/>
												</div>
											</div>
									</form>
							</div>

							<div class="col-sm-4">
							
									<form action="<?php echo MAGO?>c/<?php echo MODULO?>" method="GET" enctype="multipart/form-data" id="carga">
										<h6 class="text-left"></h6>
										
											<div class="form-row">
												<input type="hidden" name="av" value="0"/>
												<input type="hidden" name="p" value="<?php echo $pagina?>"/>
												<input type="hidden" name="categoria" value="<?php echo $cat?>"/>
												
												<div class="form-group col">
													<select name="ordenar" class="form-control" id="ordenar" placeholder="ordenar">
														<option value="orden desc"></option>
														<option value="stock desc" <?php if($ordenar=="stock desc"){ echo "selected";}?>>Mayor Stock</option>
														<option value="stock asc" <?php if($ordenar=="stock asc"){ echo "selected";}?>>Menor Stock</option>
													</select>
												</div>
											
												
												
												
												
										
												<div class="form-group col">
												<input type="submit" name="" value="ORDENAR" class="form-control"/>
												</div>
											</div>
									</form>
							</div>

							
				</div>



				<div class="row pt-0">
							
							<div class="col text-right">
								<a href="<?php echo MODULO?>/agregar" class="btn btn-primary"><i class="fas fa-plus mr-3"></i>Agregar</a>
								<!--<a href="<?php echo MODULO?>/descargar.php" class="btn btn-primary"><i class="fas fa-plus mr-3"></i>Descargar Excel (.CSV)</a>-->
							</div>
				</div>


					<!-- start: page -->
					<div class="row">
							<div class="col">
								<section class="card">
									<div class="card-body">
										<div class="table-responsive">
										<table class="table table-bordered table-striped mb-none" id="datatable-default-no">
											<thead>
												<tr>
													<th class="center">Estado</th>
													<th class="center">Orden</th>
													<th class="center">Posi.</th>
													<th>SKU</th>
													<th>Nombre</th>
													<th>Precio</th>
													<th>Precio Oferta</th>
													<th>Categoria</th>
													<th>Marca</th>
													<th>Stock</th>
													<th class="center">Acciones</th>
												</tr>
											</thead>
											<form action="<?php echo MODULO?>/ordenar.php" method="POST">
											<tbody>
												<input type="hidden" name="av" value="0"/>
												<input type="hidden" name="p" value="<?php echo $pagina?>"/>
												<input type="hidden" name="categoria" value="<?php echo $cat?>"/>
												<?php if(isset($resultados)){?>
												<?php foreach($resultados as $row){?>
												<tr class="gradeX ">
													<td class="center"><?php include("../../inc/estado.php")?></td>
													<td class="center">
													<?php include("../../inc/orden.php")?>
													</td>
													<td class="center"><input type="number" name="orden[<?php echo $row->id?>]" value="<?=$row->orden?>" class="form-control" style="width:90px"/>
													</td>
													<td><?php echo $row->codigo ?></td>
													<td><?php echo $row->nombre ?></td>
													<td class="text-right">$<?php echo funcion::formatoPrecio($row->precio) ?></td>
													<td class="text-right">$<?php echo funcion::formatoPrecio($row->precio_oferta) ?></td>
													<td><?php echo $db->getvar("categorias","nombre","id='{$row->categoria}'") ?></td>
													<td><?php echo $db->getvar("marcas","nombre","id='{$row->marca}'") ?></td>
													<td  class="text-right"><?php echo $row->stock ?></td>
													<td class="center"><?php include("../../inc/action.php")?></td>
												</tr>
												<?php }}?>
												
											</tbody>
											<tfooter>
												<tr>
													<td></td>
													<td><input type="submit" name="" class="btn btn-primary" value="ORDENAR"/></td>
													<td></td>
													<td></td>
													<td></td>
													<td></td>
													<td></td>
													<td></td>
													<td></td>
													<td></td>
													<td></td>
												</tr>
											</tfooter>
												</form>
										</table>
										</div>
									</div>


									
                            		<?php include("../../inc/paginador.php");?>


								</section>
							</div>
						</div>







		
					

		<!-- NO TOCAR -->
		<?php include("../../inc/side-right.php")?>
		<?php include("../../inc/bottom.php")?>
		<!-- NO TOCAR -->

		
		<!-- ESPACIO JS ADICIONALES -->
		<script type="text/javascript">
function td_on(t) {
 	t.className = "menu_int_on"
}
function td_out(t) {
	t.className = "menu_int_of"
}
function td_go(t) {
 	this.document.location = t
}

function td_on_menu(t) {
 	t.className = "menu_ppal_int_on"
}

function td_out_menu(t) {
	t.className = "menu_ppal_int_of"
}

function td_go_menu(t) {
 	this.document.location = t
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
   
            $("#rutsocio").validate();
</script>
	

    </body>
</html>