<?php
$page_title = 'Dashboard';
require_once 'header.php';

$stats = [];
if ($pdo) {
    $stats['news'] = (int)$pdo->query("SELECT COUNT(*) FROM news")->fetchColumn();
    $stats['gallery'] = (int)$pdo->query("SELECT COUNT(*) FROM gallery")->fetchColumn();
    $stats['testimonials'] = (int)$pdo->query("SELECT COUNT(*) FROM testimonials")->fetchColumn();
    $stats['messages'] = (int)$pdo->query("SELECT COUNT(*) FROM contact_messages")->fetchColumn();
    $stats['unread'] = (int)$pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read=0")->fetchColumn();
}

$recentMessages = [];
if ($pdo) {
    $stmt = $pdo->query("SELECT id, name, email, subject, is_read, created_at FROM contact_messages ORDER BY created_at DESC LIMIT 5");
    $recentMessages = $stmt->fetchAll();
}
?>

<h1>Dashboard</h1>
<p class="subtitle">Welcome, <?php echo htmlspecialchars($_SESSION['admin_username'] ?? 'Admin'); ?>!</p>

<div class="row g-4 mb-5">
  <div class="col-6 col-md-4 col-lg-2">
    <div class="stat-card text-center">
      <div class="number"><?php echo $stats['news'] ?? 0; ?></div>
      <div class="label">News Articles</div>
    </div>
  </div>
  <div class="col-6 col-md-4 col-lg-2">
    <div class="stat-card text-center">
      <div class="number"><?php echo $stats['gallery'] ?? 0; ?></div>
      <div class="label">Gallery Images</div>
    </div>
  </div>
  <div class="col-6 col-md-4 col-lg-2">
    <div class="stat-card text-center">
      <div class="number"><?php echo $stats['testimonials'] ?? 0; ?></div>
      <div class="label">Testimonials</div>
    </div>
  </div>
  <div class="col-6 col-md-4 col-lg-2">
    <div class="stat-card text-center">
      <div class="number"><?php echo $stats['unread'] ?? 0; ?></div>
      <div class="label">Unread Messages</div>
    </div>
  </div>
  <div class="col-6 col-md-4 col-lg-2">
    <div class="stat-card text-center">
      <div class="number"><?php echo $stats['messages'] ?? 0; ?></div>
      <div class="label">Total Messages</div>
    </div>
  </div>
</div>

<div class="card-admin">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0">Recent Messages</h5>
    <a href="messages.php" class="btn btn-sm btn-outline-primary">View All</a>
  </div>
  <?php if (!empty($recentMessages)): ?>
  <div class="table-responsive">
    <table class="table table-admin mb-0">
      <thead>
        <tr>
          <th>Name</th>
          <th>Email</th>
          <th>Subject</th>
          <th>Date</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($recentMessages as $m): ?>
        <tr>
          <td><?php echo htmlspecialchars($m['name']); ?></td>
          <td><?php echo htmlspecialchars($m['email']); ?></td>
          <td><?php echo htmlspecialchars($m['subject'] ?: '—'); ?></td>
          <td><?php echo date('j M Y', strtotime($m['created_at'])); ?></td>
          <td><?php echo $m['is_read'] ? '<span class="badge bg-success">Read</span>' : '<span class="badge bg-warning text-dark">New</span>'; ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <?php else: ?>
  <p class="text-muted mb-0">No messages yet.</p>
  <?php endif; ?>
</div>

  </main>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
