<?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 - Add Stats (rediseño)
 */
if (!isLoggedIn()) redirect(1, 'login');

$pageTitle = lang('module_titles_txt_25', true) ?: 'Add Stats';

try {
	if (!mconfig('active')) throw new Exception(lang('error_47', true));
	if (!is_array($custom['character_cmd'])) throw new Exception(lang('error_59', true));
	$maxStats = mconfig('addstats_max_stats');

	$Character = new Character();
	$AccountCharacters = $Character->AccountCharacter($_SESSION['username']);
	if (!is_array($AccountCharacters)) throw new Exception(lang('error_46', true));

	if (isset($_POST['submit'])) {
		try {
			$Character->setUserid($_SESSION['userid']);
			$Character->setUsername($_SESSION['username']);
			$Character->setCharacter($_POST['character']);
			if (isset($_POST['add_str']) && $_POST['add_str'] > 0) $Character->setStrength($_POST['add_str']);
			if (isset($_POST['add_agi']) && $_POST['add_agi'] > 0) $Character->setAgility($_POST['add_agi']);
			if (isset($_POST['add_vit']) && $_POST['add_vit'] > 0) $Character->setVitality($_POST['add_vit']);
			if (isset($_POST['add_ene']) && $_POST['add_ene'] > 0) $Character->setEnergy($_POST['add_ene']);
			if (isset($_POST['add_com']) && $_POST['add_com'] > 0) $Character->setCommand($_POST['add_com']);
			$Character->CharacterAddStats();
		} catch (Exception $ex) {
			message('error', $ex->getMessage());
		}
	}

	echo '<div class="nexora-usercp-page">';
	echo '<header class="nexora-usercp-page-header"><h1 class="nexora-usercp-page-title">' . htmlspecialchars($pageTitle) . '</h1></header>';

	foreach ($AccountCharacters as $thisCharacter) {
		$characterData = $Character->CharacterData($thisCharacter);
		$characterIMG = $Character->GenerateCharacterClassAvatar($characterData[_CLMN_CHR_CLASS_]);

		echo '<div class="nexora-usercp-card">';
		echo '<div class="nexora-usercp-card-avatar">' . $characterIMG . '</div>';
		echo '<div class="nexora-usercp-card-body">';
		echo '<div class="nexora-usercp-card-name">' . htmlspecialchars($characterData[_CLMN_CHR_NAME_]) . '</div>';
		echo '<form class="nexora-usercp-card-form nexora-usercp-form" action="" method="post">';
		echo '<input type="hidden" name="character" value="' . htmlspecialchars($characterData[_CLMN_CHR_NAME_]) . '"/>';

		echo '<div class="nexora-usercp-field"><span class="nexora-usercp-label">' . lang('addstats_txt_2', true) . '</span><span>' . langf('addstats_txt_2', array(number_format($characterData[_CLMN_CHR_LVLUP_POINT_]))) . '</span></div>';
		echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="add_str_' . $characterData[_CLMN_CHR_NAME_] . '">' . lang('addstats_txt_3', true) . ' (' . $characterData[_CLMN_CHR_STAT_STR_] . ')</label><input type="number" class="nexora-usercp-input" id="add_str_' . $characterData[_CLMN_CHR_NAME_] . '" min="1" step="1" max="' . $maxStats . '" name="add_str" placeholder="0"></div>';
		echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="add_agi_' . $characterData[_CLMN_CHR_NAME_] . '">' . lang('addstats_txt_4', true) . ' (' . $characterData[_CLMN_CHR_STAT_AGI_] . ')</label><input type="number" class="nexora-usercp-input" id="add_agi_' . $characterData[_CLMN_CHR_NAME_] . '" min="1" step="1" max="' . $maxStats . '" name="add_agi" placeholder="0"></div>';
		echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="add_vit_' . $characterData[_CLMN_CHR_NAME_] . '">' . lang('addstats_txt_5', true) . ' (' . $characterData[_CLMN_CHR_STAT_VIT_] . ')</label><input type="number" class="nexora-usercp-input" id="add_vit_' . $characterData[_CLMN_CHR_NAME_] . '" min="1" step="1" max="' . $maxStats . '" name="add_vit" placeholder="0"></div>';
		echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="add_ene_' . $characterData[_CLMN_CHR_NAME_] . '">' . lang('addstats_txt_6', true) . ' (' . $characterData[_CLMN_CHR_STAT_ENE_] . ')</label><input type="number" class="nexora-usercp-input" id="add_ene_' . $characterData[_CLMN_CHR_NAME_] . '" min="1" step="1" max="' . $maxStats . '" name="add_ene" placeholder="0"></div>';

		if (in_array($characterData[_CLMN_CHR_CLASS_], $custom['character_cmd'])) {
			echo '<div class="nexora-usercp-field"><label class="nexora-usercp-label" for="add_com_' . $characterData[_CLMN_CHR_NAME_] . '">' . lang('addstats_txt_7', true) . ' (' . $characterData[_CLMN_CHR_STAT_CMD_] . ')</label><input type="number" class="nexora-usercp-input" id="add_com_' . $characterData[_CLMN_CHR_NAME_] . '" min="1" step="1" max="' . $maxStats . '" name="add_com" placeholder="0"></div>';
		}

		echo '<div class="nexora-usercp-field" style="margin-top:16px;"><button type="submit" name="submit" value="submit" class="nexora-usercp-btn">' . lang('addstats_txt_8', true) . '</button></div>';
		echo '</form></div></div>';
	}

	echo '<div class="nexora-usercp-requirements">';
	if (mconfig('required_level') > 0) echo '<p>' . langf('addstats_txt_11', array(number_format(mconfig('required_level')))) . '</p>';
	if (mconfig('required_master_level') > 0) echo '<p>' . langf('addstats_txt_10', array(number_format(mconfig('required_master_level')))) . '</p>';
	if (mconfig('zen_cost') > 0) echo '<p>' . langf('addstats_txt_9', array(number_format(mconfig('zen_cost')))) . '</p>';
	echo '<p>' . langf('addstats_txt_12', array(number_format(mconfig('max_stats')))) . '</p>';
	if (mconfig('minimum_limit') > 0) echo '<p>' . langf('addstats_txt_13', array(number_format(mconfig('minimum_limit')))) . '</p>';
	echo '</div></div>';
} catch (Exception $ex) {
	message('error', $ex->getMessage());
}
