<?php $PAGE_TITLE='Recharge Methods'; require __DIR__.'/_layout.php';
if($_SERVER['REQUEST_METHOD']==='POST'){csrf_check();
  $id=(int)($_POST['id']??0);
  $d=[$_POST['name'],$_POST['number'],$_POST['type'],$_POST['instruction'],isset($_POST['active'])?1:0];
  if($id){$d[]=$id; $pdo->prepare("UPDATE recharge_methods SET name=?,number=?,type=?,instruction=?,active=? WHERE id=?")->execute($d);}
  else $pdo->prepare("INSERT INTO recharge_methods(name,number,type,instruction,active) VALUES(?,?,?,?,?)")->execute($d);
  redirect('recharge_methods.php');
}
if(isset($_GET['del'])){$pdo->prepare("DELETE FROM recharge_methods WHERE id=?")->execute([(int)$_GET['del']]); redirect('recharge_methods.php');}
$edit=null; if(isset($_GET['edit'])){$s=$pdo->prepare("SELECT * FROM recharge_methods WHERE id=?"); $s->execute([(int)$_GET['edit']]); $edit=$s->fetch();}
$rows=$pdo->query("SELECT * FROM recharge_methods ORDER BY id DESC")->fetchAll();
?>
<h2 style="margin-bottom:14px">💳 রিচার্জ মেথড</h2>
<div class="card" style="margin-bottom:20px">
<form method="post" class="grid g2"><?=csrf_field()?><input type="hidden" name="id" value="<?=e($edit['id']??'')?>">
<div class="field"><label>Name (e.g. bKash)</label><input class="input" name="name" value="<?=e($edit['name']??'')?>" required></div>
<div class="field"><label>Type (Personal/Agent/Merchant)</label><input class="input" name="type" value="<?=e($edit['type']??'Personal')?>"></div>
<div class="field"><label>Number</label><input class="input" name="number" value="<?=e($edit['number']??'')?>" required></div>
<div class="field"><label style="margin-top:22px;display:flex;gap:6px"><input type="checkbox" name="active" <?=($edit['active']??1)?'checked':''?>> Active</label></div>
<div class="field" style="grid-column:1/-1"><label>Instruction</label><textarea class="input" name="instruction" rows="2"><?=e($edit['instruction']??'')?></textarea></div>
<div style="grid-column:1/-1"><button class="btn"><?=$edit?'Update':'Add'?></button></div>
</form></div>
<table><thead><tr><th>#</th><th>Name</th><th>Type</th><th>Number</th><th>Instruction</th><th>Active</th><th>Actions</th></tr></thead><tbody>
<?php foreach($rows as $r):?>
<tr><td>#<?=$r['id']?></td><td><b><?=e($r['name'])?></b></td><td><?=e($r['type'])?></td>
<td><?=e($r['number'])?></td><td><?=e($r['instruction'])?></td>
<td><span class="badge <?=$r['active']?'ok':'fail'?>"><?=$r['active']?'Active':'Off'?></span></td>
<td><a class="btn sm" href="?edit=<?=$r['id']?>">✏️</a>
<a class="btn sm danger" href="?del=<?=$r['id']?>" onclick="return confirm('Delete?')">🗑️</a></td></tr>
<?php endforeach;?>
</tbody></table>
<?php require __DIR__.'/_footer.php';
