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



//========================
// FILTRAR POR CATEGORIA
$region = isset($_GET['region']) ? $_GET['region'] : NULL;
if (empty($region) && $av!='1'){
  $_SESSION['region'] = '';
  $filtro_region = '';
}
if (!empty($region) && $av!='1'){
  $_SESSION['region'] = $region;
  $filtro_region .= "AND id_region=".$_SESSION['region']."";
}
if (empty($region) && $av=='1'){
  if ($_SESSION['region']!=''){
    $filtro_region .= "AND id_region=".$_SESSION['region']."";
  } else {
    $filtro_region = '';
  }
}
//========================

$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 nombre LIKE '%".$_SESSION['busqueda']."%'";
}

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

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


?>
<?php $resultados = $db->results(TABLA,"*"," id!=0 ".$filtro_busqueda."   $filtro_region","orden desc");?>
<!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"]?> Valores, <?=$_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/<?=MODULO?>/demo-carga-despachos.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 asistentes (.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="region" class="form-control" id="regiones" placeholder="regiones">
												<?php $regiones = $db->results("regiones","id,nombre","estado=1");?>
												<option value="">regiones</option>
												<?php foreach($regiones as $row):?>
												<option value="<?php echo $row->id?>" <?php if($region==$row->id){ echo "selected";}?>><?php echo $row->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 text-right pt-2">
								<a href="<?php echo MODULO?>/descargar.php" target="_blank" class="btn btn-primary"><i class="fas fa-download mr-3"></i>Descargar .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>Región</th>
													<th>Código</th>
													<th>Comuna</th>
													<th>ZONA</th>
													<th><?php echo $db->getVar("rangos","desde","id=1");?> a <?php echo $db->getVar("rangos","hasta","id=1");?> </th>
													<th><?php echo $db->getVar("rangos","desde","id=2");?> a <?php echo $db->getVar("rangos","hasta","id=2");?> </th>
													<th><?php echo $db->getVar("rangos","desde","id=3");?> a <?php echo $db->getVar("rangos","hasta","id=3");?> </th>
													<th><?php echo $db->getVar("rangos","desde","id=4");?> a <?php echo $db->getVar("rangos","hasta","id=4");?> </th>
													<th><?php echo $db->getVar("rangos","desde","id=5");?> a <?php echo $db->getVar("rangos","hasta","id=5");?> </th>
													<th class="center">Acciones</th>
												</tr>
											</thead>
											<tbody>
												<?php foreach($resultados as $row){?>
												<tr class="gradeX ">
													<td><?php echo $db->getVar("regiones","nombre","id='{$row->id_region}'") ?></td>
													<td><?php echo $row->codigo ?></td>
													<td><?php echo $row->nombre ?></td>
													<td><?php echo $row->zona ?></td>
													<td><?php echo $row->rango_1 ?></td>
													<td><?php echo $row->rango_2 ?></td>
													<td><?php echo $row->rango_3 ?></td>
													<td><?php echo $row->rango_4 ?></td>
													<td><?php echo $row->rango_5 ?></td>
													<td class="center"><?php include("../../inc/action.php")?></td>
												</tr>
												<?php }?>
												
											</tbody>
										</table>
												</div>
									</div>
								</section>
							</div>
						</div>







		
					

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

		
		<!-- ESPACIO JS ADICIONALES -->

	

    </body>
</html>