<?php
require_once __DIR__.'/includes/functions.php';
$pageTitle='Companies Directory';
$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); $verified=(int)($_GET['verified']??0);
$rows=mj_public_fetch_companies([
  'q'=>$q,
  'country_id'=>$country,
  'state_id'=>$state,
  'city_id'=>$city,
  'category_id'=>$cat,
  'verified'=>$verified,
  '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 ("company","service") ORDER BY name');
include __DIR__.'/includes/header.php';
?>
<section class="mj-page-hero"><div class="container"><div class="row align-items-center g-4"><div class="col-lg-7"><?php if($areaLabel && !$country && !$state && !$city): ?><span class="mj-badge warn mb-2"><i class="bi bi-geo-alt-fill"></i>Nearby first: <?=e($areaLabel)?></span><br><?php endif; ?><span class="mj-badge good mb-2"><i class="bi bi-building-check"></i>Company directory</span><h1 class="fw-bold display-5 mb-1">Find shops, restaurants, services and companies.</h1><p class="text-muted mb-0">Verified company profiles, branches, services, opening hours and quotation requests.</p></div><div class="col-lg-5"><img src="<?=asset('images/company-hero.svg')?>" class="img-fluid" alt="Company directory"></div></div></div></section>
<section class="mj-section"><div class="container">
  <form class="mj-filter-bar" method="get"><div class="row g-2"><div class="col-lg-4"><input class="form-control" name="q" value="<?=e($q)?>" placeholder="Search restaurants, IT service, clinic..."></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="verified"><option value="0">All</option><option value="1" <?=$verified?'selected':''?>>Verified only</option></select></div><div class="col-lg-2 d-grid"><button class="btn btn-primary"><i class="bi bi-search me-1"></i>Search</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)?> companies found</h2><div><a class="btn btn-outline-primary me-2" href="<?=base_url('add-company.php')?>">Add Company</a><a class="btn btn-outline-secondary" href="<?=base_url('quotation-request.php')?>">Request Quote</a></div></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 companies found</h3><p class="text-muted">Try another filter, import Business Seed or add your first company.</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/company-card.php'; ?></div><?php endforeach; ?></div><?php endif; ?>
</div></section>
<?php include __DIR__.'/includes/footer.php'; ?>
