<?php
if(!isLoggedIn()) redirect(1,'login');
echo '<div class="page-title"><span>'.lang('exchangeresets_title',true).'</span></div>';
try {
    $ExchangeResets=new \Plugin\ExchangeResets\ExchangeResets();
    $exchangeList=$ExchangeResets->getExchangeList();
    if(!isset($_GET['character'])) redirect(1,'exchangeresets/characters/');
    $characterName=base64url_decode($_GET['character']);
    $Character=new Character();
    $AccountCharacters=$Character->AccountCharacter($_SESSION['username']);
    if(!is_array($AccountCharacters) || !in_array($characterName,$AccountCharacters,true)) throw new Exception(lang('exchangeresets_error_7',true));
    $characterData=$Character->CharacterData($characterName);
    if(!is_array($characterData)) throw new Exception(lang('exchangeresets_error_7',true));

    if(($_SERVER['REQUEST_METHOD']??'GET')==='POST' && isset($_POST['resets'])) {
        try {
            $token=isset($_POST['exchange_token'])?(string)$_POST['exchange_token']:'';
            if(!\Plugin\ExchangeResets\ExchangeResets::consumeExchangeToken($token)) throw new Exception(lang('exchangeresets_error_12',true));
            $ExchangeResets->setCharacter($characterName);
            $ExchangeResets->setResets($_POST['resets']);
            $ExchangeResets->exchangeResets();
        } catch(Exception $ex) { message('error',$ex->getMessage()); }
    }

    $exchangedResets=$ExchangeResets->getExchangedResetsFromCharacter($characterData[_CLMN_CHR_NAME_]);
    $availableExchanges=max(0,(int)$characterData[_CLMN_CHR_RSTS_]-(int)$exchangedResets);
    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> · '.htmlspecialchars($characterData[_CLMN_CHR_NAME_],ENT_QUOTES,'UTF-8').' · '.number_format($availableExchanges).' '.lang('exchangeresets_txt_7',true).'</p>';
    if(!is_array($exchangeList) || count($exchangeList)<1) { message('warning',lang('exchangeresets_error_6',true)); return; }
    $token=\Plugin\ExchangeResets\ExchangeResets::issueExchangeToken();
    echo '<table class="table general-table-ui"><thead><tr><th>'.lang('exchangeresets_txt_4',true).'</th><th>'.lang('exchangeresets_txt_5',true).'</th><th></th></tr></thead><tbody>';
    foreach($exchangeList as $resets=>$reward) {
        $disabled=$availableExchanges<(int)$resets;
        echo '<tr><td>'.number_format($resets).'</td><td>'.number_format($reward).' '.lang('exchangeresets_txt_3',true).'</td><td>';
        echo '<form method="post" action="" style="margin:0">';
        echo '<input type="hidden" name="resets" value="'.(int)$resets.'">';
        echo '<input type="hidden" name="exchange_token" value="'.htmlspecialchars($token,ENT_QUOTES,'UTF-8').'">';
        echo '<button type="submit" class="btn btn-primary"'.($disabled?' disabled':'').' data-nexora-submit-text="'.htmlspecialchars(lang('exchangeresets_txt_8',true),ENT_QUOTES,'UTF-8').'">'.lang('exchangeresets_txt_6',true).'</button>';
        echo '</form></td></tr>';
    }
    echo '</tbody></table>';
} catch(Exception $ex) { message('error',$ex->getMessage()); }
