<?php

/**
 * Nexora V6 Custom — WebEngine CMS template
 */
if (!defined('access') || !access) {
	die();
}

include __PATH_TEMPLATE_ROOT__ . 'inc/template.functions.php';

if (!isset($_REQUEST['page']) || $_REQUEST['page'] === '') {
	$_REQUEST['page'] = 'home';
}
if (!isset($_REQUEST['subpage'])) {
	$_REQUEST['subpage'] = '';
}

$page = $_REQUEST['page'];
$subpage = $_REQUEST['subpage'];

/*
 * Módulos de página (no home):
 * 1) templates/nexoraves6custom/inc/modules/{page}.php
 * 2) templates/nexoraves6custom/inc/modules/{page}/{subpage}.php
 * 3) usercp anidado: inc/modules/usercp/{subpage}/{request}.php
 * Si no hay override en el template → modules/ del core (Handler::loadModule).
 * Ver nexora_template_load_module() en inc/template.functions.php.
 */

if ($page === 'login' && function_exists('isLoggedIn') && isLoggedIn()) {
	header('Location: ' . __BASE_URL__ . 'usercp/myaccount/');
	exit;
}

if (
	$page === 'usercp' && function_exists('check_value') && check_value($subpage)
	&& function_exists('isLoggedIn') && !isLoggedIn()
) {
	redirect(1, 'login');
	if (!empty($GLOBALS['_captured_redirect'])) {
		$loc = $GLOBALS['_captured_redirect'];
		unset($GLOBALS['_captured_redirect']);
		header('Location: ' . __BASE_URL__ . $loc);
		exit;
	}
}

if ($page === 'logout') {
	if (function_exists('logOutUser')) {
		logOutUser();
	}
	header('Location: ' . __BASE_URL__);
	exit;
}

$nexIsHome = ($page === 'home');
$nexUsercpSidebar = ($page === 'usercp' && $subpage !== '');

if ($nexIsHome && is_file(__PATH_TEMPLATE_ROOT__ . 'inc/modules/countdown.php')) {
	require_once __PATH_TEMPLATE_ROOT__ . 'inc/modules/countdown.php';
}

$serverInfoCache = LoadCacheData('server_info.cache');
$srvInfo = (is_array($serverInfoCache) && isset($serverInfoCache[1][0])) ? explode('|', $serverInfoCache[1][0]) : array();
$nexOnline = isset($srvInfo[3]) ? (int) $srvInfo[3] : 0;
$maxOnline = function_exists('config') ? config('maximum_online', true) : 0;
$nexOnlinePct = (check_value($maxOnline) && (int) $maxOnline > 0) ? min(100, round($nexOnline * 100 / (int) $maxOnline)) : min(100, $nexOnline > 0 ? 80 : 0);

$nexLang = isset($_SESSION['language_display']) ? $_SESSION['language_display'] : (function_exists('config') ? config('language_default', true) : 'es');

/*
 * CTA comunitario del home. El enlace se administra desde la configuración
 * existente de WebEngine; sólo se imprime si pertenece a un dominio oficial
 * de WhatsApp para evitar enlaces rotos o inseguros.
 */
$nexWhatsappUrl = '';
if ($nexIsHome && function_exists('config')) {
	$nexWhatsappCandidate = trim((string) config('social_link_whatsapp', true));
	$nexWhatsappHost = strtolower((string) parse_url($nexWhatsappCandidate, PHP_URL_HOST));
	$nexWhatsappHosts = array('chat.whatsapp.com', 'wa.me', 'whatsapp.com', 'www.whatsapp.com');
	if (filter_var($nexWhatsappCandidate, FILTER_VALIDATE_URL) && in_array($nexWhatsappHost, $nexWhatsappHosts, true)) {
		$nexWhatsappUrl = $nexWhatsappCandidate;
	}
}
?>
<!DOCTYPE html>
<html lang="<?php echo htmlspecialchars($nexLang, ENT_QUOTES, 'UTF-8'); ?>">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
	<title><?php $handler->websiteTitle(); ?></title>
	<meta name="generator" content="WebEngine <?php echo __WEBENGINE_VERSION__; ?>">
	<meta name="description" content="<?php config('website_meta_description'); ?>">
	<meta name="keywords" content="<?php config('website_meta_keywords'); ?>">
	<meta property="og:type" content="website">
	<meta property="og:title" content="<?php $handler->websiteTitle(); ?>">
	<meta property="og:description" content="<?php config('website_meta_description'); ?>">
	<meta property="og:image" content="<?php echo __PATH_IMG__; ?>webengine.jpg">
	<meta property="og:url" content="<?php echo __BASE_URL__; ?>">
	<link rel="icon" href="<?php echo __PATH_TEMPLATE__; ?>favicon.ico">
	<link rel="preconnect" href="https://fonts.googleapis.com">
	<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
	<link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin>
	<link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@600;700&family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,600;0,9..40,700;1,9..40,400&display=swap" rel="stylesheet">
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css" crossorigin="anonymous">
	<link rel="stylesheet" href="<?php echo __PATH_TEMPLATE_CSS__; ?>nexora.css?v=55">
	<?php if ($page === 'vip') { ?><link rel="stylesheet" href="<?php echo __PATH_TEMPLATE_CSS__; ?>nexora-vip.css?v=1"><?php } ?>
	<?php if ($page === 'shop') { ?><link rel="stylesheet" href="<?php echo __PATH_TEMPLATE_CSS__; ?>itemshop.css?v=7"><?php } ?>
	<script>
		var baseUrl = '<?php echo rtrim(__BASE_URL__, '/'); ?>/';
	</script>
</head>

<body class="<?php echo $nexIsHome ? 'nex-body nex-body--home' : 'nex-body'; ?><?php echo (!$nexIsHome && ($page === 'usercp' || $page === 'donation')) ? ' nex-body--ucp' : ''; ?><?php echo $page === 'benefits' ? ' nex-body--benefits' : ''; ?><?php echo $page === 'shop' ? ' nex-body--shop' : ''; ?> nex-realm--<?php echo htmlspecialchars(NexoraRealm::active(), ENT_QUOTES, 'UTF-8'); ?>" data-nex-logged-in="<?php echo isLoggedIn() ? '1' : '0'; ?>" data-nex-realm="<?php echo htmlspecialchars(NexoraRealm::active(), ENT_QUOTES, 'UTF-8'); ?>" data-nex-mobile-build="responsive-20260811-v1" data-nex-guides-build="content-hub-20260812-v1" data-nex-speed-build="speed-v4-20260902">
	<a class="nex-skip" href="#nex-main"><?php echo htmlspecialchars(nexora_lang('nexora_skip_content', 'Saltar al contenido'), ENT_QUOTES, 'UTF-8'); ?></a>


	<header class="nex-topbar">
			<div class="nex-container nex-topbar-inner">
				<div class="nex-topbar-start">
					<?php nexoraLanguageSelector(); ?>
					<?php nexoraRealmSwitcher(); ?>
				<button type="button" class="nex-nav-toggle" aria-expanded="false" aria-controls="nex-nav-drawer">
					<span></span><span></span><span></span>
					<span class="nex-sr"><?php echo htmlspecialchars(nexora_lang('nexora_menu_open', 'Abrir menú'), ENT_QUOTES, 'UTF-8'); ?></span>
				</button>
			</div>
			<nav class="nex-topbar-nav" aria-label="<?php echo htmlspecialchars(nexora_lang('nexora_nav_aria', 'Navegación principal'), ENT_QUOTES, 'UTF-8'); ?>">
				<ul class="nex-nav-links" role="menubar">
					<?php nexoraNavbarItems(true, array('shop','benefits','donation')); ?>
				</ul>
			</nav>
			<div class="nex-topbar-account">
				<?php nexoraCommerceMenu(false); ?>
				<?php if (isLoggedIn()) { ?>
					<a class="nex-link" href="<?php echo __BASE_URL__; ?>usercp/"><?php echo lang('module_titles_txt_3'); ?></a>
					<a class="nex-btn nex-btn--ghost" href="<?php echo __BASE_URL__; ?>logout/"><?php echo lang('menu_txt_6'); ?></a>
				<?php } else { ?>
					<a class="nex-register-btn" href="<?php echo __BASE_URL__; ?>register/"><?php echo lang('menu_txt_3'); ?></a>
					<a class="nex-nav-img-btn nex-nav-img-btn--login" href="<?php echo __BASE_URL__; ?>login/"
						aria-label="<?php echo htmlspecialchars(lang('menu_txt_4'), ENT_QUOTES, 'UTF-8'); ?>">
						<img class="nex-nav-img-btn__media" src="<?php echo __PATH_TEMPLATE_IMG__; ?>login.webp" alt="" decoding="async">
						<span class="nex-nav-img-btn__label"><?php echo htmlspecialchars(lang('menu_txt_4'), ENT_QUOTES, 'UTF-8'); ?></span>
					</a>
				<?php } ?>
			</div>
		</div>
	</header>


	<?php if ($nexIsHome && function_exists('nexoraCountdownRender')) { nexoraCountdownRender('after-hero'); } ?>

	<div id="nex-nav-drawer" class="nex-nav-drawer" hidden aria-hidden="true" role="dialog" aria-modal="true" aria-label="<?php echo htmlspecialchars(nexora_lang('nexora_nav_aria', 'Navegación principal'), ENT_QUOTES, 'UTF-8'); ?>">
		<div class="nex-nav-drawer-inner">
			<ul class="nex-nav-drawer-list" role="menu">
				<?php nexoraCommerceMenu(true); ?>
				<?php nexoraNavbarItems(true, array('shop','benefits','donation')); ?>
				<?php if (isLoggedIn()) { ?>
					<li class="nex-nav-mobile-account" role="none"><a class="nex-nav-link" role="menuitem" href="<?php echo __BASE_URL__; ?>usercp/"><?php echo lang('module_titles_txt_3'); ?></a></li>
					<li class="nex-nav-mobile-account" role="none"><a class="nex-nav-link" role="menuitem" href="<?php echo __BASE_URL__; ?>logout/"><?php echo lang('menu_txt_6'); ?></a></li>
				<?php } else { ?>
					<li class="nex-nav-mobile-account" role="none"><a class="nex-nav-link" role="menuitem" href="<?php echo __BASE_URL__; ?>register/"><?php echo lang('menu_txt_3'); ?></a></li>
					<li class="nex-nav-mobile-account" role="none"><a class="nex-nav-link" role="menuitem" href="<?php echo __BASE_URL__; ?>login/"><?php echo lang('menu_txt_4'); ?></a></li>
				<?php } ?>
			</ul>
		</div>
	</div>

	<main id="nex-main" class="nex-main">
		<div class="nex-container">
			<?php if ($nexIsHome) { ?>
				<section class="nex-home-grid nex-reveal">
					<div class="nex-home-news">
						<div class="nex-panel-head">
							<h2><?php echo lang('news_txt_4'); ?></h2>
							<a class="nex-more" href="<?php echo __BASE_URL__; ?>news/"><?php echo lang('news_txt_5'); ?></a>
						</div>
						<div class="nex-panel-body nex-home-news-body">
							<?php include __PATH_TEMPLATE_ROOT__ . 'inc/home-news.php'; ?>
						</div>
					</div>
					<aside class="nex-home-side">
						<div class="nex-widget nex-widget--status">
							<h3><?php echo lang('sidebar_srvinfo_txt_5'); ?></h3>
							<div class="nex-widget-online">
								<span class="nex-big-num"><?php echo number_format($nexOnline); ?></span>
								<?php if (check_value($maxOnline)) { ?>
									<div class="nex-progress"><span style="width:<?php echo (int) $nexOnlinePct; ?>%"></span></div>
									<small><?php echo (int) $nexOnlinePct; ?>% <?php echo htmlspecialchars(nexora_lang('nexora_of_cap', 'del límite'), ENT_QUOTES, 'UTF-8'); ?></small>
								<?php } ?>
							</div>
							<div class="nex-times" data-server-epoch="<?php echo (int) time(); ?>" data-server-offset="<?php echo (int) date('Z'); ?>">
								<div><span><?php echo lang('server_time'); ?></span><time id="tServerTime">&nbsp;</time> <span id="tServerDate">&nbsp;</span></div>
								<div><span><?php echo lang('user_time'); ?></span><time id="tLocalTime">&nbsp;</time> <span id="tLocalDate">&nbsp;</span></div>
							</div>
						</div>
						<div class="nex-widget nex-widget--cta nex-widget--commerce">
							<?php nexoraCommerceHomeCard(); ?>
							<a class="nex-sidebar-banner nex-sidebar-banner--voting" href="<?php echo __BASE_URL__; ?>vote/"
								aria-label="<?php echo htmlspecialchars(nexora_lang('nexora_vote', 'Votar'), ENT_QUOTES, 'UTF-8'); ?>">
								<img class="nex-sidebar-banner__media" src="<?php echo __PATH_TEMPLATE_IMG__; ?>voting.webp" alt="" loading="lazy" decoding="async">
								<span class="nex-sidebar-banner__label"><?php echo htmlspecialchars(nexora_lang('nexora_vote', 'Votar'), ENT_QUOTES, 'UTF-8'); ?></span>
							</a>
						</div>
					</aside>
				</section>

				<a class="nex-guides-feature nex-reveal" href="https://guides.nexoravenetworks.com/"
					aria-label="<?php echo htmlspecialchars(nexora_lang('nexora_guides_aria', 'Abrir el centro de guías de Nexora'), ENT_QUOTES, 'UTF-8'); ?>">
					<span class="nex-guides-feature__beam" aria-hidden="true"></span>
					<span class="nex-guides-feature__sigil" aria-hidden="true">
						<svg viewBox="0 0 64 64" focusable="false">
							<path class="nex-guides-feature__ring" d="M32 5 55 18v28L32 59 9 46V18L32 5Z" />
							<path class="nex-guides-feature__book" d="M18 21.5c5.8-.8 10.1.5 14 4.1 3.9-3.6 8.2-4.9 14-4.1v22.2c-5.6-.6-9.9.8-14 4.3-4.1-3.5-8.4-4.9-14-4.3V21.5Zm14 4.1V48m-9.5-21.3c3.7-.1 6.5 1 9.5 3.5m9.5-3.5c-3.7-.1-6.5 1-9.5 3.5" />
							<path class="nex-guides-feature__spark" d="m32 10 1.8 4.2L38 16l-4.2 1.8L32 22l-1.8-4.2L26 16l4.2-1.8L32 10Z" />
						</svg>
					</span>
					<span class="nex-guides-feature__copy">
						<small><?php echo htmlspecialchars(nexora_lang('nexora_guides_kicker', 'Archivo de conocimiento'), ENT_QUOTES, 'UTF-8'); ?></small>
						<strong><?php echo htmlspecialchars(nexora_lang('nexora_guides_title', 'Guías Nexora'), ENT_QUOTES, 'UTF-8'); ?></strong>
						<span><?php echo htmlspecialchars(nexora_lang('nexora_guides_text', 'Entiende clases, sistemas y rutas de progreso antes de invertir tu tiempo. Información ordenada para decidir mejor dentro del juego.'), ENT_QUOTES, 'UTF-8'); ?></span>
					</span>
					<span class="nex-guides-feature__action">
						<span>
							<small><?php echo htmlspecialchars(nexora_lang('nexora_guides_scope', 'Clases · Sistemas · Progresión'), ENT_QUOTES, 'UTF-8'); ?></small>
							<strong><?php echo htmlspecialchars(nexora_lang('nexora_guides_open', 'Consultar guías'), ENT_QUOTES, 'UTF-8'); ?></strong>
						</span>
						<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M5 12h13M13 6l6 6-6 6" /></svg>
					</span>
				</a>

				<?php nexoraHomeRankings(); ?>

				<?php nexoraCastleSiegeEpic(); ?>

				<?php include __PATH_TEMPLATE_ROOT__ . 'inc/content-hub.php'; ?>

			<?php } else { ?>
				<div class="nex-page-layout <?php echo $nexUsercpSidebar ? 'nex-page-layout--sidebar' : ''; ?>">
					<div class="nex-page-main nex-panel-body<?php echo ($page === 'usercp' || $page === 'donation') ? ' nex-page-main--ucp-modules' : ''; ?>">
						<?php
						nexora_template_load_module($handler, $page, $subpage);
						?>
					</div>
					<?php if ($nexUsercpSidebar) { ?>
						<aside class="nex-page-aside">
							<?php include __PATH_TEMPLATE_ROOT__ . 'inc/modules/sidebar.php'; ?>
						</aside>
					<?php } ?>
				</div>
			<?php } ?>
		</div>
	</main>

	<?php if ($nexIsHome && function_exists('nexoraCountdownRender')) { nexoraCountdownRender('floating'); } ?>

	<?php if (is_file(__PATH_TEMPLATE_ROOT__ . 'inc/modules/streamer-widget.php')) {
		include __PATH_TEMPLATE_ROOT__ . 'inc/modules/streamer-widget.php';
	} ?>

	<?php if ($nexWhatsappUrl !== '') { ?>
		<a class="nex-whatsapp-cta" href="<?php echo htmlspecialchars($nexWhatsappUrl, ENT_QUOTES, 'UTF-8'); ?>" target="_blank" rel="noopener noreferrer"
			aria-label="<?php echo htmlspecialchars(nexora_lang('nexora_whatsapp_aria', 'Entrar al grupo de WhatsApp de MU Nexora'), ENT_QUOTES, 'UTF-8'); ?>">
			<span class="nex-whatsapp-cta__icon" aria-hidden="true">
				<svg viewBox="0 0 24 24" focusable="false"><path d="M17.47 14.38c-.3-.15-1.76-.87-2.03-.97-.27-.1-.47-.15-.67.15-.2.3-.77.97-.94 1.17-.17.2-.35.22-.65.07-.3-.15-1.25-.46-2.39-1.47-.88-.79-1.48-1.76-1.65-2.06-.17-.3-.02-.46.13-.61.13-.13.3-.35.45-.52.15-.17.2-.3.3-.5.1-.2.05-.37-.02-.52-.08-.15-.67-1.61-.92-2.21-.24-.58-.49-.5-.67-.51h-.57c-.2 0-.52.07-.79.37-.27.3-1.04 1.02-1.04 2.48s1.07 2.88 1.22 3.08c.15.2 2.1 3.21 5.08 4.5.71.31 1.26.49 1.69.63.71.23 1.36.19 1.87.12.57-.08 1.76-.72 2.01-1.41.25-.7.25-1.3.17-1.42-.07-.13-.27-.2-.57-.35M12.04 21.5h-.01a9.84 9.84 0 0 1-5.02-1.38l-.36-.21-3.73.98 1-3.64-.24-.37a9.83 9.83 0 0 1-1.51-5.26C2.17 6.17 6.6 1.74 12.05 1.74a9.8 9.8 0 0 1 6.99 2.9 9.82 9.82 0 0 1 2.89 6.99c0 5.44-4.43 9.87-9.89 9.87m8.41-18.29A11.8 11.8 0 0 0 12.05 0C5.5 0 .18 5.32.18 11.87c0 2.09.55 4.12 1.59 5.91L.08 24l6.36-1.67a11.86 11.86 0 0 0 5.61 1.43h.01c6.54 0 11.87-5.32 11.87-11.87 0-3.17-1.24-6.15-3.48-8.68"/></svg>
			</span>
			<span class="nex-whatsapp-cta__copy">
				<small><?php echo htmlspecialchars(nexora_lang('nexora_whatsapp_kicker', 'Comunidad Nexora'), ENT_QUOTES, 'UTF-8'); ?></small>
				<strong><?php echo htmlspecialchars(nexora_lang('nexora_whatsapp_join', 'Entrar al grupo'), ENT_QUOTES, 'UTF-8'); ?></strong>
			</span>
		</a>
	<?php } ?>

	<?php include __PATH_TEMPLATE_ROOT__ . 'inc/music-player.php'; ?>

	<?php include __PATH_TEMPLATE_ROOT__ . 'inc/footer.php'; ?>

	<script src="<?php echo __PATH_TEMPLATE_JS__; ?>main.js?v=3" defer></script>
	<?php if (!empty($GLOBALS['nex_music_rendered'])) { ?><script src="<?php echo __PATH_TEMPLATE_JS__; ?>music-player.js?v=5" defer></script><?php } ?>
	<script src="<?php echo __PATH_TEMPLATE_JS__; ?>nexora.js?v=19" defer></script>
	<?php if ($nexIsHome) { ?><script src="<?php echo __PATH_TEMPLATE_JS__; ?>events.js?v=3" defer></script><?php } ?>
	<?php if ($page === 'shop') { ?><script src="<?php echo __PATH_TEMPLATE_JS__; ?>itemshop.js?v=3" defer></script><?php } ?>
</body>

</html>
