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

if (isLoggedIn()) {
	header('Location: ' . __BASE_URL__ . 'usercp/myaccount/');
	exit;
}

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

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

	if (isset($_POST['webengineLogin_submit'])) {
		try {
			$realm = NexoraRealm::requirePosted('nexora_realm');
			$userLogin = new login($realm);
			$userLogin->validateLogin($_POST['webengineLogin_user'], $_POST['webengineLogin_pwd']);
		} catch (Exception $ex) {
			message('error', $ex->getMessage());
		}
	}

	$t_title = lang('module_titles_txt_2', true);
	if ($t_title === 'ERROR' || $t_title === '') {
		$t_title = nexora_lang('nexora_auth_login_title', 'Iniciar sesión');
	}
	$t_sub = nexora_lang('nexora_auth_login_sub', 'Accede a tu panel de jugador.');
	$t_reg_cta = nexora_lang('nexora_auth_no_account', '¿No tienes cuenta?');
	$t_reg_link = lang('menu_txt_3', true);
	if ($t_reg_link === 'ERROR' || $t_reg_link === '') {
		$t_reg_link = nexora_lang('nexora_auth_register_link', 'Registrarse');
	}
?>
<div class="nex-auth nex-auth--login">
	<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_eyebrow_back', 'Bienvenido de nuevo')); ?></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="webengineLogin1"><?php echo lang('login_txt_1', true); ?></label>
				<input type="text" class="nex-auth-input" id="webengineLogin1" name="webengineLogin_user" required autocomplete="username">
			</div>
			<div class="nex-auth-field">
				<label class="nex-auth-label" for="webengineLogin2"><?php echo lang('login_txt_2', true); ?></label>
				<input type="password" class="nex-auth-input" id="webengineLogin2" name="webengineLogin_pwd" required autocomplete="current-password">
				<p class="nex-auth-help nex-auth-help--link"><a href="<?php echo $h(rtrim(__BASE_URL__, '/') . '/forgotpassword/'); ?>"><?php echo lang('login_txt_4', true); ?></a></p>
			</div>

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

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