<?php
	session_start();
	$consec = $_POST["consec"];
	$fondoR = $_POST["fondo"];
	$fecIni = $_POST["fecIni"];
	$fecFin = $_POST["fecFin"];
	$tipoR = $_POST["tipo"];
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Movimiento</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">
    <style type="text/css">
		form{
			width: 80%;
			margin: 10% auto;
		}

		.form-floating{
			margin: 2% 0;
		}
	</style>
	<script type="text/javascript">
	    function chec(){
	        var tipo = document.getElementById("cargo").value;
	        var fondo = document.getElementById("fondo").value;
	        var cred = document.getElementById("f" + fondo).dataset.tipo;
	        var cat = document.getElementById("tipo");
	        var catCont = document.getElementById("tipoCont");
	        var meses = document.getElementById("meses");
	        var mesesCont = document.getElementById("mesesCont");
	        cat.required = false;
            //var tipos = document.getElementsByClassName("tipos");
            //for (var x = 0; x <= tipos.length; x++){
            //    tipos[x].selected = false;
            //}
            //document.getElementById("tipoTarg").selected = true;
            //cat.value = ""
            catCont.style.display = "none";
            //meses.value = "";
            meses.min = "";
            meses.required = false;
            mesesCont.style.display = "none";
	        if (tipo != ""){
	            if (tipo == "Abono"){
	                cat.required = false;
	                //var tipos = document.getElementsByClassName("tipos");
	                //for (var x = 0; x <= tipos.length; x++){
	                //    tipos[x].selected = false;
	                //}
	                //document.getElementById("tipoTarg").selected = true;
	                cat.value = "";
	                catCont.style.display = "none";
	                meses.value = "";
	                meses.min = "";
	                meses.required = false;
	                mesesCont.style.display = "none";
	            }
	            else{
	                cat.required = true;
	                catCont.style.display = "block";
	                if (cred == "1"){
	                    meses.min = "1";
	                    meses.required = true;
	                    mesesCont.style.display = "block";
	                }
	            }
	        }
	    }
	</script>
</head>
<body onload="chec()">
	<?php
		require 'nav.php';
		require 'conexion.php';
		$user = $_SESSION["user"];
		if ($consec == "-1"){
			$fondo = "";
			$cant = "";
			$fecha = date("Y-m-d");
			$meses = "1";
			$nom = "";
			$tipo = "";
		}
		else{
			$actual = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM movimientos WHERE Consec='$consec'"));
			$fondo = $actual["Fondo"];
			$cant = $actual["Cant"];
			$fecha = $actual["Fecha"];
			$meses = $actual["Meses"];
			$nom = $actual["Nombre"];
			$tipo = $actual["Tipo"];
		}
		$cierre = mysqli_fetch_assoc(mysqli_query($con, "SELECT Mes, Year FROM saldos WHERE User='$user' ORDER BY Year DESC, Mes DESC"));
		if ($cierre["Mes"] == "12"){
    		$mes = "1";
    		$year = $cierre["Year"] + 1;
		}
		else{
		    $mes = $cierre["Mes"] + 1;
		    $year = $cierre["Year"];
		}
		if ($mes < 10){
		    $mes = "0" . $mes;
		}
		echo '
	<form action="guardMov.php" method="POST">
		<input type="hidden" name="consec" value="', $consec,'">
		<input type="hidden" name="user" value="', $user,'">
		<input type="hidden" name="fecIni" value="', $fecIni,'">
		<input type="hidden" name="fecFin" value="', $fecFin,'">
		<input type="hidden" name="fondoR" value="', $fondoR,'">
		<input type="hidden" name="tipoR" value="', $tipoR,'">
		<div class="form-floating">
			<input type="text" name="nombre" id="nombre" required class="form-control" value="', $nom,'">
			<label for="nombre">Nombre</label>
		</div>
		<div class="form-floating">
			<select class="form-select" id="cargo" name="cargo" required onchange="chec()">
				<option></option>
		';
		if ($cant == ""){
		    echo '
				<option value="Cargo">Cargo</option>
				<option value="Abono">Abono</option>
			';
		}
		else{
		    if ($cant < 0){
		        echo '
				<option value="Cargo" selected>Cargo</option>
				<option value="Abono">Abono</option>
			    ';
		    }
		    else{
    		    echo '
    			<option value="Cargo">Cargo</option>
    			<option value="Abono" selected>Abono</option>
    			';
		    }
		}
		echo '
			</select>
			<label for="cargo">Cargo/Abono</label>
		</div>
		<div class="form-floating">
			<select class="form-select" id="fondo" name="fondo" required onchange="chec()">
				<option id="f" data-tipo="0"></option>
		';
		$fondos = mysqli_query($con, "SELECT * FROM fondos WHERE User='$user'");
		while ($f = mysqli_fetch_assoc($fondos)){
			if ($f["Consec"] == $fondo){
				echo '
				<option value="', $f["Consec"],'" selected id="f', $f["Consec"],'" data-tipo="', $f["Credito"],'">', $f["Nombre"],'</option>
				';
			}
			else{
				echo '
				<option value="', $f["Consec"],'" id="f', $f["Consec"],'" data-tipo="', $f["Credito"],'">', $f["Nombre"],'</option>
				';
			}
		}
		echo '
			</select>
			<label for="fondo">Fondo</label>
		</div>
		<div class="form-floating">
			<input type="number" name="cant" id="cant" required class="form-control" step=".01" value="', $cant,'" min="0">
			<label for="cant">Cantidad</label>
		</div>
		<div class="form-floating">
			<input type="date" name="fecha" id="fecha" required class="form-control" value="', $fecha,'" min="', $year,'-', $mes,'-01">
			<label for="fecha">Fecha</label>
		</div>
		<div class="form-floating" id="mesesCont">
			<input type="number" name="meses" id="meses" required class="form-control" value="', $meses,'" min="0">
			<label for="meses">Meses</label>
		</div>
		<div class="form-floating" id="tipoCont">
			<select class="form-select" id="tipo" name="tipo" required>
				<option id="tipoTarg"></option>
		';
		$tipos = mysqli_query($con, "SELECT * FROM tipos WHERE User='$user'");
		while ($t = mysqli_fetch_assoc($tipos)){
			if ($t["Consec"] == $tipo){
				echo '
				<option value="', $t["Consec"],'" selected class="tipos">', $t["Nombre"],'</option>
				';
			}
			else{
				echo '
				<option value="', $t["Consec"],'" class="tipos">', $t["Nombre"],'</option>
				';
			}
		}
		echo '
			</select>
			<label for="tipo">Tipo</label>
		</div>
		<button type="submit" class="btn btn-primary">Guardar</button>
	</form>
		';
	?>
</body>
</html>

