<?php
require_once ("../config/db.php");
require_once ("../config/conexion.php");
include('is_logged.php');
	
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax')
{
	// escaping, additionally removing everything that could be (html/javascript-) code
    $q = $_POST['q'];
	$est = $_POST['lugar'];
	
	
	if (!empty($q) && empty($est)) {
    $sql = "SELECT * FROM tiendas WHERE nombre LIKE '%$q%'";
    $count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM tiendas WHERE nombre LIKE '%$q%'");
} elseif (!empty($q) && !empty($est)) {
    $sql = "SELECT * FROM tiendas WHERE nombre LIKE '%$q%' AND estado='$est'";
    $count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM tiendas WHERE nombre LIKE '%$q%' AND estado='$est'");
} elseif (empty($q) && !empty($est)) {
    $sql = "SELECT * FROM tiendas WHERE estado='$est'";
    $count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM tiendas WHERE estado='$est'");
} else {
    // ESTA ES LA PARTE QUE CARGA TODO AL INICIO
    $sql = "SELECT * FROM tiendas";
    $count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM tiendas");
}

		
	include 'pagination.php'; //include pagination file
	//pagination variables
	$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
	$per_page = 10; //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*/
	$row= mysqli_fetch_array($count_query);
	$numrows = $row['numrows'];
	$total_pages = ceil($numrows/$per_page);
	$reload = './tiendas.php';
	//main query to fetch the data
	$sql.=" order by nombre ASC LIMIT $offset,$per_page";
	$query = mysqli_query($con, $sql) or die(mysqli_error($con));
	//loop through fetched data
	?>
		<div class="table-responsive">
			<table class="table">
				<tr  class="warning">
					<th>ID</th>
					<th>Nombre de la Tienda</th>
					<th>Direccion</th>
					<th>Estado</th>
					<th>Agregado</th>
					<th><span class="pull-right">Acciones</span></th>
				</tr>
				<?php
				$i=1;
				if ($numrows>0)
				{
				while ($row=mysqli_fetch_array($query))
				{
					$id=$row['id_tienda'];
					$nombre_tienda=$row['nombre'];
					$estado_tienda=$row['estado'];
					$date_added= date('d/m/Y', strtotime($row['date_added']));
					$direccion_tienda= $row['direccion'];
					
					?>
					<input type="hidden" value="<?php echo $row['nombre'];?>" id="nombre_tienda<?php echo $id;?>">
					<input type="hidden" value="<?php echo $row['direccion'];?>" id="direccion_tienda<?php echo $id;?>">
					<input type="hidden" value="<?php echo $row['estado'];?>" id="est_tienda<?php echo $id;?>">
					
					<tr>
						<td><?php echo $i; ?></td>
						<td><?php echo $nombre_tienda; ?></td>
						<td><?php echo $direccion_tienda; ?></td>
						<td><?php echo $estado_tienda; ?></td>
						<td><?php echo $date_added;?></td>
						<td ><span class="pull-right">
							<a href="#" class='btn btn-default' title='Editar tienda' onclick="obtener_datos('<?php echo $id;?>')" data-toggle="modal" data-target="#tiendaModal2"><i class="glyphicon glyphicon-edit"></i></a> 
						<a href="#" class='btn btn-default' title='Borrar tienda' onclick="eliminar('<?php echo $id; ?>')"><i class="glyphicon glyphicon-trash"></i> </a></span>
						</td>
						
					</tr>
				<?php
				$i=$i+1;
				}
				?>
				<tr>
               			<td colspan=7><span class="pull-right"><?php echo paginate($reload, $page, $total_pages, $adjacents);?></span></td>
				</tr>
			</table>
		</div>
		<?php
	}
	
}
?>