<?php
// ⚠️ POST handle করতে হবে layout require এর আগে, নাহলে redirect() → সাদা পেজ।
require_once __DIR__.'/../config/config.php';
require_admin();

if($_SERVER['REQUEST_METHOD']==='POST'){
  csrf_check();
  $id=(int)$_POST['id']; $act=$_POST['act']; $note=$_POST['note']??'';
  $s=$pdo->prepare("SELECT * FROM recharges WHERE id=?"); $s->execute([$id]); $r=$s->fetch();
  if($r && $r['status']==='pending'){
    if($act==='approve'){
      $pdo->prepare("UPDATE users SET balance=balance+?,total_recharge=total_recharge+? WHERE id=?")->execute([$r['amount'],$r['amount'],$r['user_id']]);
      $pdo->prepare("UPDATE recharges SET status='approved',admin_note=? WHERE id=?")->execute([$note,$id]);
      log_txn($pdo,$r['user_id'],'credit',$r['amount'],'recharge#'.$id,"Recharge approved");
    } elseif($act==='reject'){
      $pdo->prepare("UPDATE recharges SET status='rejected',admin_note=? WHERE id=?")->execute([$note,$id]);
    }
  }
  redirect('recharges.php');
}

$PAGE_TITLE='Recharges'; require __DIR__.'/_layout.php';

// ============ SEARCH ============
$q=trim($_GET['q']??'');
if($q!==''){
  $like='%'.$q.'%';
  $st=$pdo->prepare("SELECT r.*,u.name AS uname,u.email FROM recharges r
    LEFT JOIN users u ON u.id=r.user_id
    WHERE r.txn_id LIKE ? OR r.sender_number LIKE ? OR u.email LIKE ? OR u.phone LIKE ? OR u.name LIKE ?
    ORDER BY r.status='pending' DESC, r.id DESC LIMIT 300");
  $st->execute([$like,$like,$like,$like,$like]);
  $rows=$st->fetchAll();
}else{
  $rows=$pdo->query("SELECT r.*,u.name AS uname,u.email FROM recharges r LEFT JOIN users u ON u.id=r.user_id ORDER BY r.status='pending' DESC, r.id DESC")->fetchAll();
}
?>
<h2 style="margin-bottom:14px">💰 রিচার্জ রিকোয়েস্ট</h2>
<form method="get" style="margin-bottom:14px;display:flex;gap:8px;flex-wrap:wrap">
  <input class="input" name="q" value="<?=e($q)?>" placeholder="🔍 ট্রানজেকশন আইডি, ইমেইল বা ফোন নাম্বার দিয়ে সার্চ করুন" style="flex:1;min-width:240px">
  <button class="btn">সার্চ</button>
  <?php if($q!==''):?><a class="btn" style="background:#64748b" href="recharges.php">✕ Clear</a><?php endif;?>
</form>
<?php if($q!==''):?><p style="color:#64748b;font-size:13px;margin-bottom:8px">"<?=e($q)?>" — <?=count($rows)?> টি রেজাল্ট</p><?php endif;?>
<table><thead><tr><th>#</th><th>Date</th><th>User</th><th>Method</th><th>Amount</th><th>TxnID</th><th>Sender</th><th>Status</th><th>Action</th></tr></thead><tbody>
<?php foreach($rows as $r):?>
<tr><td>#<?=$r['id']?></td><td><?=e($r['created_at'])?></td>
<td><?=e($r['uname'])?><br><small><?=e($r['email'])?></small></td>
<td><?=e($r['method'])?></td><td><b><?=money($r['amount'])?></b></td>
<td><?=e($r['txn_id'])?></td><td><?=e($r['sender_number'])?></td>
<td><span class="badge <?=$r['status']==='approved'?'ok':($r['status']==='rejected'?'fail':'pending')?>"><?=e($r['status'])?></span></td>
<td>
<?php if($r['status']==='pending'):?>
<form method="post" style="display:flex;gap:4px"><?=csrf_field()?><input type="hidden" name="id" value="<?=$r['id']?>">
<input class="input" name="note" placeholder="Note" style="width:100px;padding:4px 8px">
<button class="btn sm success" name="act" value="approve">✓</button>
<button class="btn sm danger" name="act" value="reject">✗</button>
</form>
<?php else:?><?=e($r['admin_note'])?><?php endif;?>
</td></tr>
<?php endforeach;?>
<?php if(!$rows):?><tr><td colspan="9" style="text-align:center;padding:20px;color:#64748b">কোন রিচার্জ পাওয়া যায়নি</td></tr><?php endif;?>
</tbody></table>
<?php require __DIR__.'/_footer.php';
