<?php require_once __DIR__.'/_header.php';
if($_SERVER['REQUEST_METHOD']==='POST'){
    csrf_check();
    if(($_POST['action']??'')==='activate_listings'){
        q("UPDATE mj_listings SET status='active', published_at=COALESCE(published_at,NOW()) WHERE status IN ('pending','approved','published')");
        flash('success','Pending listings are now visible on frontend.'); redirect('visibility-check.php');
    }
    if(($_POST['action']??'')==='activate_companies'){
        q("UPDATE mj_companies SET status='active' WHERE status IN ('pending','approved','published')");
        flash('success','Pending companies are now visible on frontend.'); redirect('visibility-check.php');
    }
}
$listingStatus=safe_all_rows_public("SELECT status, COUNT(*) total FROM mj_listings GROUP BY status ORDER BY status");
$companyStatus=safe_all_rows_public("SELECT status, COUNT(*) total FROM mj_companies GROUP BY status ORDER BY status");
$listingCountry=safe_all_rows_public("SELECT c.name country, COUNT(l.id) total FROM mj_listings l LEFT JOIN mj_countries c ON c.id=l.country_id GROUP BY l.country_id ORDER BY total DESC LIMIT 20");
$companyCountry=safe_all_rows_public("SELECT c.name country, COUNT(co.id) total FROM mj_companies co LEFT JOIN mj_countries c ON c.id=co.country_id GROUP BY co.country_id ORDER BY total DESC LIMIT 20");
?>
<div class="card card-primary card-outline"><div class="card-header"><h3 class="card-title"><i class="fas fa-eye mr-2"></i>Frontend Visibility Check</h3></div><div class="card-body">
<div class="alert alert-info">If records exist in admin but not public website, check status and country/state/city filters here. Public frontend now shows active and pending records, with fallback if area filters are too strict.</div>
<div class="row">
  <div class="col-md-6"><div class="card"><div class="card-header"><b>Listing Status</b></div><div class="card-body"><table class="table table-sm"><tbody><?php foreach($listingStatus as $r): ?><tr><td><?=e($r['status'] ?: 'empty')?></td><td><?=e($r['total'])?></td></tr><?php endforeach; ?></tbody></table><form method="post"><?=csrf_input()?><button class="btn btn-success" name="action" value="activate_listings">Make Pending Listings Active</button></form></div></div></div>
  <div class="col-md-6"><div class="card"><div class="card-header"><b>Company Status</b></div><div class="card-body"><table class="table table-sm"><tbody><?php foreach($companyStatus as $r): ?><tr><td><?=e($r['status'] ?: 'empty')?></td><td><?=e($r['total'])?></td></tr><?php endforeach; ?></tbody></table><form method="post"><?=csrf_input()?><button class="btn btn-success" name="action" value="activate_companies">Make Pending Companies Active</button></form></div></div></div>
</div>
<div class="row">
  <div class="col-md-6"><div class="card"><div class="card-header"><b>Listings by Country</b></div><div class="card-body"><table class="table table-sm"><tbody><?php foreach($listingCountry as $r): ?><tr><td><?=e($r['country'] ?: 'No country')?></td><td><?=e($r['total'])?></td></tr><?php endforeach; ?></tbody></table></div></div></div>
  <div class="col-md-6"><div class="card"><div class="card-header"><b>Companies by Country</b></div><div class="card-body"><table class="table table-sm"><tbody><?php foreach($companyCountry as $r): ?><tr><td><?=e($r['country'] ?: 'No country')?></td><td><?=e($r['total'])?></td></tr><?php endforeach; ?></tbody></table></div></div></div>
</div>
<div class="mt-3"><a class="btn btn-primary" target="_blank" href="../browse.php">Open Browse</a> <a class="btn btn-primary" target="_blank" href="../companies.php">Open Companies</a> <a class="btn btn-secondary" target="_blank" href="../singapore/">Open Singapore</a></div>
</div></div>
<?php require_once __DIR__.'/_footer.php'; ?>
