<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
	$q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
	include '../include/pagination.php';
	$page= (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
	$per_page= 15; //how much records you want to show
	$adjacents= 4; //gap between pages after number of adjacents
	$offset= ($page - 1) * $per_page;

	$count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM marca where empresa='$empresa' and mar_nombre like '%$q%'");
	$row= mysqli_fetch_array($count_query);
	$numrows = $row['numrows'];
	$total_pages = ceil($numrows/$per_page);
	$reload = '../I_marca.php';
	$sql=mysqli_query($con,"SELECT * FROM marca where empresa='$empresa' and mar_nombre like '%$q%' order by mar_nombre asc LIMIT $offset,$per_page");
	if ($numrows>0)
	{
?>
<div class="table-responsive">
	<table class="table table-bordered table-hover table-striped">
		<thead>
			<tr>
				<th width="1%">LOGO</th>
				<th width="">MARCAS</th>
				<th width="15%"></th>
			</tr>
		</thead>
		<tbody>
		<?php
			while ($reg=mysqli_fetch_array($sql)){
			$id=$reg['mar_id'];
        ?>
            <input type="hidden" value="<?php echo $reg['mar_nombre'];?>" id="mar_nombre<?php echo $reg['mar_id'];?>">
			<tr>
				<td>  
	                <?php
	                    $files = glob("../img/marca/$id.jpg");
	                    if(count($files) > 0) 
	                    { 
	                        foreach($files as $file) 
	                        { 
	                    ?>
	                        <?php $nu=rand(1,1000); $nu; ?>
	                        <a href="img/marca\<?php echo $id.'.jpg?' .$nu .''; ?>" class="ui-lightbox">
	                        <center><div class="contenedor"> <img  class="marca" src="img/marca\<?php echo $id.'.jpg?' .$nu .''; ?>" alt=""/></div></center>
	                        </a>
	                   
	                    <?php } } else { ?>
	                        <a href="img/marca/sin_imagen.jpg" class="ui-lightbox">
	                       	<center><div class="contenedor"><img class="marca" src="img/marca/sin_imagen.jpg" alt=""/></div></center>
	                        </a>
	                    <?php } ?>
				</td>
				<td style="vertical-align: middle;"><?php  echo $reg['mar_nombre'];?></td>
				<td style="vertical-align: middle;">
				<?php
					if ($reg['mar_nombre']<>'SIN MARCA')
					{
				?>
	            		<a href="#" class='btn btn-xs btn-info' title='Editar' onclick="obtener_datos('<?php echo $reg['mar_id'];?>');" data-toggle="modal" data-target="#modificar_marca"><i class="btn-icon-only icon-edit"></i></a> 
	            		           					
	   					<a href="#" onclick="eliminar('<?php echo $reg['mar_id'];?>')" title="Eliminar" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
				<?php } ?>
				</td>
			</tr>
		<?php  } ?>
				<tr>
					<td colspan=3>
						<span class="pull-right">
							<?php echo paginate($reload, $page, $total_pages, $adjacents);?>
						</span>
					</td>
				</tr>
			</tbody>
	</table>
</div>		
<?php
		}
	}
?> 
<script>
function eliminar (mar_id)
{
    if (confirm("Realmente deseas eliminar la Marca")){ 
    $.ajax({
        type: "GET",
        url: "php_marca/eliminar_marca.php",
      	data: "mar_id="+mar_id,
     	beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
      	},
        success: function(datos){
    	$("#resultados").html(datos);
    	load(1);
    	}
      	});
    }
} 
function obtener_datos(id){
      var mar_nombre = $("#mar_nombre"+id).val();
      $("#id").val(id);
      $("#nombre").val(mar_nombre);
    }
</script>

<style type="text/css">
/*Supongamos que nuestro contenedor mide 150px x 150px*/
.contenedor{
  	background-color: white;
  	width:60px;
  	height:30px;
}
/*Aplicamos la propiedad object-fit cover, ajustar su tamaño y no perder la proporcion de nuestra imagen*/
.marca{
  	object-fit: scale-down;
  	width:100%;
  	height:100%;
}
</style>