<?php
if(!isLoggedIn()) redirect(1,'login');
try {
    $VIP=new \Plugin\VIP\VIP();
    $VIP->setUsername($_SESSION['username']);
    $active=$VIP->isAccountVip();
    $button=$active ? lang('vip_txt_3') : lang('vip_txt_2');
    $token=\Plugin\VIP\VIP::issuePurchaseToken();
?>
<section class="nex-vip-shell">
    <div class="nex-vip-head">
        <span class="nex-vip-kicker">REINO <?php echo htmlspecialchars(strtoupper(NexoraRealm::label()),ENT_QUOTES,'UTF-8'); ?></span>
        <h1>Membresia VIP</h1>
        <p>La membresia se aplica unicamente al reino de tu sesion actual.</p>
    </div>
    <?php
    if(isset($_GET['success'])) message('success',lang('vip_success_1'));
    if($active) {
        $d=$VIP->getAccountVipData();
        $remaining=sec_to_dhms(max(0,strtotime($d[_CLMN_VIP_DATE_])-time()));
        echo '<div class="nex-vip-current"><strong>'.htmlspecialchars(lang('vip_title_type_'.$d[_CLMN_VIP_TYPE_]),ENT_QUOTES,'UTF-8').'</strong><span>Activo por '.(int)$remaining[0].' dias mas</span></div>';
    }
    ?>
    <div class="nex-vip-grid">
    <?php foreach($VIP->getVipTypeData() as $type=>$data) { if(!$data['enabled']) continue; ?>
        <article class="nex-vip-card nex-vip-card--<?php echo (int)$type; ?>">
            <div class="nex-vip-card-head"><span>VIP</span><h2><?php echo htmlspecialchars($data['title'],ENT_QUOTES,'UTF-8'); ?></h2></div>
            <div class="nex-vip-packages">
            <?php foreach($data['packages'] as $idx=>$days) { $cost=$VIP->getVipCost($days,$type); $discount=isset($data['discounts'][$idx])?(int)$data['discounts'][$idx]:0; ?>
                <form method="post" action="<?php echo __VIP_HOME__; ?>order/" class="nex-vip-package">
                    <input type="hidden" name="type" value="<?php echo (int)$type; ?>">
                    <input type="hidden" name="package" value="<?php echo (int)$days; ?>">
                    <input type="hidden" name="purchase_token" value="<?php echo htmlspecialchars($token,ENT_QUOTES,'UTF-8'); ?>">
                    <div><strong><?php echo (int)$days; ?> dias</strong><?php if($discount>0){ ?><small>-<?php echo $discount; ?>%</small><?php } ?></div>
                    <span><?php echo number_format($cost); ?> creditos</span>
                    <button type="submit" class="nex-vip-buy" data-processing="Procesando..."><?php echo htmlspecialchars($button,ENT_QUOTES,'UTF-8'); ?></button>
                </form>
            <?php } ?>
            </div>
        </article>
    <?php } ?>
    </div>
    <p class="nex-vip-note">Si estas conectado al juego, la membresia puede requerir reconexion para reflejar sus beneficios.</p>
</section>
<script>
document.addEventListener('submit',function(e){var f=e.target;if(!f.classList.contains('nex-vip-package'))return;var b=f.querySelector('button[type="submit"]');if(!b||b.disabled)return;b.disabled=true;b.dataset.original=b.textContent;b.textContent=b.dataset.processing||'Procesando...';});
</script>
<?php
} catch(Exception $ex) { message('error',$ex->getMessage()); }
