<?php
$realmLabel=class_exists('NexoraRealm')?NexoraRealm::label():'Actual';
echo '<h2>Códigos — '.htmlspecialchars($realmLabel,ENT_QUOTES,'UTF-8').'</h2>';
if(isset($_POST['disable'])) { try { (new \Plugin\RedeemCode\RedeemCode())->disableCode($_POST['disable']); message('success','Código deshabilitado.'); } catch(Exception $e){message('error',$e->getMessage());} }
$r=new \Plugin\RedeemCode\RedeemCode(); $codes=$r->getRedeemCodesList(); $credit=new CreditSystem();
$configs=$credit->showConfigs(); $titles=array(0=>'None'); if(is_array($configs)) foreach($configs as $c) $titles[(int)$c['config_id']]=$c['config_title'];
echo '<table class="table table-hover table-striped"><thead><tr><th>Código</th><th>Tipo</th><th>Límite</th><th>Cuenta</th><th>Créditos</th><th>Reward</th><th>Estado</th><th>Acciones</th></tr></thead><tbody>';
foreach($codes as $c) {
    $id=(int)$c['id']; $status=(int)$c['status']===1?'<span class="label label-danger">cerrado</span>':'<span class="label label-success">activo</span>';
    echo '<tr><td>'.htmlspecialchars($c['redeem_code'],ENT_QUOTES,'UTF-8').'</td><td>'.htmlspecialchars($c['redeem_type'],ENT_QUOTES,'UTF-8').'</td><td>'.($c['redeem_limit']===null?'—':(int)$c['redeem_limit']).'</td><td>'.htmlspecialchars((string)$c['redeem_user'],ENT_QUOTES,'UTF-8').'</td><td>'.htmlspecialchars($titles[(int)$c['redeem_credit_config_id']]??('ID '.(int)$c['redeem_credit_config_id']),ENT_QUOTES,'UTF-8').'</td><td>'.number_format((int)$c['redeem_credit_amount']).'</td><td>'.$status.'</td><td>';
    if((int)$c['status']!==1) echo '<form method="post" style="display:inline"><input type="hidden" name="disable" value="'.$id.'"><button class="btn btn-danger btn-xs" type="submit">disable</button></form> ';
    echo '<a class="btn btn-default btn-xs" href="'.admincp_base('redeem&page=logs&id='.$id).'">logs</a></td></tr>';
}
echo '</tbody></table>';
