<?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'];
	
	if ( $_POST['q']!= "")
	{
		$sql="select * from marcas where marca LIKE '%".$q."%'";
		$count_query = mysqli_query($con, "SELECT count(*) AS numrows from marcas where marca LIKE '%".$q."%'");
	}
		else if( $_POST['q'] == "")
	{
		$sql="select * from marcas";
		$count_query = mysqli_query($con, "SELECT count(*) AS numrows from marcas");	
	}
		
	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 = './marcas.php';
	//main query to fetch the data
	$sql.=" 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>Nombres</th>
					<th>Agregado</th>
					<th><span class="pull-right">Acciones</span></th>
				</tr>
				<?php
				if ($numrows>0)
				{
				while ($row=mysqli_fetch_array($query))
				{
					$id=$row['id_marca'];
					$nombre_marca=$row['marca'];
					$date_added= date('d/m/Y', strtotime($row['date_added']));
					?>
					<input type="hidden" value="<?php echo $row['marca'];?>" id="nombre_marca<?php echo $id;?>">
					
					<tr>
						<td><?php echo $id; ?></td>
						<td><?php echo $nombre_marca; ?></td>
						<td><?php echo $date_added;?></td>
						<td ><span class="pull-right">
							<a href="#" class='btn btn-default' title='Editar marca' onclick="obtener_datos('<?php echo $id;?>')" data-toggle="modal" data-target="#marcaModal2"><i class="glyphicon glyphicon-edit"></i></a> 
						<a href="#" class='btn btn-default' title='Borrar marca' onclick="eliminar('<?php echo $id; ?>')"><i class="glyphicon glyphicon-trash"></i> </a></span>
						</td>
						
					</tr>
				<?php
				}
				?>
				<tr>
               			<td colspan=7><span class="pull-right"><?php echo paginate($reload, $page, $total_pages, $adjacents);?></span></td>
				</tr>
			</table>
		</div>
		<?php
	}
	
}
?>