<?php
/**
 * WebEngine CMS
 * https://webenginecms.org/
 * 
 * @version 1.2.5
 * @author Gabo Development <https://gabodevelop.com/>
 * @copyright (c) 2026 Gabriel Development, All Rights Reserved
 * 
 * Licensed under the MIT license
 * http://opensource.org/licenses/MIT
 */

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

$pageTitle = lang('module_titles_txt_13',true) ?: 'Clear PK';

try {
	if(!mconfig('active')) throw new Exception(lang('error_47',true));
	$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']);
			$Character->CharacterClearPK();
		} 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>';
	echo '<div class="nexora-usercp-panel"><div class="nexora-usercp-panel-body"><div class="nexora-usercp-table-wrap">';
	echo '<table class="nexora-usercp-table"><thead><tr><th></th><th>'.lang('clearpk_txt_1',true).'</th><th>'.lang('clearpk_txt_2',true).'</th><th>'.lang('clearpk_txt_3',true).'</th><th></th></tr></thead><tbody>';
	foreach($AccountCharacters as $thisCharacter) {
		$characterData = $Character->CharacterData($thisCharacter);
		$characterIMG = $Character->GenerateCharacterClassAvatar($characterData[_CLMN_CHR_CLASS_]);
		echo '<form action="" method="post"><input type="hidden" name="character" value="'.htmlspecialchars($characterData[_CLMN_CHR_NAME_]).'"/><tr>';
		echo '<td class="nexora-usercp-char-avatar">'.$characterIMG.'</td>';
		echo '<td>'.htmlspecialchars($characterData[_CLMN_CHR_NAME_]).'</td>';
		echo '<td>'.number_format($characterData[_CLMN_CHR_ZEN_]).'</td>';
		echo '<td>'.returnPkLevel($characterData[_CLMN_CHR_PK_LEVEL_]).'</td>';
		echo '<td><button type="submit" name="submit" value="submit" class="nexora-usercp-btn">'.lang('clearpk_txt_4',true).'</button></td></tr></form>';
	}
	echo '</tbody></table></div></div></div>';
	echo '<div class="nexora-usercp-requirements">';
	if(mconfig('zen_cost') > 0) echo '<p>'.langf('clearpk_txt_5', array(number_format(mconfig('zen_cost')))).'</p>';
	echo '</div></div>';
} catch(Exception $ex) {
	message('error', $ex->getMessage());
}