<?php 

	if (Session::isRegistered("id_usuario")) {
		echo "<script>window.location.href='".show_link("/dashboard/")."'</script>";
	}
?>

<!DOCTYPE html>
<html lang="pt-br">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

		<link href="<?php echo show_file_link(SYS_ICONE); ?>" rel="icon" type="image/png">
		<title>Login | <?php echo SYS_NAME; ?></title>

		<link href="<?php echo show_file_link("/assets/bootstrap/css/bootstrap.min.css"); ?>" rel="stylesheet">
		<link href="<?php echo show_file_link("/assets/fontawesome/releases/v6.2.0/css/all.css"); ?>" rel="stylesheet" type="text/css">
		<link href="<?php echo show_file_link("/assets/fontawesome/releases/v6.2.0/css/sharp-solid.css"); ?>" rel="stylesheet" type="text/css">
		<link href="<?php echo show_file_link("/assets/css/sb-admin.css"); ?>" rel="stylesheet">
		<link href="<?php echo show_file_link("/assets/custom/css/style.css"); ?>" rel="stylesheet">
		<link href="<?php echo show_file_link("/assets/custom/css/custom.css"); ?>" rel="stylesheet">
		<script src="<?php echo show_file_link("/assets/jquery/jquery.min.js"); ?>"></script>
		<script src="<?php echo show_file_link("/assets/bootstrap/js/bootstrap.bundle.min.js"); ?>"></script>
		<?php import_file(DIR_TEMPLATES."style.php"); ?>

		<style>
			html, body{
				min-height: 100%;
			}

			.background-login {
				background-size: cover;
				background-position: center;
				background-image: url("<?php echo show_file_link(BACKGROUND_IMAGE_LOGIN); ?>");
				background-repeat: no-repeat;
			}
		</style>
	</head>
	<body style="background-image: linear-gradient(<?php echo GRADIENT_COLOR_1; ?>, <?php echo GRADIENT_COLOR_2; ?>);">
		<div class="container">
			<div class="row justify-content-center my-5">
				<div class="col-xl-10 col-lg-10 col-md-9 col-sm-12">
					<div class="card o-hidden border-0 shadow-lg">
						<div class="card-body p-0">
							<div class="form-row">
								<div class="col-lg-6 d-none d-lg-block background-login" style="border-top-left-radius: 5px; border-bottom-left-radius: 5px;"></div>
								<div class="col-lg-6">
									<div class="p-3 p-md-5">
										<div class="h4 text-center mb-3 mb-md-5">
											<?php if (PRESENTATION_STANDARD == 1): ?>
												<a href="<?php echo show_link("/"); ?>">
													<img src="<?php echo show_file_link(SYS_SECONDARY_LOGO); ?>" alt="logo" style="max-width:250px; max-height:100px; width: auto; height: auto;">
												</a>
												<br>
												<?php echo SYS_NAME; ?>
											<?php elseif (PRESENTATION_STANDARD == 2): ?>
												<a href="<?php echo show_link("/"); ?>">
													<img src="<?php echo show_file_link(SYS_SECONDARY_LOGO); ?>" alt="logo" style="max-width:250px; max-height:100px; width: auto; height: auto;">
												</a>
											<?php else: ?>
												<a href="<?php echo show_link("/"); ?>">
													<?php echo SYS_NAME; ?>
												</a>
											<?php endif; ?>
											<br>
											<small>Acesso ao Painel</small>
										</div>
										<form id="form-login">
											<div id="msg-erro"></div>
											<div class="form-group">
												<div class="input-group input-group-lg">
													<div class="input-group-prepend">
														<span class="input-group-text">
															<i class="fa-regular fa-at"></i>
														</span>
													</div>
													<input type="email" name="email_usuario" id="email-usuario" class="form-control" placeholder="E-mail" autocomplete="off" maxlength="255" autofocus="autofocus" required>
												</div>
											</div>
											<div class="form-group">
												<div class="input-group input-group-lg">
													<div class="input-group-prepend">
														<span class="input-group-text">
															<i class="fa-regular fa-key"></i>
														</span>
													</div>
													<input type="password" name="senha_usuario" id="senha-usuario" class="form-control" placeholder="Senha" autocomplete="off" maxlength="60" required>
												</div>
											</div>
											<button type="submit" class="btn btn-primary btn-lg btn-block">
												<i class="fa-regular fa-sign-in"></i> Entrar
											</button>

											<div class="text-center mt-3">
												<a href="<?php echo show_link("/esqueci-minha-senha/"); ?>">Esqueci minha senha</a>
											</div>

											<div class="text-center mt-3">
												Não possui uma conta? 
												<a href="<?php echo show_link("/cadastro-usuario/"); ?>">
													Cadastre-se
												</a>
											</div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>

					<div class="text-center mt-4">
						<a href="<?php echo show_link("/"); ?>" class="text-white">
							Voltar para Início
						</a>
					</div>
				</div>
			</div>
		</div>

		<script>
			$("#form-login").submit(function(event){
				event.preventDefault();
				var form = $(this);

				$.ajax({
					type: "POST",
					data: form.serialize(),
					url: "<?php echo show_link("/logar/"); ?>",
					async: false
				}).then(function(response){
					var resposta = $.parseJSON(response);

					if (resposta.status == true){
						location.reload(true);
					}
					else{
						var html = '<div class="alert alert-danger alert-dismissible fade show" role="alert">'+
							'E-mail ou Senha Inválido!'+
							'<button type="button" class="close" data-dismiss="alert" aria-label="Close">'+
								'<span aria-hidden="true">&times;</span>'+
							'</button>'+
						'</div>';

						$("#msg-erro").html(html);
					}
				},function(error){
					console.log("Erro: "+error);
				});
			});
		</script>
	</body>
</html>