<?php
if(!isLoggedIn()) redirect(1,'login');
echo '<div class="page-title"><span>'.lang('exchangeresets_title',true).'</span></div>';
try {
    $ExchangeResets=new \Plugin\ExchangeResets\ExchangeResets();
    $Character=new Character();
    $AccountCharacters=$Character->AccountCharacter($_SESSION['username']);
    if(!is_array($AccountCharacters)) throw new Exception(lang('exchangeresets_error_5',true));
    $exchangeMap=$ExchangeResets->getExchangedResetsForCharacters($AccountCharacters);
    if(isset($_GET['success']) && (int)$_GET['success']===1) message('success',lang('exchangeresets_success_1',true));
    echo '<p><strong>'.htmlspecialchars(\NexoraRealm::label(),ENT_QUOTES,'UTF-8').'</strong> · '.lang('exchangeresets_txt_9',true).'</p>';
    echo '<table class="table general-table-ui"><tr><td></td><td>'.lang('exchangeresets_txt_1',true).'</td><td>'.lang('exchangeresets_txt_4',true).'</td><td>'.lang('exchangeresets_txt_2',true).'</td><td>'.lang('exchangeresets_txt_7',true).'</td><td></td></tr>';
    foreach($AccountCharacters as $thisCharacter) {
        $data=$Character->CharacterData($thisCharacter); if(!is_array($data)) continue;
        $name=$data[_CLMN_CHR_NAME_]; $exchanged=isset($exchangeMap[$name])?(int)$exchangeMap[$name]:0;
        $available=max(0,(int)$data[_CLMN_CHR_RSTS_]-$exchanged);
        echo '<tr><td>'.$Character->GenerateCharacterClassAvatar($data[_CLMN_CHR_CLASS_]).'</td><td>'.htmlspecialchars($name,ENT_QUOTES,'UTF-8').'</td><td>'.number_format($data[_CLMN_CHR_RSTS_]).'</td><td>'.number_format($exchanged).'</td><td>'.number_format($available).'</td><td><a href="'.__EXCHANGERESETS_HOME__.'exchange/character/'.base64url_encode($name).'/" class="btn btn-primary">'.lang('exchangeresets_txt_6',true).'</a></td></tr>';
    }
    echo '</table>';
} catch(Exception $ex) { message('error',$ex->getMessage()); }
