<?php
function exchangePackagesPath(){ return \Plugin\ExchangeResets\ExchangeResets::configPath(class_exists('NexoraRealm')?NexoraRealm::active():null); }
function exchangePackagesLoad(){ $cfg=json_decode((string)file_get_contents(exchangePackagesPath()),true); if(!is_array($cfg)) $cfg=array(); if(!isset($cfg['exchange_list'])||!is_array($cfg['exchange_list'])) $cfg['exchange_list']=array(); if(!isset($cfg['credit_config'])) $cfg['credit_config']='0'; return $cfg; }
function exchangePackagesSave($cfg){ $path=exchangePackagesPath(); if(!is_writable($path)) throw new Exception('El archivo de configuración no tiene permisos de escritura.'); $json=json_encode($cfg,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); if($json===false||file_put_contents($path,$json,LOCK_EX)===false) throw new Exception('No se pudo guardar la configuración.'); }
$pluginConfig=exchangePackagesLoad();
if(isset($_POST['delete'])) { try { $key=(int)$_POST['delete']; if($key<1||!array_key_exists($key,$pluginConfig['exchange_list'])) throw new Exception('El paquete no es válido.'); unset($pluginConfig['exchange_list'][$key]); exchangePackagesSave($pluginConfig); $pluginConfig=exchangePackagesLoad(); message('success','Paquete eliminado.'); } catch(Exception $e){message('error',$e->getMessage());} }
if(isset($_POST['package_submit'])) { try { $resets=(int)($_POST['package_resets']??0); $credits=(int)($_POST['package_credits']??-1); if($resets<1||$credits<0) throw new Exception('Los valores del paquete no son válidos.'); if(array_key_exists($resets,$pluginConfig['exchange_list'])) throw new Exception('Ya existe un paquete para esa cantidad de resets.'); $pluginConfig['exchange_list'][$resets]=$credits; ksort($pluginConfig['exchange_list'],SORT_NUMERIC); exchangePackagesSave($pluginConfig); $pluginConfig=exchangePackagesLoad(); message('success','Paquete agregado.'); } catch(Exception $e){message('error',$e->getMessage());} }
?>
<h2>Paquetes de Canje — Nexora Edition</h2><p><strong>Contexto:</strong> <?php echo class_exists('NexoraRealm')?htmlspecialchars(NexoraRealm::label(),ENT_QUOTES,'UTF-8'):'Actual'; ?>.</p>
<div class="row"><div class="col-xs-12 col-md-4"><div class="panel panel-primary"><div class="panel-heading">Nuevo paquete</div><div class="panel-body"><form method="post"><div class="form-group"><label>Resets:</label><input type="number" min="1" class="form-control" name="package_resets" required></div><div class="form-group"><label>Créditos:</label><input type="number" min="0" class="form-control" name="package_credits" required></div><button class="btn btn-primary" type="submit" name="package_submit" value="1">Agregar paquete</button></form></div></div></div>
<div class="col-xs-12 col-md-8"><div class="panel panel-default"><div class="panel-heading">Paquetes configurados</div><div class="panel-body">
<?php if(count($pluginConfig['exchange_list'])){ ?><table class="table table-hover table-striped"><thead><tr><th>Resets</th><th>Créditos</th><th></th></tr></thead><tbody><?php foreach($pluginConfig['exchange_list'] as $resets=>$credits){ ?><tr><td><?php echo number_format($resets); ?></td><td><?php echo number_format($credits); ?></td><td><form method="post" style="display:inline"><button class="btn btn-xs btn-danger" type="submit" name="delete" value="<?php echo (int)$resets; ?>">Eliminar</button></form></td></tr><?php } ?></tbody></table><?php } else { message('warning','No hay paquetes configurados para este reino.'); } ?>
</div></div></div></div>
