<?php
include ("conexao.php");

$campo = $_POST["campo"];
$turma = $_POST["valor"];
$ex_faltas = $_POST["ex_faltas"];

$data_inicial = $_POST["data_inicial"];
$d=explode("/",$data_inicial);
$data_inicial=$d[2]."/".$d[1]."/".$d[0];


$data_final = $_POST["data_final"];
$d=explode("/",$data_final);
$data_final=$d[2]."/".$d[1]."/".$d[0];

$ano = $d[2];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--

@media print
{
    #printbtn { display: none; }
	#header { display:block;	}
}
@media screen
{
    printbtn { display: block; }
	#header { display:none; }
}
-->
</style>
<link href="style2.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="header">
		<div id="header_logo"><img src="images/logo_escola.jpg" width="193px" /></div><!-- Fecha Header_logo -->
		<div id="header_logo2"><img src="images/logo_diario.jpg" width="205" height="79" /></div><!-- Fecha Header_logo -->
	<h2 align="center">Relatório de frequência da Turma <?php echo $turma;?> / Período</h2>
</div><!-- Fecha Header -->
<table width="100%" border="1" cellpadding="4" cellspacing="0">
	<tr>
		<td colspan="4" valign="top" bgcolor="#CCCCCC"><br />		  Período: <?php echo $_POST["data_inicial"];?> à <?php echo $_POST["data_final"];?></td>
	</tr>
	<?php	
		$sql = "select * from tab_horarios where turma = '$turma' and ano = $ano";
		$res = mysql_query($sql);
		$row = mysql_fetch_array($res);
		
		// select distinct dia from tab_freq where turma = 60 and (dia between '2012-11-10' and '2012-11-29') and (dayofweek(dia) = 2 or dayofweek(dia)=4) 		
		$sql_semana = "select distinct dia from tab_freq where turma = '$turma' and (dia BETWEEN '$data_inicial' and '$data_final') order by dia";
		// Use o echo $sql_semana; para teste da query
		// echo $sql_semana;
		// Colsultando os dias do trimestre - aulas dadas.
		//$sql = "select distinct dia from tab_freq";
		$res = mysql_query($sql_semana);
		
		?>
	
	<tr>
		<td width="3%" valign="top" bgcolor="#CCCCCC">nº</td>
		<td valign="top" bgcolor="#CCCCCC">Aluno</td>
		<?php if ($ex_faltas != "S"){ ?>
        <td width="62%" bgcolor="#CCCCCC">Dias<br />
		<table border="0" cellspacing="0" cellpadding="0">
				<tr>
				<?php while ($row = mysql_fetch_array($res)){ ?>
					<td align="center" width="25" class="diario"><?php echo date('d/m', strtotime($row["dia"]));?></td>
				<?php } ?>	
				</tr>
		</table>
		</td>
        <?php } ?>
		<td width="5%" valign="top" bgcolor="#CCCCCC">Faltas</td>
	</tr>
	<?php
		$sql = "select matricula, nome from tab_alunos where turma = '$turma' and ano=$ano order by nome";
		$res = mysql_query($sql);
		$cont = 1;
		
		while($row = mysql_fetch_array($res)){
		$matriculas[$cont] = $row["matricula"];
	?>	
	<tr>
		<td class="diario"><?php echo $cont;?></td>
		<td width="30%" class="diario"><?php echo $row["nome"];?></td>
		<?php if ($ex_faltas != "S"){ ?>
        <td class="diario">
		<!-- CONSULTANDO A FREQUENCIA DO ALUNO -->
			<table border="0" cellspacing="0" cellpadding="0">
				<tr>
				<?php
				// Buscando os dias novamente e verificando se o aluno veio
				$sql2 = $sql_semana;
				$res2 = mysql_query($sql2);
				$faltas = 0;
				while($row2 = mysql_fetch_array($res2)){
					
					$dia = $row2["dia"];
					$sql3 = "select * from tab_freq where dia = '$dia' and matricula = $matriculas[$cont]";
					$res3 = mysql_query($sql3) or die("Erro na query: ".mysql_error());
					if (mysql_num_rows($res3) == 0 ){
						$freq = "F";
						$faltas++;
					}else{
						$freq = "&#8226;";
					}
				?>
					<td align="center" width="25" class="diario"><?php echo $freq;?></td>
				<?php next($row2); } ?>
				</tr>
			</table>

			<!-- FIM DA CONSULTA -->
		</td>
        <?php }else{
			
			// Buscando os dias novamente e verificando se o aluno veio
				$sql2 = $sql_semana;
				$res2 = mysql_query($sql2);
				$faltas = 0;
				while($row2 = mysql_fetch_array($res2)){
					
					$dia = $row2["dia"];
					$sql3 = "select * from tab_freq where dia = '$dia' and matricula = $matriculas[$cont]";
					$res3 = mysql_query($sql3) or die("Erro na query: ".mysql_error());
					if (mysql_num_rows($res3) == 0 ){
						$freq = "F";
						$faltas++;
					}
				}
		} ?>
		<td><?php echo $faltas;?></td>
	</tr>
	<?php $cont++; 
	next($row);} ?>
</table>
<br />
<input type="button" name="printbtn" id="printbtn" value="Imprimir" onClick="window.print()" />
</p>
</body>
</html>