<?php
/**
 * Recuperar contraseña — Nexora V6 Custom (layout auth glass).
 */
if (!defined('access') || !access) {
	die();
}

if (isLoggedIn()) {
	redirect();
}

$h = function ($s) {
	return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8');
};

try {
	if (!mconfig('active')) {
		throw new Exception(lang('error_47', true));
	}

	if (isset($_GET['ui']) && isset($_GET['ue']) && isset($_GET['key'])) {
		try {
			$realm = NexoraRealm::requireQuery('realm');
			$Account = new Account($realm);
			$Account->passwordRecoveryVerificationProcess($_GET['ui'], $_GET['ue'], $_GET['key']);
		} catch (Exception $ex) {
			message('error', $ex->getMessage());
		}
	} else {
		if (isset($_POST['webengineEmail_submit'])) {
			try {
				$realm = NexoraRealm::requirePosted('nexora_realm');
				$Account = new Account($realm);
				$Account->passwordRecoveryProcess($_POST['webengineEmail_current'], $_SERVER['REMOTE_ADDR']);
			} catch (Exception $ex) {
				message('error', $ex->getMessage());
			}
		}

		$t_title = lang('module_titles_txt_15', true);
		if ($t_title === 'ERROR' || $t_title === '') {
			$t_title = nexora_lang('nexora_auth_forgot_title', 'Recuperar contraseña');
		}
		$t_sub = nexora_lang('nexora_auth_forgot_sub', 'Te enviaremos un enlace al correo asociado a tu cuenta.');
		$t_login_cta = nexora_lang('nexora_auth_back_login', '¿Recordaste tu contraseña?');
		$t_login_link = lang('menu_txt_4', true);
		if ($t_login_link === 'ERROR' || $t_login_link === '') {
			$t_login_link = nexora_lang('nexora_auth_login_link', 'Iniciar sesión');
		}
?>
<div class="nex-auth nex-auth--forgot">
	<div class="nex-auth-card nex-glass-panel">
		<header class="nex-auth-head">
			<p class="nex-auth-eyebrow"><?php echo $h(nexora_lang('nexora_auth_forgot_eyebrow', 'Recuperación')); ?></p>
			<h1 class="nex-auth-title"><?php echo $h($t_title); ?></h1>
			<p class="nex-auth-sub"><?php echo $h($t_sub); ?></p>
		</header>

		<form class="nex-auth-form" action="" method="post">
			<?php nexoraRealmAuthField(); ?>
			<div class="nex-auth-field">
				<label class="nex-auth-label" for="webengineEmail"><?php echo lang('forgotpass_txt_1', true); ?></label>
				<input type="text" class="nex-auth-input" id="webengineEmail" name="webengineEmail_current" required autocomplete="email" maxlength="190">
			</div>

			<div class="nex-auth-actions">
				<button type="submit" name="webengineEmail_submit" value="submit" class="nex-btn nex-btn--gold nex-btn--block nex-auth-submit"><?php echo lang('forgotpass_txt_2', true); ?></button>
			</div>
		</form>

		<p class="nex-auth-switch">
			<?php echo $h($t_login_cta); ?>
			<a href="<?php echo $h(rtrim(__BASE_URL__, '/') . '/login/'); ?>"><?php echo $h($t_login_link); ?></a>
		</p>
	</div>
</div>
<?php
	}
} catch (Exception $ex) {
	message('error', $ex->getMessage());
}
