<?php
require_once '../config.php';
require_once '../functions.php';
requireAdmin();

$action = $_GET['action'] ?? '';
$id     = (int)($_GET['id'] ?? 0);

if ($action === 'delete' && $id && $id !== (int)$_SESSION['user_id']) {
    $pdo->prepare("DELETE FROM users WHERE id=? AND is_admin=0")->execute([$id]);
    $_SESSION['flash'] = "Utilisateur supprimé.";
    redirect('users.php');
}

$users = $pdo->query("SELECT u.*, COUNT(r.id) as nb_res
                       FROM users u
                       LEFT JOIN reservations r ON u.id=r.user_id
                       WHERE u.is_admin=0
                       GROUP BY u.id
                       ORDER BY u.created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Utilisateurs – Admin First Car</title>
    <link rel="stylesheet" href="../style.css">
    <style>
      .admin-layout{display:grid;grid-template-columns:220px 1fr;min-height:100vh;}
      .admin-sidebar{background:rgba(0,0,0,.6);border-right:1px solid rgba(232,130,12,.15);padding:2rem 1.2rem;position:sticky;top:0;height:100vh;}
      .admin-sidebar .nav-logo{font-size:1.4rem;display:block;margin-bottom:.3rem;}
      .admin-sidebar .admin-badge{font-size:.65rem;text-transform:uppercase;letter-spacing:.12em;color:var(--orange);font-weight:700;margin-bottom:2rem;display:block;}
      .sidebar-nav{list-style:none;}.sidebar-nav li{margin-bottom:.3rem;}
      .sidebar-nav a{display:block;padding:.65rem 1rem;border-radius:8px;color:var(--light2);text-decoration:none;font-size:.82rem;font-weight:500;transition:all .2s;}
      .sidebar-nav a:hover,.sidebar-nav a.active{background:rgba(232,130,12,.12);color:var(--orange);}
      .admin-main{padding:2rem 2.5rem;}
    </style>
</head>
<body>
<div class="admin-layout">
  <aside class="admin-sidebar">
    <a href="../index.php" class="nav-logo">First Car<span>.</span></a>
    <span class="admin-badge">Administration</span>
    <ul class="sidebar-nav">
      <li><a href="index.php">📊 Tableau de bord</a></li>
      <li><a href="reservations.php">📅 Réservations</a></li>
      <li><a href="users.php" class="active">👥 Utilisateurs</a></li>
      <li><a href="chauffeurs.php">🎩 Chauffeurs</a></li>
      <li><a href="cars.php">🚗 Véhicules</a></li>
      <li><a href="ratings.php">⭐ Avis clients</a></li>
      <li style="margin-top:2rem;border-top:1px solid rgba(255,255,255,.06);padding-top:1rem;">
        <a href="../dashboard.php">← Espace client</a></li>
      <li><a href="../logout.php" style="color:#f44336;">Déconnexion</a></li>
    </ul>
  </aside>
  <main class="admin-main">
    <?php if (isset($_SESSION['flash'])): ?>
    <div class="alert success"><?= sanitize($_SESSION['flash']) ?></div>
    <?php unset($_SESSION['flash']); endif; ?>

    <h1 style="font-family:'Cormorant Garamond',serif;font-size:2rem;margin-bottom:1.5rem;">
      Utilisateurs <span style="font-size:1rem;color:var(--light2);font-weight:400;">(<?= count($users) ?>)</span>
    </h1>
    <div class="reservations-table-wrapper">
      <table class="reservations-table">
        <thead>
          <tr><th>ID</th><th>Nom</th><th>Email</th><th>Âge</th><th>Téléphone</th><th>Réservations</th><th>Inscrit le</th><th>Action</th></tr>
        </thead>
        <tbody>
          <?php foreach ($users as $u): ?>
          <tr>
            <td style="opacity:.5;">#<?= $u['id'] ?></td>
            <td><strong><?= sanitize($u['name']) ?></strong></td>
            <td><?= sanitize($u['email']) ?></td>
            <td><?= $u['age'] ?> ans</td>
            <td><?= sanitize($u['phone'] ?: '—') ?></td>
            <td><span style="color:var(--orange);font-weight:600;"><?= $u['nb_res'] ?></span></td>
            <td style="font-size:.8rem;"><?= date('d/m/Y', strtotime($u['created_at'])) ?></td>
            <td>
              <a href="users.php?action=delete&id=<?= $u['id'] ?>" class="btn small danger"
                 onclick="return confirm('Supprimer cet utilisateur et toutes ses réservations ?')">Supprimer</a>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </main>
</div>
</body>
</html>