<?php
require_once("_guard.php");
$search=trim($_GET['search']??'');
$sql="SELECT u.id,u.full_name,u.email,u.balance,u.demo_balance,u.role,u.created_at,COALESCE(v.status,'not_submitted') verification,COALESCE(t.net_pl,0) net_pl FROM users u LEFT JOIN user_verifications v ON v.user_id=u.id LEFT JOIN (SELECT user_id,SUM(net_pl) net_pl FROM trade_sessions GROUP BY user_id) t ON t.user_id=u.id";
if($search!==''){ $term='%'.$conn->real_escape_string($search).'%'; $sql.=" WHERE u.full_name LIKE '$term' OR u.email LIKE '$term'"; }
$sql.=" ORDER BY u.created_at DESC LIMIT 100"; $users=[]; $r=$conn->query($sql); while($r && ($x=$r->fetch_assoc())) $users[]=$x;
echo json_encode(["success"=>true,"users"=>$users]);
?>
