<?php
	session_start();
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Movimientos</title>
	<link rel="stylesheet" type="text/css" href="css/bootstrap/css/bootstrap.min.css">
    <script src="css/bootstrap/js/bootstrap.bundle.min.js"></script>
    <link rel="icon" href="../images/icon.png">
    <script type="text/javascript">
    </script>
</head>
<body>
	<?php
		require 'nav.php';
		require 'conexion.php';
		$user = $_SESSION["user"];
		$query = "SELECT * FROM recurrentes WHERE User='$user'";
		if ((isset($_POST["fecIni"]) and isset($_POST["fecFin"])) and ($_POST["fecIni"]!="-1" and $_POST["fecFin"]!="-1")  and ($_POST["fecIni"]!="" and $_POST["fecFin"]!="")){
			$fecIni = $_POST["fecIni"];
			$fecFin = $_POST["fecFin"];
			$query .= " AND Fecini<='" . $fecFin . "' AND FecFin>='" . $fecFin . "'";
		}
		else{
			$fecIni = "-1";
			$fecFin = "-1";
		}
		if (isset($_POST["fondo"]) and $_POST["fondo"]!= "-1" and $_POST["fondo"]!= ""){
			$fondo = $_POST["fondo"];
			$query .= " AND Fondo='" . $fondo . "'";
		}
		else{
			$fondo = "-1";
		}
		if (isset($_POST["tipo"]) and $_POST["tipo"]!= "-1" and $_POST["tipo"]!= ""){
			$tipo = $_POST["tipo"];
			$query .= " AND Tipo='" . $tipo . "'";
		}
		else{
			$tipo = "-1";
		}
		$recurrentes = mysqli_query($con, $query);
	?>
	<table class="table table-striped" id="tabla">
		<thead>
			<tr>
				<th>Nombre</th>
				<th>Cantidad</th>
				<th>Periodos</th>
				<th>
			<?php
			    echo '
					<form action="rec.php" method="POST">
						<input type="hidden" name="consec" value="-1">
						<input type="hidden" name="fecIni" value="', $fecIni,'">
				        <input type="hidden" name="fecFin" value="', $fecFin,'">
				        <input type="hidden" name="fondo" value="', $fondo,'">
				        <input type="hidden" name="tipo" value="', $tipo,'">
						<button class="btn btn-primary">Nuevo</button>
					</form>
				';
			?>
				</th>
			</tr>
		</thead>
		<tbody>
<?php
	$i = 0;
	while ($r = mysqli_fetch_assoc($recurrentes)){
		$fon = $r["Fondo"];
		$tip = $r["Tipo"];
		$fondo = mysqli_fetch_assoc(mysqli_query($con, "SELECT Nombre FROM fondos WHERE Consec='$fon'"))["Nombre"];
		$tipo = mysqli_fetch_assoc(mysqli_query($con, "SELECT Nombre FROM tipos WHERE Consec='$tip'"))["Nombre"];
		echo '
			<form action="rec.php" method="POST" id="modi', $i,'" style="display: none;">
				<input type="hidden" name="consec" value="', $r["Consec"],'">
				<input type="hidden" name="fecIni" value="', $fecIni,'">
				<input type="hidden" name="fecFin" value="', $fecFin,'">
				<input type="hidden" name="fondo" value="', $fondo,'">
				<input type="hidden" name="tipo" value="', $tipo,'">
			</form>
			<form action="borraRec.php" method="POST" id="borra', $i,'" style="display: none;">
				<input type="hidden" name="consec" value="', $r["Consec"],'">
				<input type="hidden" name="fecIni" value="', $fecIni,'">
				<input type="hidden" name="fecFin" value="', $fecFin,'">
				<input type="hidden" name="fondo" value="', $fondo,'">
				<input type="hidden" name="tipo" value="', $tipo,'">
			</form>
			<tr class="align-middle">
				<!--<td>', $fondo,'</td>-->
				<td>', $r["Nombre"],'</td>
				<td>', $r["Cant"],'</td>
				<td>', $r["FecIni"],'-', $r["FecFin"],'</td>
				<!--<td>', $tipo,'</td>
				<td>', $r["Meses"],'</td>-->
				<td>
				    <button type="button" class="btn btn-primary" onclick="document.getElementById(\'modi', $i,'\').submit()">Modificar</button><br>
				    <button type="button" class="btn btn-primary" onclick="document.getElementById(\'borra', $i,'\').submit()" style="margin-top: 1%;">Borrar</button>
				</td>
			</tr>
		';
		$i++;
	}
?>
		</tbody>
	</table>
</body>
</html>


