<?php $PAGE_TITLE='Customers'; require __DIR__.'/_layout.php';
if($_SERVER['REQUEST_METHOD']==='POST'){csrf_check();
  $uid=(int)$_POST['uid']; $act=$_POST['act']??'';
  if($act==='addbal'){$a=(float)$_POST['amt'];
    $pdo->prepare("UPDATE users SET balance=balance+?,total_recharge=total_recharge+? WHERE id=?")->execute([$a,$a,$uid]);
    log_txn($pdo,$uid,'credit',$a,'admin','Admin credit'); }
  if($act==='sub'){$a=(float)$_POST['amt'];
    $pdo->prepare("UPDATE users SET balance=balance-? WHERE id=?")->execute([$a,$uid]);
    log_txn($pdo,$uid,'debit',$a,'admin','Admin debit'); }
  if($act==='ban') $pdo->prepare("UPDATE users SET status='banned' WHERE id=?")->execute([$uid]);
  if($act==='unban') $pdo->prepare("UPDATE users SET status='active' WHERE id=?")->execute([$uid]);
  redirect('customers.php'.(isset($_POST['q'])&&$_POST['q']!==''?('?q='.urlencode($_POST['q'])):''));
}
$q = trim($_GET['q'] ?? '');
if($q!==''){
  $st=$pdo->prepare("SELECT * FROM users WHERE role='user' AND (email LIKE ? OR name LIKE ? OR phone LIKE ?) ORDER BY id DESC");
  $like='%'.$q.'%'; $st->execute([$like,$like,$like]); $rows=$st->fetchAll();
}else{
  $rows=$pdo->query("SELECT * FROM users WHERE role='user' ORDER BY id DESC")->fetchAll();
}
?>
<h2 style="margin-bottom:14px">👥 কাস্টমার</h2>
<form method="get" style="display:flex;gap:8px;margin-bottom:14px;max-width:480px">
  <input class="input" name="q" value="<?=e($q)?>" placeholder="🔎 Email / নাম / ফোন দিয়ে সার্চ করুন">
  <button class="btn">সার্চ</button>
  <?php if($q!==''):?><a class="btn secondary" href="customers.php">Clear</a><?php endif;?>
</form>
<?php if($q!==''):?><p style="color:#64748b;margin-bottom:10px">Result: <b><?=count($rows)?></b> কাস্টমার</p><?php endif;?>
<table><thead><tr><th>#</th><th>নাম</th><th>Email</th><th>Phone</th><th>Balance</th><th>Total Recharge</th><th>Status</th><th>Actions</th></tr></thead><tbody>
<?php foreach($rows as $r):?>
<tr><td>#<?=$r['id']?></td><td><?=e($r['name'])?></td><td><?=e($r['email'])?></td><td><?=e($r['phone'])?></td>
<td><b><?=money($r['balance'])?></b></td><td><?=money($r['total_recharge'])?></td>
<td><span class="badge <?=$r['status']==='active'?'ok':'fail'?>"><?=e($r['status'])?></span></td>
<td>
<form method="post" style="display:inline-flex;gap:4px"><?=csrf_field()?>
<input type="hidden" name="uid" value="<?=$r['id']?>">
<input type="hidden" name="q" value="<?=e($q)?>">
<input class="input" style="width:80px;padding:4px 8px" name="amt" type="number" step="0.01" placeholder="Amt">
<button class="btn sm success" name="act" value="addbal">+</button>
<button class="btn sm danger" name="act" value="sub">-</button>
<?php if($r['status']==='active'):?><button class="btn sm danger" name="act" value="ban" onclick="return confirm('Ban?')">🚫</button>
<?php else:?><button class="btn sm success" name="act" value="unban">✓</button><?php endif;?>
</form>
</td></tr>
<?php endforeach;?>
<?php if(!$rows):?><tr><td colspan="8" style="text-align:center;color:#94a3b8;padding:20px">কোনো কাস্টমার পাওয়া যায়নি</td></tr><?php endif;?>
</tbody></table>
<?php require __DIR__.'/_footer.php';
