<?php
// --- Comptage des statistiques globales ---
try {
  // 1️⃣ Nombre total d’annonces visibles pour cet admin
  $where = "";
  $params = [];

  if ($_SESSION['admin']['countries'] != 'all') {
    $countries = explode(',', $_SESSION['admin']['countries']);
    $in = str_repeat('?,', count($countries) - 1) . '?';
    $where .= " AND pays IN ($in)";
    $params = array_merge($params, $countries);
  }

  if ($_SESSION['admin']['cities'] != 'all') {
    $cities = explode(',', $_SESSION['admin']['cities']);
    $in = str_repeat('?,', count($cities) - 1) . '?';
    $where .= " AND ville IN ($in)";
    $params = array_merge($params, $cities);
  }

  $stmt = $dbh->prepare("SELECT COUNT(*) FROM mondialisa_annonces WHERE 1=1 $where");
  $stmt->execute($params);
  $totalAnnonces = (int) $stmt->fetchColumn();

  // 2️⃣ Nombre total d’utilisateurs
  $stmt = $dbh->prepare("SELECT COUNT(*) FROM mondialisa_users");
  $stmt->execute();
  $totalUsers = (int) $stmt->fetchColumn();

  // 3️⃣ Nombre de pays distincts dans les annonces
  $stmt = $dbh->prepare("SELECT COUNT(DISTINCT pays) FROM mondialisa_annonces");
  $stmt->execute();
  $totalPays = (int) $stmt->fetchColumn();

  // 4️⃣ Nombre de villes distinctes dans les annonces
  $stmt = $dbh->prepare("SELECT COUNT(DISTINCT ville) FROM mondialisa_annonces");
  $stmt->execute();
  $totalVilles = (int) $stmt->fetchColumn();

} catch (PDOException $e) {
  $totalAnnonces = $totalUsers = $totalPays = $totalVilles = 0;
}
?>
<div class="pt-3 pb-2 mb-3 border-bottom">
    <h2>Hello <?= htmlspecialchars($_SESSION['admin']['nom']) ?></h2>
    <p>Role : <?= htmlspecialchars($_SESSION['admin']['role']) ?></p>
  </div>

  <div class="row g-3">
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>🌍 Classifieds </h5>
        <p class="display-6 text-primary"><?= $totalAnnonces ?></p>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>👤 Users </h5>
        <p class="display-6 text-success"><?= $totalUsers ?></p>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>🗺️ Country</h5>
        <p class="display-6 text-warning"><?= $totalPays ?></p>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>🏙️ Cities</h5>
        <p class="display-6 text-danger"><?= $totalVilles ?></p>
      </div>
    </div>
  </div>

  <div class="row g-3">
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>🌍 Classifieds </h5>
        <p class="display-6 text-primary"><?= $totalAnnonces ?></p>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>👤 Users </h5>
        <p class="display-6 text-success"><?= $totalUsers ?></p>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>🗺️ Country</h5>
        <p class="display-6 text-warning"><?= $totalPays ?></p>
      </div>
    </div>
    <div class="col-md-3">
      <div class="card shadow-sm p-3 text-center">
        <h5>🏙️ Cities</h5>
        <p class="display-6 text-danger"><?= $totalVilles ?></p>
      </div>
    </div>
  </div>