<? 
include_once("valida_sessao.php");
protegePagina();
?>

<?
include("config.php");
include("utf8.php");

//paginação
include_once("pag_ini.php");
$ajax = 1;
$total = 0;
//fim paginação

if($_GET['mes']!="" && $_GET['ano']!=""){
$mes = $_GET['mes'];
$ano = $_GET['ano'];

//busca ajax
if(isset($_GET['digito'])){
	$digito = $_GET['digito'];
}else{
	$digito = "";
}
//fim busca ajax

	$qry = "SELECT *, COUNT(data_view) as r,(SELECT COUNT(data_click) FROM rel_banner_clicks as y 
		INNER JOIN bnr_ban_imagens AS b ON id_banner = b.id 
		WHERE MONTH(data_click)=$mes AND YEAR(data_click)=$ano AND z.id_banner = y.id_banner ) as t  
		FROM rel_banner_views as z INNER JOIN bnr_ban_imagens AS b ON id_banner = b.id 
		WHERE MONTH(data_view)=$mes AND YEAR(data_view)=$ano AND b.descricao like '%".$digito."%' GROUP BY id_banner ";

		$consulta = mysqli_query($conn, $qry.$sqlFinal);			

//navegador de paginação
include_once("pag_fim.php");
//fim navegador de paginação

?>


<p align="center">Relatório de Banners: <? echo $_GET['mes']." / ".$_GET['ano'] ?></p>
<table align="center" class="exibe">

<tr>
		<th>Código(ID)</th>
		<th>Descrição</th>
        <th>Views</th>
        <th>Clicks</th>		
        <th>Porcentagem<br />Clicks por Exibição</th>        
</tr>


<?php

while ($result = mysqli_fetch_array($consulta)) {
?>
  <tr>
    	<td>
      		<?php echo $result['id_banner']; ?>
    	</td>
    	<td>
      		<?php echo $result['descricao']; ?>
    	</td>   
    	<td>
        	<?php echo $result['r']; ?>      		
     	</td>    
        <td>
      		<?php echo $result['t']; ?>
     	</td>    
        <td>
      		<?php if($result['r']!=0){ echo round(($result['t']/$result['r'])*100,2); } ?>%
     	</td>    
  </tr>
<?php
}
}
mysqli_close($conn);
?>
</table>