<?php
$roulette=new \Plugin\NexoraRoulette\NexoraRoulette();
if(isset($_POST['deliver_manual'])) {
    try { $roulette->markManualDelivered((int)$_POST['deliver_manual'],(string)($_POST['admin_note']??'')); message('success','Premio manual marcado como entregado.'); }
    catch(Throwable $e){ message('error',$e->getMessage()); }
}
if(isset($_POST['deliver_exchange_manual'])) {
    try { $roulette->markExchangeManualDelivered((int)$_POST['deliver_exchange_manual'],(string)($_POST['admin_note']??'')); message('success','Canje manual marcado como entregado.'); }
    catch(Throwable $e){ message('error',$e->getMessage()); }
}
$claims=$roulette->pendingClaims(300);
$exchangeClaims=$roulette->pendingExchangeClaims(300);
$rarities=\Plugin\NexoraRoulette\NexoraRoulette::rarityLabels();
?>
<h1 class="page-header">Reclamos de Ruleta <small><?php echo htmlspecialchars($roulette->getRealmLabel(),ENT_QUOTES,'UTF-8'); ?></small></h1>
<div class="alert alert-info">Este centro agrupa entregas pendientes tanto de <strong>giros</strong> como de <strong>canjes de Fragmentos</strong>. Los ítems de Arsenal los reclama el jugador cuando su cuenta está desconectada; las entregas manuales requieren confirmación del staff.</div>

<div class="panel panel-default">
<div class="panel-heading"><strong>Premios pendientes desde giros</strong> <span class="badge"><?php echo number_format(count($claims)); ?></span></div>
<div class="table-responsive"><table class="table table-striped table-hover" style="margin:0"><thead><tr><th>Giro</th><th>Cuenta</th><th>Premio</th><th>Rareza</th><th>Estado</th><th>Fecha</th><th>Acción</th></tr></thead><tbody>
<?php if(!$claims){ ?><tr><td colspan="7" class="text-muted">No hay reclamos pendientes desde giros.</td></tr><?php } ?>
<?php foreach($claims as $row){ ?><tr>
<td>#<?php echo (int)$row['id']; ?></td>
<td><strong><?php echo htmlspecialchars($row['account_username'],ENT_QUOTES,'UTF-8'); ?></strong><br><small>UID <?php echo (int)$row['user_id']; ?></small></td>
<td><strong><?php echo htmlspecialchars($row['prize_name'],ENT_QUOTES,'UTF-8'); ?></strong><?php if($row['prize_type']==='itemshop'){ ?><br><small>Arsenal ID: <?php echo (int)$row['prize_item_id']; ?></small><?php } ?></td>
<td><?php echo htmlspecialchars($rarities[$row['rarity']]??$row['rarity'],ENT_QUOTES,'UTF-8'); ?></td>
<td><?php echo $row['status']==='pending_item'?'<span class="label label-warning">Esperando reclamo</span>':'<span class="label label-info">Entrega manual</span>'; ?></td>
<td><?php echo htmlspecialchars((string)$row['created_at'],ENT_QUOTES,'UTF-8'); ?></td>
<td><?php if($row['status']==='pending_manual'){ ?><form method="post" style="min-width:260px"><div class="input-group input-group-sm"><input class="form-control" name="admin_note" maxlength="500" placeholder="Referencia / nota"><span class="input-group-btn"><button class="btn btn-success" name="deliver_manual" value="<?php echo (int)$row['id']; ?>" onclick="return confirm('¿Confirmar que este premio fue entregado?');">Confirmar</button></span></div></form><?php } else { ?><span class="text-muted">Jugador debe estar offline</span><?php } ?></td>
</tr><?php } ?>
</tbody></table></div>
</div>

<div class="panel panel-warning">
<div class="panel-heading"><strong>Entregas pendientes desde canjes de Fragmentos</strong> <span class="badge"><?php echo number_format(count($exchangeClaims)); ?></span></div>
<div class="table-responsive"><table class="table table-striped table-hover" style="margin:0"><thead><tr><th>Canje</th><th>Cuenta</th><th>Recompensa</th><th>Costo</th><th>Estado</th><th>Fecha</th><th>Acción</th></tr></thead><tbody>
<?php if(!$exchangeClaims){ ?><tr><td colspan="7" class="text-muted">No hay entregas pendientes desde canjes.</td></tr><?php } ?>
<?php foreach($exchangeClaims as $row){ ?><tr>
<td>#<?php echo (int)$row['id']; ?></td>
<td><strong><?php echo htmlspecialchars($row['account_username'],ENT_QUOTES,'UTF-8'); ?></strong><br><small>UID <?php echo (int)$row['user_id']; ?></small></td>
<td><strong><?php echo htmlspecialchars($row['reward_name'],ENT_QUOTES,'UTF-8'); ?></strong><?php if($row['reward_type']==='itemshop'){ ?><br><small>Arsenal ID: <?php echo (int)$row['reward_item_id']; ?></small><?php } ?></td>
<td><strong><?php echo number_format((int)$row['fragment_cost']); ?> ◆</strong></td>
<td><?php echo $row['status']==='pending_item'?'<span class="label label-warning">Reservado / esperando reclamo</span>':'<span class="label label-info">Entrega manual</span>'; ?></td>
<td><?php echo htmlspecialchars((string)$row['created_at'],ENT_QUOTES,'UTF-8'); ?></td>
<td><?php if($row['status']==='pending_manual'){ ?><form method="post" style="min-width:260px"><div class="input-group input-group-sm"><input class="form-control" name="admin_note" maxlength="500" placeholder="Referencia / nota"><span class="input-group-btn"><button class="btn btn-success" name="deliver_exchange_manual" value="<?php echo (int)$row['id']; ?>" onclick="return confirm('¿Confirmar que este canje fue entregado?');">Confirmar</button></span></div></form><?php } else { ?><span class="text-muted">El jugador debe reclamar offline</span><?php } ?></td>
</tr><?php } ?>
</tbody></table></div>
</div>
