<?php
/**
 * WebEngine CMS
 * https://webenginecms.org/
 *
 * @version 1.2.6
 * @author Gabo Development <https://gabodevelop.com/>
 * @copyright (c) 2026 Gabriel Development, All Rights Reserved
 */

/**
 * Nexora VE S6 - My Account (rediseño)
 * Información de cuenta, personajes y historial de conexión con estilo glass y acentos dorados.
 */

if (!isLoggedIn()) redirect(1, 'login');

if (!mconfig('active')) throw new Exception(lang('error_47'));

$common = new common();
$accountInfo = $common->accountInformation($_SESSION['userid']);
if (!is_array($accountInfo)) throw new Exception(lang('error_12'));

$onlineStatus = $common->accountOnline($_SESSION['username'])
	? '<span class="nexora-myaccount-badge nexora-myaccount-badge-success">' . lang('myaccount_txt_9') . '</span>'
	: '<span class="nexora-myaccount-badge nexora-myaccount-badge-danger">' . lang('myaccount_txt_10') . '</span>';

$accountStatus = ($accountInfo[_CLMN_BLOCCODE_] == 1)
	? '<span class="nexora-myaccount-badge nexora-myaccount-badge-danger">' . lang('myaccount_txt_8') . '</span>'
	: '<span class="nexora-myaccount-badge nexora-myaccount-badge-default">' . lang('myaccount_txt_7') . '</span>';

$Character = new Character();
$AccountCharacters = $Character->AccountCharacter($_SESSION['username']);

$creditsRows = array();
try {
	$creditSystem = new CreditSystem();
	$creditCofigList = $creditSystem->showConfigs();
	if (is_array($creditCofigList)) {
		foreach ($creditCofigList as $myCredits) {
			if (!$myCredits['config_display']) continue;
			$creditSystem->setConfigId($myCredits['config_id']);
			switch ($myCredits['config_user_col_id']) {
				case 'userid': $creditSystem->setIdentifier($accountInfo[_CLMN_MEMBID_]); break;
				case 'username': $creditSystem->setIdentifier($accountInfo[_CLMN_USERNM_]); break;
				case 'email': $creditSystem->setIdentifier($accountInfo[_CLMN_EMAIL_]); break;
				default: continue 2;
			}
			$creditsRows[] = array('title' => $myCredits['config_title'], 'value' => number_format($creditSystem->getCredits()));
		}
	}
} catch (Exception $ex) {}

$pageTitle = lang('module_titles_txt_4', true) ?: 'My Account';
?>
<div class="nexora-myaccount">
	<header class="nexora-myaccount-header">
		<h1 class="nexora-myaccount-title"><?php echo htmlspecialchars($pageTitle); ?></h1>
		<p class="nexora-myaccount-subtitle"><?php echo htmlspecialchars(config('website_title')); ?> — <?php echo htmlspecialchars($accountInfo[_CLMN_USERNM_]); ?></p>
	</header>

	<!-- Información de cuenta -->
	<section class="nexora-myaccount-panel nexora-myaccount-section">
		<div class="nexora-myaccount-panel-head">
			<h2 class="nexora-myaccount-panel-title"><?php echo htmlspecialchars(lang('myaccount_txt_1', true) ?: 'Account'); ?></h2>
		</div>
		<div class="nexora-myaccount-panel-body">
			<div class="nexora-myaccount-rows">
				<div class="nexora-myaccount-row">
					<span class="nexora-myaccount-label"><?php echo lang('myaccount_txt_1'); ?></span>
					<span class="nexora-myaccount-value"><?php echo $accountStatus; ?></span>
				</div>
				<div class="nexora-myaccount-row">
					<span class="nexora-myaccount-label"><?php echo lang('myaccount_txt_2'); ?></span>
					<span class="nexora-myaccount-value"><?php echo htmlspecialchars($accountInfo[_CLMN_USERNM_]); ?></span>
				</div>
				<div class="nexora-myaccount-row">
					<span class="nexora-myaccount-label"><?php echo lang('myaccount_txt_3'); ?></span>
					<span class="nexora-myaccount-value">
						<?php echo htmlspecialchars($accountInfo[_CLMN_EMAIL_]); ?>
						<a href="<?php echo __BASE_URL__; ?>usercp/myemail/" class="nexora-myaccount-btn"><?php echo lang('myaccount_txt_6'); ?></a>
					</span>
				</div>
				<div class="nexora-myaccount-row">
					<span class="nexora-myaccount-label"><?php echo lang('myaccount_txt_4'); ?></span>
					<span class="nexora-myaccount-value">
						&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;
						<a href="<?php echo __BASE_URL__; ?>usercp/mypassword/" class="nexora-myaccount-btn"><?php echo lang('myaccount_txt_6'); ?></a>
					</span>
				</div>
				<div class="nexora-myaccount-row">
					<span class="nexora-myaccount-label"><?php echo lang('myaccount_txt_5'); ?></span>
					<span class="nexora-myaccount-value"><?php echo $onlineStatus; ?></span>
				</div>
				<?php foreach ($creditsRows as $cr) { ?>
				<div class="nexora-myaccount-row">
					<span class="nexora-myaccount-label"><?php echo htmlspecialchars($cr['title']); ?></span>
					<span class="nexora-myaccount-value nexora-myaccount-value-number"><?php echo $cr['value']; ?></span>
				</div>
				<?php } ?>
			</div>
		</div>
	</section>

	<!-- Personajes -->
	<section class="nexora-myaccount-section">
		<h2 class="nexora-myaccount-section-title"><?php echo lang('myaccount_txt_15'); ?></h2>
		<?php if (is_array($AccountCharacters) && count($AccountCharacters) > 0) {
			$onlineCharacters = loadCache('online_characters.cache') ? loadCache('online_characters.cache') : array();
		?>
		<div class="nexora-myaccount-characters">
			<?php foreach ($AccountCharacters as $characterName) {
				$characterData = $Character->CharacterData($characterName);
				if (!is_array($characterData)) continue;
				if (defined('_TBL_MASTERLVL_')) {
					if (_TBL_MASTERLVL_ != _TBL_CHR_) {
						$characterMLData = $Character->getMasterLevelInfo($characterName);
						if (is_array($characterMLData)) $characterData[_CLMN_CHR_LVL_] += $characterMLData[_CLMN_ML_LVL_];
					} else {
						$characterData[_CLMN_CHR_LVL_] += $characterData[_CLMN_ML_LVL_];
					}
				}
				$characterClassAvatar = getPlayerClassAvatar($characterData[_CLMN_CHR_CLASS_], false);
				$characterOnlineStatus = in_array($characterName, $onlineCharacters)
					? '<img src="' . __PATH_ONLINE_STATUS__ . '" class="nexora-myaccount-char-status" alt="" />'
					: '<img src="' . __PATH_OFFLINE_STATUS__ . '" class="nexora-myaccount-char-status" alt="" />';
			?>
			<div class="nexora-myaccount-character-card">
				<div class="nexora-myaccount-character-name"><?php echo playerProfile($characterName); ?><?php echo $characterOnlineStatus; ?></div>
				<div class="nexora-myaccount-character-avatar">
					<a href="<?php echo playerProfile($characterName, true); ?>" target="_blank" rel="noopener"><?php echo '<img src="' . htmlspecialchars($characterClassAvatar) . '" alt="" />'; ?></a>
				</div>
				<div class="nexora-myaccount-character-location"><?php echo returnMapName($characterData[_CLMN_CHR_MAP_]); ?><br /><?php echo $characterData[_CLMN_CHR_MAP_X_]; ?>, <?php echo $characterData[_CLMN_CHR_MAP_Y_]; ?></div>
				<div class="nexora-myaccount-character-level"><?php echo $characterData[_CLMN_CHR_LVL_]; ?></div>
			</div>
			<?php } ?>
		</div>
		<?php } else {
			message('warning', lang('error_46'));
		} ?>
	</section>

	<!-- Historial de conexión -->
	<?php if (defined('_TBL_CH_')) {
		$me = Connection::Database('Me_MuOnline');
		$connectionHistory = $me->query_fetch("SELECT TOP 10 * FROM " . _TBL_CH_ . " WHERE " . _CLMN_CH_ACCID_ . " = ? ORDER BY " . _CLMN_CH_ID_ . " DESC", array($_SESSION['username']));
		if (is_array($connectionHistory) && count($connectionHistory) > 0) {
	?>
	<section class="nexora-myaccount-panel nexora-myaccount-section">
		<div class="nexora-myaccount-panel-head">
			<h2 class="nexora-myaccount-panel-title"><?php echo lang('myaccount_txt_16'); ?></h2>
		</div>
		<div class="nexora-myaccount-panel-body">
			<div class="nexora-myaccount-table-wrap">
				<table class="nexora-myaccount-table">
					<thead>
						<tr>
							<th><?php echo lang('myaccount_txt_13'); ?></th>
							<th><?php echo lang('myaccount_txt_17'); ?></th>
							<th><?php echo lang('myaccount_txt_18'); ?></th>
							<th><?php echo lang('myaccount_txt_19'); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ($connectionHistory as $row) { ?>
						<tr>
							<td><?php echo htmlspecialchars($row[_CLMN_CH_DATE_]); ?></td>
							<td><?php echo htmlspecialchars($row[_CLMN_CH_SRVNM_]); ?></td>
							<td><?php echo htmlspecialchars($row[_CLMN_CH_IP_]); ?></td>
							<td><?php echo htmlspecialchars($row[_CLMN_CH_STATE_]); ?></td>
						</tr>
						<?php } ?>
					</tbody>
				</table>
			</div>
		</div>
	</section>
	<?php }
	} ?>
</div>
