<?php

$message = "";

session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
	header('location: instalaciones/instalaciones.php');
}

if(isset($_GET["session_expired"])) 
{
	$message = "La sesion ha expirado por seguridad. Por favor entre de nuevo";
	session_unset();
	session_destroy();
}

?>

<!DOCTYPE html>
<html>
<head>
	<!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<!-- Bootstrap CSS -->
    <link type="text/css" rel="stylesheet" href="css/bootstrap.min.css" media="screen,projection">

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<script>
		$(document).ready(function(){
			$('#alerta').hide();
			$(document).on('submit', '#login', function(e){
				e.preventDefault();
				$.ajax({
					type: 'post',
					url: $(this).attr("action"),
					data: $(this).serialize(),
					dataType: 'json',
					success: function(data){
						document.getElementById('login').reset();
						if (data.error) {
							$('#alerta').text(data.message);
							$('#alerta').show();
							$('#alerta').alert();
						}
						else {
							window.location.href = data.redirect;
							//console.log(window.location.href + " " + data.redirect);
						}
					}
				});
			});
		});
	</script>
</head>

<body>
<div class="container">
	<?php if($message!="") 
	{ 	
	    ?>
	    <div class="alert alert-danger" style="text-align: center"><?php echo $message; ?></div>
	    <?php 
	   } 
	 ?>
	<div class="row justify-content-md-center">
		<aside class="col-sm-8 col-md-6 col-lg-6 col-xl-6">
			<div class="card">
				<article class="card-body">
					<h4 class="card-title text-center mb-4 mt-1">Iniciar sesión</h4>
					<hr>
					<div id="alerta" class="alert alert-danger" role="alert">

					</div>
					<form id="login" class="col s12" method="post" action="login.php">
						<div class="form-group">
							<label for="user">Usuario</label>
							<input type="text" class="form-control" id="user" name="user" placeholder="Introduce nombre de usuario" required>
						</div>
						<div class="form-group">
							<label for="password">Contraseña</label>
							<input type="password" class="form-control" id="password" name="pass" placeholder="Password" required>
						</div>
						<div class="form-group">
							<button type="submit" class="btn btn-primary btn-block" name="login">Login</button>	
						</div>
						<div class="form-group">
							<a href="registration.html">
								<button type="button" class="btn btn-outline-primary btn-block" name="register" name="signUp">Sign up</button>
							</a>
						</div>
					</form>
				</article>
			</div>
		</aside>
	</div>
</div>

	

	<!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <!--<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
</body>
</html>