<?php
require_once __DIR__ . '/includes/auth.php';
require_admin();

$counts = [
    'properties' => (int)db()->query('SELECT COUNT(*) FROM properties')->fetchColumn(),
    'new_leads' => (int)db()->query("SELECT COUNT(*) FROM leads WHERE status='new'")->fetchColumn(),
    'vip_subscribers' => (int)db()->query('SELECT COUNT(*) FROM vip_alerts')->fetchColumn(),
    'blog_posts' => (int)db()->query('SELECT COUNT(*) FROM blog_posts')->fetchColumn(),
    'newsletter' => (int)db()->query('SELECT COUNT(*) FROM newsletter_subscribers')->fetchColumn(),
    'procurement' => (int)db()->query('SELECT COUNT(*) FROM procurement_notices')->fetchColumn(),
    'developments' => (int)db()->query('SELECT COUNT(*) FROM developments')->fetchColumn(),
];
$recentLeads = db()->query('SELECT * FROM leads ORDER BY created_at DESC LIMIT 6')->fetchAll();

$pageTitle = 'Dashboard';
include __DIR__ . '/includes/layout-header.php';
$errorFlash = flash_get('error');
?>
<?php if ($errorFlash): ?><div class="form-error mb-6"><?= e($errorFlash) ?></div><?php endif; ?>
<div class="grid mb-8" style="grid-template-columns:repeat(5,1fr);">
  <div class="admin-card"><div class="text-xs text-muted uppercase mb-2">Properties</div><div style="font-size:var(--fs-2xl);font-family:var(--font-heading);"><?= $counts['properties'] ?></div><a href="/admin/properties.php" class="btn-ghost text-xs">Manage →</a></div>
  <div class="admin-card"><div class="text-xs text-muted uppercase mb-2">New Leads</div><div style="font-size:var(--fs-2xl);font-family:var(--font-heading);color:var(--color-red);"><?= $counts['new_leads'] ?></div><a href="/admin/leads.php" class="btn-ghost text-xs">View Inbox →</a></div>
  <div class="admin-card"><div class="text-xs text-muted uppercase mb-2">New Developments</div><div style="font-size:var(--fs-2xl);font-family:var(--font-heading);"><?= $counts['developments'] ?></div><a href="/admin/developments.php" class="btn-ghost text-xs">Manage →</a></div>
  <div class="admin-card"><div class="text-xs text-muted uppercase mb-2">VIP Subscribers</div><div style="font-size:var(--fs-2xl);font-family:var(--font-heading);"><?= $counts['vip_subscribers'] ?></div><a href="/admin/vip-alerts.php" class="btn-ghost text-xs">View List →</a></div>
  <div class="admin-card"><div class="text-xs text-muted uppercase mb-2">Blog &amp; News Posts</div><div style="font-size:var(--fs-2xl);font-family:var(--font-heading);"><?= $counts['blog_posts'] ?></div><a href="/admin/blog.php" class="btn-ghost text-xs">Manage →</a></div>
</div>

<div class="admin-card">
  <div class="flex justify-between items-center mb-4">
    <h3 style="margin:0;">Recent Leads</h3>
    <a href="/admin/leads.php" class="btn btn-outline btn-sm">View All</a>
  </div>
  <div style="overflow-x:auto;">
  <table class="admin-table">
    <thead><tr><th>Type</th><th>Name</th><th>Contact</th><th>Message</th><th>Status</th><th>Date</th></tr></thead>
    <tbody>
      <?php foreach ($recentLeads as $l): ?>
        <tr>
          <td><span class="badge badge-outline"><?= e($l['type']) ?></span></td>
          <td><?= e($l['name']) ?></td>
          <td><?= e($l['email']) ?><br><span class="text-muted"><?= e($l['phone']) ?></span></td>
          <td><?= e(truncate($l['message'] ?? '', 60)) ?></td>
          <td><?= e($l['status']) ?></td>
          <td><?= date('M j, Y', strtotime($l['created_at'])) ?></td>
        </tr>
      <?php endforeach; ?>
      <?php if (empty($recentLeads)): ?><tr><td colspan="6" class="text-center text-muted">No leads yet.</td></tr><?php endif; ?>
    </tbody>
  </table>
  </div>
</div>
<?php include __DIR__ . '/includes/layout-footer.php'; ?>
