PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]); $db->exec("SET search_path TO admin,public"); }catch(Exception $e){echo json_encode(["ok"=>false,"error"=>"db"]);exit;} if($action==="status"){ $t=(int)$db->query("SELECT COUNT(*) FROM office_accounts")->fetchColumn(); $a=(int)$db->query("SELECT COUNT(*) FROM office_accounts WHERE LOWER(status)='active'")->fetchColumn(); $w=(int)$db->query("SELECT COUNT(*) FROM office_accounts WHERE LOWER(status)='warming'")->fetchColumn(); $s=(int)$db->query("SELECT COUNT(*) FROM office_accounts WHERE LOWER(status) IN ('suspended','blocked')")->fetchColumn(); $snd=(int)$db->query("SELECT COUNT(*) FROM graph_send_log")->fetchColumn(); $exc=(int)$db->query("SELECT COUNT(*) FROM office_accounts WHERE exchange_configured=true")->fetchColumn(); $ten=(int)$db->query("SELECT COUNT(*) FROM graph_tenants")->fetchColumn(); echo json_encode(["ok"=>true,"total"=>$t,"active"=>$a,"warming_count"=>$w,"warming"=>$w,"suspended"=>$s,"sends"=>$snd,"exchange"=>$exc,"tenants"=>$ten,"accounts_warming"=>$w,"health"=>$t>0?round($a/$t*100)."%":"0%"]); }elseif($action==="accounts"){ $p=max(1,(int)($_GET["p"]??1));$pp=50;$off=($p-1)*$pp; $q=$_GET["q"]??""; $wh=$q?"WHERE (name ILIKE '%".addslashes($q)."%' OR tenant_domain ILIKE '%".addslashes($q)."%')":""; $rows=$db->query("SELECT id,name,tenant_domain,LOWER(status) as status,current_step,exchange_configured,domains_count FROM office_accounts $wh ORDER BY id DESC LIMIT $pp OFFSET $off")->fetchAll(); $cnt=(int)$db->query("SELECT COUNT(*) FROM office_accounts $wh")->fetchColumn(); echo json_encode(["ok"=>true,"accounts"=>$rows,"total"=>$cnt,"page"=>$p,"pages"=>max(1,ceil($cnt/$pp))]); }elseif($action==="tenants"){ $rows=$db->query("SELECT tenant_domain,status,users_count,sends_today,daily_limit FROM graph_tenants ORDER BY users_count DESC LIMIT 20")->fetchAll(); echo json_encode(["ok"=>true,"tenants"=>$rows]); }else{ echo json_encode(["ok"=>true,"actions"=>["status","accounts","tenants"]]); }