<?php
require_once __DIR__.'/../includes/functions.php';
$admin=require_admin(); require_once __DIR__.'/../includes/mj_v3_helpers.php'; mj_v300_ensure_schema(); $adminTitle='Deal Limit Report'; $rows=[];
try{ $rows=all_rows('SELECT u.id,u.name,u.email,ul.max_deals, (SELECT COUNT(*) FROM mj_deals d WHERE d.user_id=u.id AND d.status NOT IN ("deleted","rejected")) used_deals, (SELECT COUNT(*) FROM mj_deals d WHERE d.user_id=u.id AND d.is_featured=1 AND (d.featured_until IS NULL OR d.featured_until>=NOW())) featured_deals, (SELECT COUNT(*) FROM mj_deals d WHERE d.user_id=u.id AND d.is_sponsored=1 AND (d.sponsored_until IS NULL OR d.sponsored_until>=NOW())) sponsored_deals FROM mj_users u LEFT JOIN mj_user_limits ul ON ul.user_id=u.id ORDER BY used_deals DESC, u.id DESC LIMIT 300'); }catch(Throwable $e){ $rows=[]; }
include __DIR__.'/_header.php'; ?>
<div class="card border-0 shadow-sm rounded-lg"><div class="card-header border-0 d-flex justify-content-between align-items-center"><h3 class="card-title font-weight-bold">User deal limits</h3><a class="btn btn-sm btn-primary rounded-pill" href="deal-upgrade-settings.php">Deal Upgrade Settings</a></div><div class="card-body table-responsive p-0"><table class="table table-hover align-middle mb-0"><thead><tr><th>User</th><th>Email</th><th>Used / Allowed</th><th>Remaining</th><th>Featured</th><th>Sponsored</th><th>Action</th></tr></thead><tbody><?php foreach($rows as $r): $max=(int)($r['max_deals'] ?? setting('default_max_deals_per_user','3')); $used=(int)$r['used_deals']; ?><tr><td><b><?=e($r['name'])?></b><br><small>#<?=e($r['id'])?></small></td><td><?=e($r['email'])?></td><td><span class="badge badge-<?=($max>0 && $used>=$max)?'danger':'success'?>"><?=e($used)?> / <?=e($max)?></span></td><td><?=e(max(0,$max-$used))?></td><td><?=e($r['featured_deals'])?></td><td><?=e($r['sponsored_deals'])?></td><td><a class="btn btn-sm btn-outline-primary" href="user-limits.php?user_id=<?=e($r['id'])?>">Edit limits</a></td></tr><?php endforeach; ?><?php if(!$rows): ?><tr><td colspan="7" class="text-center text-muted py-4">No users found.</td></tr><?php endif; ?></tbody></table></div></div>
<?php include __DIR__.'/_footer.php'; ?>
