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

$ano = $_GET['ano'];				
$soma_view	= 0;	
$soma_click	= 0;		
		
//paginação
include_once("pag_ini.php");
$ajax = 1;
$total = 1;
//fim paginação

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

$qry = "SELECT * FROM bnr_ban_imagens WHERE descricao like '%".$digito."%' ORDER BY id ASC";
		
$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: Ano <? echo $_GET['ano'] ?></p>
<table align="center" class="exibe">

<tr>
		<th rowspan="2">Código(ID)</th>
		<th rowspan="2">Descrição</th>
        <th colspan="3">Janeiro</th>
        <th colspan="3">Fevereiro</th>
        <th colspan="3">Março</th>
        <th colspan="3">Abril</th>
        <th colspan="3">Maio</th>
        <th colspan="3">Junho</th>
        <th colspan="3">Julho</th>
        <th colspan="3">Agosto</th>
        <th colspan="3">Setembro</th>
        <th colspan="3">Outubro</th>
        <th colspan="3">Novembro</th>
        <th colspan="3">Dezembro</th>		
        <th colspan="3">Total</th>        
</tr>

<tr>
		<th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
        <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
         <th>Views</th>
		<th>Clicks</th>
        <th>%</th>
</tr>


<?php
while ($result = mysqli_fetch_array($consulta)) {
?>
  <tr>
    	<td>
      		<?php echo $result['id']; ?>
    	</td>
    	<td>
      		<?php echo $result['descricao']; ?>
    	</td>
        <?php 
		for ($i=1;$i<=12;$i++) {
		
		$qry2 = "SELECT COUNT(data_view) as r,(SELECT COUNT(data_click) FROM rel_banner_clicks as y 		
		WHERE MONTH(data_click)=$i AND YEAR(data_click)=$ano AND z.id_banner = y.id_banner ) as t  
		FROM rel_banner_views as z  
		WHERE MONTH(data_view)=$i AND YEAR(data_view)=$ano AND id_banner=".$result['id']." GROUP BY id_banner";
						
		$con = mysqli_query($conn, $qry2);
		
		$linha = mysqli_fetch_array($con);		
		?>   
    	<td>        
        	<?php echo $linha['r']; 
			$soma_view += $linha['r'];
			?>      		
     	</td>    
        <td>
      		<?php echo $linha['t']; 
			$soma_click += $linha['t']; 
			?>
     	</td>    
        <td>
      		<?php if($linha['r']!=0){ echo round(($linha['t']/$linha['r'])*100,2); } ?>%
     	</td> 
        <?php } ?>
        <td>
        	<?php echo $soma_view; ?>
        </td>
        <td>
        	<?php echo $soma_click; ?>
        </td>
        <td>
        	<?php if($soma_view!=0){ echo round(($soma_click/$soma_view)*100,2); } ?>%
        </td>
 </tr>
<?php
$soma_view = 0;
$soma_click = 0;
}
mysqli_close($conn);
?>
</table>