<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/auth.php';
$leads      = readJSON(LEADS_FILE);
$gallery    = readJSON(GALLERY_FILE);
$packages   = readJSON(PACKAGES_FILE);
$sponsorship = readJSON(SPONSORSHIP_FILE);
$pageTitle  = 'Admin Dashboard | Kenya Expo';
require_once __DIR__ . '/../includes/header.php';
?>
<div class="admin-body">
  <!-- Sidebar -->
  <div class="admin-sidebar d-none d-lg-block">
    <div class="admin-sidebar-logo">
      <a href="/admin/dashboard.php">KTE Admin</a>
      <span>20th Kenya Expo</span>
    </div>
    <ul class="admin-nav">
      <li><a href="/admin/dashboard.php" class="active"><i class="fas fa-chart-line"></i> Dashboard</a></li>
      <li><a href="/admin/leads.php"><i class="fas fa-users"></i> All Leads</a></li>
      <li><a href="/admin/packages.php"><i class="fas fa-box-open"></i> Packages Manager</a></li>
      <li><a href="/admin/sponsorship.php"><i class="fas fa-crown"></i> Sponsorship Manager</a></li>
      <li><a href="/admin/gallery.php"><i class="fas fa-images"></i> Gallery Manager</a></li>
      <li><a href="/admin/export.php"><i class="fas fa-file-csv"></i> Export CSV</a></li>
      <li><a href="/admin/logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
    </ul>
  </div>

  <!-- Main Content -->
  <div class="admin-main">
    <div class="admin-header">
      <div>
        <h4 style="font-family:var(--font-heading);margin-bottom:4px;">Welcome to Admin Panel</h4>
        <p style="color:var(--text-muted);font-size:0.85rem;margin:0;">20th Kenya Expo 2026 &middot; Last login: <?php echo $_SESSION['admin_login_time'] ?? 'N/A'; ?></p>
      </div>
      <a href="/admin/logout.php" class="btn btn-outline-gold btn-sm"><i class="fas fa-sign-out-alt me-2"></i>Logout</a>
    </div>

    <!-- KPI Cards -->
    <div class="row g-3 mb-4">
      <div class="col-6 col-lg-2">
        <div class="kpi-card">
          <div class="kpi-number" id="totalLeads"><?php echo count($leads); ?></div>
          <div class="kpi-label">Total Leads</div>
        </div>
      </div>
      <div class="col-6 col-lg-2">
        <div class="kpi-card">
          <div class="kpi-number" id="exhibitorLeads"><?php echo count(array_filter($leads, fn($l) => ($l['type'] ?? '') === 'exhibitor')); ?></div>
          <div class="kpi-label">Exhibitors</div>
        </div>
      </div>
      <div class="col-6 col-lg-2">
        <div class="kpi-card">
          <div class="kpi-number" id="visitorLeads"><?php echo count(array_filter($leads, fn($l) => ($l['type'] ?? '') === 'visitor')); ?></div>
          <div class="kpi-label">Visitors</div>
        </div>
      </div>
      <div class="col-6 col-lg-2">
        <div class="kpi-card">
          <div class="kpi-number" id="sponsorLeads"><?php echo count(array_filter($leads, fn($l) => ($l['type'] ?? '') === 'sponsor')); ?></div>
          <div class="kpi-label">Sponsors</div>
        </div>
      </div>
      <div class="col-6 col-lg-2">
        <div class="kpi-card">
          <div class="kpi-number" id="totalGallery"><?php echo count($gallery); ?></div>
          <div class="kpi-label">Gallery Images</div>
        </div>
      </div>
      <div class="col-6 col-lg-2">
        <div class="kpi-card">
          <div class="kpi-number" id="totalPackages"><?php echo count(array_filter($packages, fn($p) => !empty($p['active']))); ?></div>
          <div class="kpi-label">Active Packages</div>
        </div>
      </div>
    </div>

    <!-- Quick Actions -->
    <div class="mb-4">
      <h5 style="font-family:var(--font-heading);margin-bottom:16px;">Quick Actions</h5>
      <div class="d-flex flex-wrap gap-2">
        <a href="/admin/export.php" class="btn btn-gold btn-sm"><i class="fas fa-file-csv me-2"></i>Export CSV</a>
        <a href="/admin/leads.php" class="btn btn-outline-gold btn-sm"><i class="fas fa-users me-2"></i>View Leads</a>
        <a href="/admin/packages.php" class="btn btn-outline-gold btn-sm"><i class="fas fa-box-open me-2"></i>Packages</a>
        <a href="/admin/sponsorship.php" class="btn btn-outline-gold btn-sm"><i class="fas fa-crown me-2"></i>Sponsorship</a>
        <a href="/admin/gallery.php" class="btn btn-outline-gold btn-sm"><i class="fas fa-images me-2"></i>Gallery</a>
      </div>
    </div>

    <!-- Event Countdown -->
    <div class="glass-card mb-4 text-center" style="padding:28px;">
      <div style="font-family:var(--font-stats);font-size:0.9rem;letter-spacing:3px;color:var(--secondary);margin-bottom:8px;">COUNTDOWN TO EVENT</div>
      <h3 style="font-family:var(--font-heading);margin-bottom:8px;">Days Until Kenya Expo 2026</h3>
      <div style="font-family:var(--font-stats);font-size:4rem;color:var(--secondary);line-height:1;" id="countdownDays">--</div>
      <div style="color:var(--text-muted);font-size:0.85rem;margin-top:8px;">November 19, 2026</div>
    </div>

    <!-- Recent Leads -->
    <div class="mb-4">
      <div class="d-flex align-items-center justify-content-between mb-3">
        <h5 style="font-family:var(--font-heading);margin:0;">Recent Leads</h5>
        <a href="/admin/leads.php" class="btn btn-sm btn-outline-gold">View All</a>
      </div>
      <div class="table-responsive">
        <table class="admin-table">
          <thead>
            <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Email</th>
              <th>Type</th>
              <th>Country</th>
              <th>Date</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $recent = array_slice(array_reverse($leads), 0, 10);
            if (empty($recent)): ?>
            <tr><td colspan="6" class="text-center" style="color:var(--text-muted);">No leads yet</td></tr>
            <?php else:
              foreach ($recent as $lead): ?>
            <tr>
              <td><code style="font-size:0.75rem;"><?php echo htmlspecialchars($lead['id'] ?? ''); ?></code></td>
              <td><?php echo htmlspecialchars($lead['name'] ?? ''); ?></td>
              <td><?php echo htmlspecialchars($lead['email'] ?? ''); ?></td>
              <td><span class="badge" style="background:rgba(212,175,55,0.15);color:var(--secondary);font-size:0.7rem;padding:4px 10px;border-radius:10px;"><?php echo ucfirst(htmlspecialchars($lead['type'] ?? '')); ?></span></td>
              <td><?php echo htmlspecialchars($lead['country'] ?? ''); ?></td>
              <td style="font-size:0.8rem;color:var(--text-muted);"><?php echo htmlspecialchars($lead['submitted_at'] ?? ''); ?></td>
            </tr>
            <?php endforeach; endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<script>initCountdown('2026-11-19T00:00:00', 'countdownDays');</script>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
