<?php
require_once __DIR__.'/../includes/functions.php';
require_once __DIR__.'/../includes/v292_admin_portal_helpers.php';
$admin=require_admin(); mj292_ensure_schema();
$adminTitle='Seller Performance'; $adminSubtitle='See seller listings, views, enquiries, wallet usage and paid service value.';
$rows=[];
try { if(table_exists('mj_users')) { $rows=all_rows("SELECT u.id,u.name,u.email,u.created_at,
  (SELECT COUNT(*) FROM mj_listings l WHERE l.user_id=u.id) listing_count,
  (SELECT COALESCE(SUM(COALESCE(l.views,0)),0) FROM mj_listings l WHERE l.user_id=u.id) total_views,
  (SELECT COUNT(*) FROM mj_companies c WHERE c.user_id=u.id) company_count,
  (SELECT COALESCE(SUM(amount),0) FROM mj_wallet_transactions wt WHERE wt.user_id=u.id AND wt.direction='debit') wallet_spent
  FROM mj_users u ORDER BY listing_count DESC, total_views DESC, u.id DESC LIMIT 100"); } } catch(Throwable $e) { $rows=[]; }
include __DIR__.'/_header.php';
?>
<div class="card mj-v292-card"><div class="card-header"><h3 class="card-title">Top Sellers</h3></div><div class="table-responsive"><table class="table table-hover mb-0"><thead><tr><th>User</th><th class="text-right">Listings</th><th class="text-right">Companies</th><th class="text-right">Views</th><th class="text-right">Wallet Spent</th><th>Action</th></tr></thead><tbody><?php foreach($rows as $r): ?><tr><td><b><?=e($r['name']??'User')?></b><br><small><?=e($r['email']??'')?></small></td><td class="text-right"><?=e($r['listing_count'])?></td><td class="text-right"><?=e($r['company_count'])?></td><td class="text-right"><?=e($r['total_views'])?></td><td class="text-right"><?=mj292_money0($r['wallet_spent']??0,'SGD')?></td><td><a class="btn btn-xs btn-outline-primary" href="user-view.php?id=<?=e($r['id'])?>">User 360</a></td></tr><?php endforeach; if(!$rows): ?><tr><td colspan="6" class="text-center text-muted p-4">No seller records.</td></tr><?php endif; ?></tbody></table></div></div>
<?php include __DIR__.'/_footer.php'; ?>
