<?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 = 12; //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 tarifas  
		where empresa='$empresa' and tar_nombre like '%$q%'");
	$row= mysqli_fetch_array($count_query);
	$numrows = $row['numrows'];
	$total_pages = ceil($numrows/$per_page);
	$reload = '../I_tarifas.php';
	//main query to fetch the data
	$sql = mysqli_query($con,"SELECT * FROM tarifas 
		where empresa='$empresa' and tar_nombre like '%$q%'  
		order by tar_id asc LIMIT $offset,$per_page");
		
if ($numrows>0){
?>
<div class="table-responsive">
	<table class="table table-bordered table-hover table-striped">
		<thead>
			<tr>
			<?php	if ($tienda_online=='si'){ ?>
				<th width="5%">TIENDA</th>
			<?php } ?>
				<th>NOMBRE</th>
				<th width="15%"></th>
			</tr>
		</thead>
		<tbody >
		<?php
			while ($reg=mysqli_fetch_array($sql)){
        ?>
        	<input type="hidden" value="<?php echo $reg['tar_nombre'];?>" id="tar_nombre<?php echo $reg['tar_id'];?>">
			<tr>
				<?php  $id=$reg['tar_id'];

				if ($tienda_online=='si'){ ?>
						<td><?php 
							
							if ($reg['tar_destacado']=='si') { ?>
								<center>
							 		<a href="#" onclick="estado('<?php echo $id;?>')"> <img src="img/tarifas/destacado.png" alt="" style="width: 40%; height: 40%; " /></a>
							 	</center>

							<?php } else { ?>
								<center>
									<a href="#" onclick="estado('<?php echo $id;?>')"> <img src="img/tarifas/no-destacado.png" alt="" title="selecione tarifa para pagina online" style="width: 40%; height: 40%; " /></a>
								</center>

						<?php } ?>
						</td>
					<?php } ?>
						<td><?php  echo $reg['tar_nombre']; ?></td>
						<td>
						<?php
							if ($reg['tar_nombre']=='GENERAL')
								{
		  					 	echo'';
							 	}
							 	else
							 	{ 
						?>
							<a href="#" class='btn btn-xs btn-info' title='Editar' onclick="obtener_datos('<?php echo $reg['tar_id'];?>');" data-toggle="modal" data-target="#modificar_tarifa"><i class="btn-icon-only icon-edit"></i></a> 
		   					<a href="#" onclick="eliminar('<?php echo $reg['tar_id'];?>')"  title="Eliminar" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
						<?php } ?>
						</td>
			</tr>
		<?php }?>
			<tr>	<?php	if ($tienda_online=='si'){ $col=4;} else { $col=2;} ?>
				<td colspan=<?php echo $col; ?>><span class="pull-right">
					<?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
				</td>
			</tr>
		</tbody>
	</table>
</div>		
<?php
		}
	}
?>
<script>
function eliminar (tar_id)
{
    if (confirm("Realmente deseas eliminar la tarifa")){ 
    $.ajax({
        type: "GET",
        url: "php_tarifa/eliminar_tarifa.php",
      	data: "tar_id="+tar_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 tar_nombre = $("#tar_nombre"+id).val();
	$("#id").val(id);
	$("#nombre").val(tar_nombre);
}


function estado(id) 
{ 
    $.ajax({
        type: "POST",
        url: "php_tarifa/cambiar_estado.php",
        data: "tar_id="+id,
        success: function(datos){
        $("#resultados").html(datos);
        load(1);
    }
    });
} 

function promo(id) 
{ 
    $.ajax({
        type: "POST",
        url: "php_tarifa/cambiar_estado_promo.php",
        data: "tar_id="+id,
        success: function(datos){
        $("#resultados").html(datos);
        load(1);
    }
    });
} 
</script>