<?php
    @session_start();
    if (!isset($_SESSION["admin"]["id"])) {
        echo "<script>location.href='index.php';</script>";
    }
?>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="js/https://apis.google.com/js/platform.js"></script>
<h1 class="page-header">
    
    	<div class="row">
    		<div class="col-md-7 col-sm-6">
    			Relatórios de Envios
            </div>

    	</div>
    
</h1>

<div class="container" style="width: 90%">

<form name="form1" method="post" class="form-inline text-center">
	<label for="mes">Selecione o Mês:
	<select name="mes" required class="form-control">
		<option value=""></option>
		<option value="todos">Todos os Meses</option>
		<option value="01">01</option>
		<option value="02">02</option>
		<option value="03">03</option>
		<option value="04">04</option>
		<option value="05">05</option>
		<option value="06">06</option>
		<option value="07">07</option>
		<option value="08">08</option>
		<option value="09">09</option>
		<option value="10">10</option>
		<option value="11">11</option>
		<option value="12">12</option>
	</select></label>

	<label for="ano">Selecione o Ano:
	<select name="ano" required class="form-control">
		<option value=""></option>
		<?php
			$ano = date("Y");

			$i = 2017;
			while ( $i <= $ano ) {

				echo "<option value='$i'>$i</option>";
				$i++;

			}

		?>
	</select></label>

	<button type="submit" class="btn btn-success">Buscar</button>

</form>
<br>
<table class="table table-bordered table-hover table-striped">
	<thead>
		<tr>	
			<td>Data</td>
			<td>Número de Envios</td>
			<td>Opções</td>
		</tr>
	</thead>
	<?php


		if ( ( isset ( $_POST["mes"] ) ) or ( isset ( $param[2] ) ) ) {

			$mes = date("m");
			$ano = date("Y");


			if ( isset ( $_POST["mes"] ) ) $mes = $_POST["mes"];
			if ( isset ( $param[2] ) ) $mes = $param[2];
			if ( isset ( $_POST["ano"] ) ) $ano = $_POST["ano"];
			if ( isset ( $param[3] ) ) $ano = $param[3];

			$data = "$ano-$mes";

			$d = $data."%";

			$sql = "select count(id) c from envio where data like '".$ano."-".$mes."%' limit 1";
			$consulta = $db->prepare($sql);
			$consulta->execute();

			while ( $dados = $consulta->fetch(PDO::FETCH_OBJ)) {

				echo "<tr>
					<td>$mes/$ano</td><td>$dados->c</td><td>
					<a href='cadastro/relEnvios/$data' class='btn btn-success'>
						<i class='glyphicon glyphicon-search'></i>
					</a>
					<a href='cadastro/naoenviados/$data' class='btn btn-info'>
						<i class='glyphicon glyphicon-folder-open'></i>
					</a>
					</td></tr>";

			}

		}

	?>
</table>

</div>