<?php
if(!isLoggedIn()) redirect(1,'login');
echo '<div class="page-title"><span>'.lang('referralsystem_title').'</span></div>';
try {
    $r=new \Plugin\ReferralSystem\ReferralSystem();
    $r->setReferralUsername($_SESSION['username']);
    $characters=$r->getReferralCharacters($_SESSION['username']);
    echo '<p><strong>'.htmlspecialchars(NexoraRealm::label(),ENT_QUOTES,'UTF-8').'</strong> — '.lang('referralsystem_txt_7').'</p>';
    echo '<h3>'.lang('referralsystem_txt_1').'</h3>';
    if(count($characters)===0) { message('warning',lang('error_46',true)); }
    else {
        foreach($characters as $character) {
            $url=$r->inviteUrl($character);
            echo '<div style="margin-bottom:12px"><strong>'.htmlspecialchars($character,ENT_QUOTES,'UTF-8').'</strong><input type="text" value="'.htmlspecialchars($url,ENT_QUOTES,'UTF-8').'" class="form-control" readonly onclick="this.select()"></div>';
        }
    }
    echo '<h3>'.lang('referralsystem_txt_6').'</h3>';
    $rows=$r->getAccountReferrals();
    if(!is_array($rows)) { message('warning',lang('referralsystem_txt_2')); return; }
    echo '<div class="table-responsive"><table class="table"><thead><tr><th>'.lang('referralsystem_col_date').'</th><th>'.lang('referralsystem_col_player').'</th><th>'.lang('referralsystem_col_complete').'</th><th>'.lang('referralsystem_col_status').'</th></tr></thead><tbody>';
    foreach($rows as $row) {
        $status=(int)$row['status'];
        $labels=array(0=>lang('referralsystem_txt_3'),1=>lang('referralsystem_txt_4'),2=>lang('referralsystem_txt_8'),3=>lang('referralsystem_txt_9'));
        $player=check_value($row['referred_character']) ? playerProfile($row['referred_character']) : lang('referralsystem_txt_5');
        echo '<tr><td>'.htmlspecialchars((string)$row['referred_registration_date'],ENT_QUOTES,'UTF-8').'</td><td>'.$player.'</td><td>'.htmlspecialchars((string)$row['referred_complete_date'],ENT_QUOTES,'UTF-8').'</td><td>'.htmlspecialchars(isset($labels[$status])?$labels[$status]:'-',ENT_QUOTES,'UTF-8').'</td></tr>';
    }
    echo '</tbody></table></div>';
} catch(Exception $ex) { message('error',$ex->getMessage()); }
