<?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>Recuperação de Senha | <?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">
		<script src="<?php echo show_file_link("/assets/jquery/jquery.min.js"); ?>"></script>
		<script src="<?php echo show_file_link("/assets/jquery-mask/jquery.mask.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%;
			}

			.alert-white{
				color: #212529;
				background-color: #fff;
				border-color: #ced4da;
			}
		</style>
	</head>
	<body style="background-image: linear-gradient(<?php echo GRADIENT_COLOR_1; ?>, <?php echo GRADIENT_COLOR_2; ?>);">
		<div class="container my-5">
			<div class="row justify-content-center">
				<div class="col-xl-5 col-lg-6 col-md-7 col-sm-12">
					<div class="card o-hidden border-0 shadow-lg">
						<div class="card-body p-0">
							<div class="p-3 p-md-5">
								<div class="h4 text-center">
									<?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>Recuperação de Senha</small>
								</div>

								<div class="alert alert-white text-center my-3">
									<strong>Aviso:</strong><br>
									O e-mail contendo a nova senha de acesso pode demorar entre 5 a 10 minutos dependendo do seu provedor.<br>
									O e-mail também pode parar na sua caixa de spam ou lixo eletrônico.
								</div>

								<form id="form-recuperar-senha">
									<div id="msg"></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-address-card"></i>
												</span>
											</div>
											<input type="text" name="cpf_usuario" id="cpf-usuario" class="form-control" placeholder="CPF/CNPJ" autocomplete="off" required>
										</div>
									</div>
									<button type="submit" class="btn btn-primary btn-lg btn-block">
										<i class="fa-regular fa-sync"></i> Redefinir Senha
									</button>
								</form>
							</div>
						</div>
					</div>

					<div class="text-center mt-4">
						<a href="<?php echo show_link("/login/"); ?>" class="text-white">Valtar para login</a>
					</div>
				</div>
			</div>
		</div>

		<script src="<?php echo show_file_link("/assets/jquery-loading-overlay/loading-overlay.js"); ?>"></script>
		<script>
			$("#cpf-usuario").mask("000.000.000-00", {
				onKeyPress : function(cpfcnpj, e, field, options) {
					const masks = ["000.000.000-000", "00.000.000/0000-00"];
					const mask = (cpfcnpj.length > 14) ? masks[1] : masks[0];
					$("#cpf-usuario").mask(mask, options);
				}
			});

			$("#form-recuperar-senha").submit(function(event){
				event.preventDefault();
				var form = $(this);

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

					if (resposta.status == true){
						var html = '<div class="alert alert-success alert-dismissible fade show" role="alert">'+
							'Senha Redefinida com Sucesso!<br>Foi enviado para o seu e-mail a sua nova senha de acesso.'+
							'<button type="button" class="close" data-dismiss="alert" aria-label="Close">'+
								'<span aria-hidden="true">&times;</span>'+
							'</button>'+
						'</div>';

						$("#msg").append(html);

						$.each(form, function(){
							this.reset();
						})
					}
					else{
						var html = '<div class="alert alert-danger alert-dismissible fade show" role="alert">'+
							'E-mail ou CPF Inválido!'+
							'<button type="button" class="close" data-dismiss="alert" aria-label="Close">'+
								'<span aria-hidden="true">&times;</span>'+
							'</button>'+
						'</div>';

						$("#msg").append(html);
					}
				},function(error){
					console.log("Erro: "+error);
				});
			});

			$(document).ajaxStart(function(){
				$.LoadingOverlay("show", {
					fontawesome : "",
					image: "<?php echo show_file_link("/assets/img/loading.svg"); ?>",
				});
			});

			$(document).ajaxStop(function(){
				$.LoadingOverlay("hide");
			});

			$(".loadAjax").on("click", function(){
				$.LoadingOverlay("show", {
					fontawesome : "",
					image: "<?php echo show_file_link("/assets/img/loading.svg"); ?>"
				});
			});

			$("body").on("click", ".loadAjax", function(){
				$.LoadingOverlay("show", {
					fontawesome : "",
					image: "<?php echo show_file_link("/assets/img/loading.svg"); ?>"
				});
			});
		</script>
	</body>
</html>