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

// Stats globales
$totalUsers     = $pdo->query("SELECT COUNT(*) FROM users WHERE is_admin=0")->fetchColumn();
$totalRes       = $pdo->query("SELECT COUNT(*) FROM reservations")->fetchColumn();
$activeRes      = $pdo->query("SELECT COUNT(*) FROM reservations WHERE status='confirmée'")->fetchColumn();
$avgRating      = $pdo->query("SELECT ROUND(AVG(stars),1) FROM ratings")->fetchColumn();

// Dernières réservations
$lastRes = $pdo->query("SELECT r.*, u.name as user_name, c.name as car_name, ch.name as chauffeur_name
                         FROM reservations r
                         JOIN users u ON r.user_id=u.id
                         JOIN cars c ON r.car_id=c.id
                         JOIN chauffeurs ch ON r.chauffeur_id=ch.id
                         ORDER BY r.created_at DESC LIMIT 8")->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Administration - 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;overflow-y:auto;}
      .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;overflow-x:hidden;}
      .stat-cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(160px,1fr));gap:1.2rem;margin-bottom:2.5rem;}
      .stat-card{background:rgba(255,255,255,.04);border:1px solid rgba(255,255,255,.07);border-radius:12px;padding:1.4rem;text-align:center;}
      .stat-card .num{font-family:'Cormorant Garamond',serif;font-size:2.5rem;font-weight:700;color:var(--orange);}
      .stat-card .lbl{font-size:.72rem;text-transform:uppercase;letter-spacing:.1em;color:var(--light2);margin-top:.3rem;}
      @media(max-width:768px){.admin-layout{grid-template-columns:1fr;}.admin-sidebar{height:auto;position:static;}}
    </style>
</head>
<body>
<div class="admin-layout">
  <!-- Sidebar -->
  <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" class="active">📊 Tableau de bord</a></li>
      <li><a href="reservations.php">📅 Réservations</a></li>
      <li><a href="users.php">👥 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 -->
  <main class="admin-main">
    <h1 style="font-family:'Cormorant Garamond',serif;font-size:2rem;margin-bottom:.3rem;">Tableau de bord</h1>
    <p style="color:var(--light2);font-size:.85rem;margin-bottom:2rem;">Bienvenue, <?= sanitize($_SESSION['user_name']) ?></p>

    <div class="stat-cards">
      <div class="stat-card"><div class="num"><?= $totalUsers ?></div><div class="lbl">Clients</div></div>
      <div class="stat-card"><div class="num"><?= $totalRes ?></div><div class="lbl">Réservations</div></div>
      <div class="stat-card"><div class="num" style="color:#4caf50;"><?= $activeRes ?></div><div class="lbl">Confirmées</div></div>
      <div class="stat-card"><div class="num"><?= $avgRating ?: '—' ?></div><div class="lbl">Note moyenne</div></div>
    </div>

    <h2 style="font-family:'Cormorant Garamond',serif;font-size:1.5rem;margin-bottom:1.2rem;">Dernières réservations</h2>
    <div class="reservations-table-wrapper">
      <table class="reservations-table">
        <thead>
          <tr><th>Client</th><th>Véhicule</th><th>Chauffeur</th><th>Date</th><th>Heure</th><th>Statut</th><th>Action</th></tr>
        </thead>
        <tbody>
          <?php foreach ($lastRes as $r): ?>
          <tr>
            <td><?= sanitize($r['user_name']) ?></td>
            <td><?= sanitize($r['car_name']) ?></td>
            <td><?= sanitize($r['chauffeur_name']) ?></td>
            <td><?= date('d/m/Y', strtotime($r['reservation_date'])) ?></td>
            <td><?= substr($r['reservation_time'],0,5) ?></td>
            <td><span class="status <?= $r['status'] ?>"><?= $r['status'] ?></span></td>
            <td>
              <?php if ($r['status']==='confirmée'): ?>
              <a href="reservations.php?action=terminate&id=<?= $r['id'] ?>" class="btn small btn-ghost"
                 onclick="return confirm('Marquer comme terminée ?')">✅ Terminer</a>
              <?php endif; ?>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </main>
</div>
</body>
</html>
