<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/auth.php';
$leads = readJSON(LEADS_FILE);
$pageTitle = 'All Leads | KTE Admin';
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($pageTitle); ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700;900&family=DM+Sans:wght@300;400;500&family=Bebas+Neue&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="/assets/css/style.css">
</head>
<body class="admin-body">
<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"><i class="fas fa-chart-line"></i> Dashboard</a></li>
    <li><a href="/admin/leads.php" class="active"><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>
<div class="admin-main">
  <div class="admin-header">
    <div><h4 style="font-family:var(--font-heading);margin-bottom:4px;">All Leads</h4><p style="color:var(--text-muted);font-size:0.85rem;margin:0;"><?php echo count($leads); ?> total submissions</p></div>
    <a href="/admin/dashboard.php" class="btn btn-outline-gold btn-sm"><i class="fas fa-arrow-left me-2"></i>Dashboard</a>
  </div>

  <div class="d-flex flex-wrap gap-3 mb-3 align-items-center">
    <input type="text" id="leadsSearch" class="admin-search" placeholder="Search by name, email, company, type, country...">
    <div class="admin-filter-tabs">
      <button class="admin-filter-tab active" data-filter="all">All (<?php echo count($leads); ?>)</button>
      <button class="admin-filter-tab" data-filter="exhibitor">Exhibitors (<?php echo count(array_filter($leads, fn($l) => ($l['type']??'')==='exhibitor')); ?>)</button>
      <button class="admin-filter-tab" data-filter="visitor">Visitors (<?php echo count(array_filter($leads, fn($l) => ($l['type']??'')==='visitor')); ?>)</button>
      <button class="admin-filter-tab" data-filter="sponsor">Sponsors (<?php echo count(array_filter($leads, fn($l) => ($l['type']??'')==='sponsor')); ?>)</button>
      <button class="admin-filter-tab" data-filter="newsletter">Newsletter (<?php echo count(array_filter($leads, fn($l) => ($l['type']??'')==='newsletter')); ?>)</button>
      <button class="admin-filter-tab" data-filter="contact">Contact (<?php echo count(array_filter($leads, fn($l) => ($l['type']??'')==='contact')); ?>)</button>
    </div>
  </div>

  <div class="table-responsive admin-leads-table">
    <table class="admin-table">
      <thead><tr><th>ID</th><th>Name</th><th>Email</th><th>Company</th><th>Phone</th><th>Country</th><th>Type</th><th>Date</th></tr></thead>
      <tbody>
        <?php
        $all = array_reverse($leads);
        if (empty($all)): ?>
        <tr><td colspan="8" class="text-center" style="color:var(--text-muted);">No leads submitted yet</td></tr>
        <?php else:
          foreach ($all as $lead):
        ?>
        <tr class="lead-row" data-type="<?php echo htmlspecialchars($lead['type'] ?? ''); ?>">
          <td><code style="font-size:0.7rem;"><?php echo htmlspecialchars($lead['id'] ?? ''); ?></code></td>
          <td><?php echo htmlspecialchars($lead['name'] ?? ''); ?></td>
          <td><?php echo htmlspecialchars($lead['email'] ?? ''); ?></td>
          <td><?php echo htmlspecialchars($lead['company'] ?? ''); ?></td>
          <td><?php echo htmlspecialchars($lead['phone'] ?? ''); ?></td>
          <td><?php echo htmlspecialchars($lead['country'] ?? ''); ?></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 style="font-size:0.78rem;color:var(--text-muted);"><?php echo htmlspecialchars($lead['submitted_at'] ?? ''); ?></td>
        </tr>
        <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
  <p class="mt-2" style="font-size:0.8rem;color:var(--text-muted);">Showing <span id="leadsCount"><?php echo count($all); ?></span> leads</p>
</div>
<script src="/assets/js/main.js" defer></script>
<script>document.addEventListener('DOMContentLoaded', () => { if (document.querySelector('.admin-leads-table')) adminLeads.init(); });</script>
</body>
</html>
