<?php
require_once __DIR__.'/includes/functions.php';
$pageTitle='Browse Listings';
$q=trim((string)($_GET['q']??''));
$area=visitor_area(); $areaLabel=visitor_area_label(); $softArea=mj_soft_area_mode();
$countrySlug = trim((string)($_GET['country'] ?? ''));
$countryRow = $countrySlug !== '' ? country_by_slug($countrySlug) : null;
$country=(int)($_GET['country_id']??($countryRow['id'] ?? 0));
$state=(int)($_GET['state_id']??0); $city=(int)($_GET['city_id']??0); $cat=(int)($_GET['category_id']??0); $type=trim((string)($_GET['type']??''));
$companyIdFilter=(int)($_GET['company_id']??0);
$rows=mj_public_fetch_listings([
  'q'=>$q,
  'country_id'=>$country,
  'state_id'=>$state,
  'city_id'=>$city,
  'category_id'=>$cat,
  'type'=>$type,
  'company_id'=>$companyIdFilter,
  'soft_area'=>$softArea,
  'area'=>$area
], 60);
$countries=safe_all_rows_public('SELECT id,name,flag_emoji FROM mj_countries WHERE is_active=1 ORDER BY name LIMIT 260');
$cats=safe_all_rows_public('SELECT id,name FROM mj_categories WHERE is_active=1 AND type IN ("marketplace","service","vehicle","property","job","course","event") ORDER BY name');
include __DIR__.'/includes/header.php';
?>
<section class="mj-page-hero"><div class="container"><?php if($areaLabel && !$country && !$state && !$city): ?><span class="mj-badge warn mb-2"><i class="bi bi-geo-alt-fill"></i>Area priority: <?=e($areaLabel)?></span><br><?php endif; ?><span class="mj-badge mb-2"><i class="bi bi-search"></i>Marketplace search</span><h1 class="fw-bold display-5 mb-1">Browse listings</h1><p class="text-muted mb-0">Filter by country, category, type and keyword.</p></div></section>
<section class="mj-section"><div class="container">
  <form class="mj-filter-bar" method="get"><?php if($companyIdFilter): ?><input type="hidden" name="company_id" value="<?=e($companyIdFilter)?>"><?php endif; ?><div class="row g-2"><div class="col-lg-4"><input class="form-control" name="q" value="<?=e($q)?>" placeholder="Search keyword"></div><div class="col-lg-2"><select class="form-select" name="country_id"><option value="">All countries</option><?php foreach($countries as $c): ?><option value="<?=e($c['id'])?>" <?=$country==$c['id']?'selected':''?>><?=e(($c['flag_emoji'] ?? '').' '.$c['name'])?></option><?php endforeach; ?></select></div><div class="col-lg-2"><select class="form-select" name="category_id"><option value="">All categories</option><?php foreach($cats as $c): ?><option value="<?=e($c['id'])?>" <?=$cat==$c['id']?'selected':''?>><?=e($c['name'])?></option><?php endforeach; ?></select></div><div class="col-lg-2"><select class="form-select" name="type"><option value="">All types</option><?php foreach(['product','service','rental','auction','job','property','vehicle','course','event'] as $t): ?><option value="<?=$t?>" <?=$type===$t?'selected':''?>><?=ucfirst($t)?></option><?php endforeach; ?></select></div><div class="col-lg-2 d-grid"><button class="btn btn-primary"><i class="bi bi-funnel me-1"></i>Filter</button></div></div></form>
  <div class="d-flex justify-content-between align-items-center mb-3"><h2 class="h4 fw-bold mb-0"><?=count($rows)?> listings found</h2><a class="btn btn-outline-primary" href="<?=base_url('post-listing.php')?>"><i class="bi bi-plus-circle me-1"></i>Post Listing</a></div>
  <?php if(!$rows): ?><div class="text-center py-5"><img src="<?=asset('images/empty-state.svg')?>" class="img-fluid" style="max-width:420px"><h3 class="fw-bold mt-3">No listings found</h3><p class="text-muted">Try another filter or import sample data from admin.</p></div><?php else: ?><div class="row g-4"><?php foreach($rows as $r): ?><div class="col-md-6 col-xl-3"><?php include __DIR__.'/partials/listing-card.php'; ?></div><?php endforeach; ?></div><?php endif; ?>
</div></section>
<?php include __DIR__.'/includes/footer.php'; ?>
