<?php
require_once __DIR__.'/includes/functions.php';
$pageTitle='Country Marketplaces';
$metaDescription='Choose a country marketplace to browse local listings, companies, services, currency, timezone and city pages.';
$rows=safe_all_rows_public('SELECT * FROM mj_countries WHERE is_active=1 ORDER BY name LIMIT 260');

$countryListingCounts = [];
foreach (safe_all_rows_public('SELECT country_id, COUNT(*) c FROM mj_listings WHERE '.mj_public_listing_status_sql('mj_listings').' GROUP BY country_id') as $row) {
    $countryListingCounts[(int)$row['country_id']] = (int)$row['c'];
}
$countryCompanyCounts = [];
foreach (safe_all_rows_public('SELECT country_id, COUNT(*) c FROM mj_companies WHERE '.mj_public_company_status_sql('mj_companies').' GROUP BY country_id') as $row) {
    $countryCompanyCounts[(int)$row['country_id']] = (int)$row['c'];
}
$totalCountries = count($rows);
$totalListings = array_sum($countryListingCounts);
$totalCompanies = array_sum($countryCompanyCounts);

include __DIR__.'/includes/header.php';
?>
<section class="mj-home-hero mj-country-home-hero mj-countries-home-hero">
  <div class="container position-relative">
    <div class="row align-items-center g-5">
      <div class="col-lg-7" data-aos="fade-right">
        <div class="d-flex flex-wrap gap-2 mb-3">
          <span class="mj-home-pill is-local"><i class="bi bi-globe2 me-1"></i>Global directory</span>
          <span class="mj-home-pill"><i class="bi bi-currency-exchange me-1"></i>Local currency</span>
          <span class="mj-home-pill"><i class="bi bi-clock-history me-1"></i>Local timezone</span>
        </div>
        <h1>Country Marketplaces</h1>
        <p class="mj-home-lead">Choose a country marketplace. Each country can have its own currency, timezone, companies, listings, deals, states and city pages.</p>
        <form class="mj-home-search" method="get" action="<?=base_url('browse.php')?>">
          <div class="row g-2 align-items-center">
            <div class="col-lg-5"><div class="mj-home-input"><i class="bi bi-search"></i><input class="form-control" name="q" placeholder="Search listings or services..."></div></div>
            <div class="col-lg-5"><select class="form-select" name="country_id"><option value="">Choose country</option><?php foreach($rows as $c): ?><option value="<?=e($c['id'])?>"><?=e(($c['flag_emoji'] ?: '🌍').' '.$c['name'])?></option><?php endforeach; ?></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="mj-home-stats mj-country-stats mt-4">
          <div><b><?=e(number_format($totalCountries))?></b><span>Countries</span></div>
          <div><b><?=e(number_format($totalListings))?></b><span>Listings</span></div>
          <div><b><?=e(number_format($totalCompanies))?></b><span>Companies</span></div>
        </div>
      </div>
      <div class="col-lg-5" data-aos="fade-left">
        <div class="mj-home-hero-board">
          <div class="mj-home-window">
            <div class="mj-window-top"><span></span><span></span><span></span><b>Global</b></div>
            <img src="<?=asset('images/marketplace-hero.svg')?>" alt="Country marketplace dashboard">
            <div class="mj-home-check"><i class="bi bi-globe2"></i></div>
          </div>
          <div class="mj-home-floating-card card-a"><i class="bi bi-map"></i><b>States</b><small>Browse regions</small></div>
          <div class="mj-home-floating-card card-b"><i class="bi bi-building-check"></i><b>Companies</b><small>Country profiles</small></div>
          <div class="mj-home-floating-card card-c"><i class="bi bi-tags"></i><b>Listings</b><small>Local deals</small></div>
        </div>
      </div>
    </div>
  </div>
</section>

<section class="mj-section mj-home-category-section">
  <div class="container">
    <div class="mj-section-title mj-home-title">
      <div><span class="mj-badge mb-2"><i class="bi bi-globe2"></i>Countries</span><h2>Browse by country</h2><p class="text-muted mb-0">Same homepage card design for all country pages.</p></div>
    </div>
    <?php if(!$rows): ?>
      <div class="alert alert-info rounded-4">No active countries found. Please enable countries in admin or run sample data/migration.</div>
    <?php else: ?>
      <div class="row g-3">
        <?php foreach($rows as $c): $cid=(int)$c['id']; ?>
          <div class="col-6 col-md-4 col-xl-3">
            <a class="mj-home-country-card h-100" href="<?=base_url($c['slug'].'/')?>">
              <span class="flag"><?=e($c['flag_emoji'] ?: '🌍')?></span>
              <span class="flex-grow-1"><b><?=e($c['name'])?></b><small><?=e(($c['currency_code'] ?? 'USD').' • '.($c['timezone_default'] ?? 'UTC'))?></small><em><?=e(number_format($countryListingCounts[$cid] ?? 0))?> listings • <?=e(number_format($countryCompanyCounts[$cid] ?? 0))?> companies</em></span>
              <i class="bi bi-chevron-right"></i>
            </a>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>
<?php include __DIR__.'/includes/footer.php'; ?>
