<?php
require_once __DIR__ . '/auth-check.php';
require_once __DIR__ . '/../includes/functions.php';

$leads = getData('leads');
$exhibitors = getData('exhibitors');

$totalLeads = count($leads);
$newLeads = count(array_filter($leads, fn($l) => ($l['status'] ?? '') === 'New'));
$thisMonth = count(array_filter($leads, fn($l) => str_starts_with($l['date'] ?? '', date('Y-m'))));
$totalExhibitors = count($exhibitors);

// Recent 5 leads
usort($leads, fn($a, $b) => strcmp($b['date'] ?? '', $a['date'] ?? ''));
$recentLeads = array_slice($leads, 0, 5);

$pageTitle = 'Dashboard';
include __DIR__ . '/sidebar.php';
?>

<div class="admin-header">
    <h1 class="admin-title">Dashboard</h1>
</div>

<!-- Stat Cards -->
<div class="row g-3 mb-4">
    <div class="col-md-3 col-sm-6">
        <div class="admin-card text-center">
            <div style="font-family:var(--font-heading);font-size:2rem;font-weight:700;color:var(--green2);"><?php echo $totalLeads; ?></div>
            <div style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">Total Enquiries</div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="admin-card text-center">
            <div style="font-family:var(--font-heading);font-size:2rem;font-weight:700;color:var(--warning);"><?php echo $newLeads; ?></div>
            <div style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">New Enquiries</div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="admin-card text-center">
            <div style="font-family:var(--font-heading);font-size:2rem;font-weight:700;color:#60a5fa;"><?php echo $thisMonth; ?></div>
            <div style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">This Month</div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="admin-card text-center">
            <div style="font-family:var(--font-heading);font-size:2rem;font-weight:700;color:#c084fc;"><?php echo $totalExhibitors; ?></div>
            <div style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">Total Exhibitors</div>
        </div>
    </div>
</div>

<!-- Recent Submissions -->
<div class="admin-card">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:1rem;">
        <h2 style="font-family:var(--font-heading);font-size:0.95rem;text-transform:uppercase;color:var(--text);margin:0;">Recent Submissions</h2>
        <a href="leads.php" class="btn-admin-outline" style="font-size:0.75rem;padding:0.35rem 0.75rem;">View All</a>
    </div>
    <?php if (!empty($recentLeads)): ?>
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Name / Company</th>
                    <th>Email</th>
                    <th>Type</th>
                    <th>Date</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recentLeads as $lead):
                    $name = ($lead['form_type'] ?? '') === 'register'
                        ? e(($lead['first_name'] ?? '') . ' ' . ($lead['last_name'] ?? ''))
                        : e($lead['name'] ?? '');
                    $company = e($lead['company_name'] ?? $lead['company'] ?? '');
                    $statusColors = [
                        'New' => 'badge-new',
                        'Contacted' => 'badge-contacted',
                        'Replied' => 'badge-replied',
                        'Closed' => 'badge-closed',
                    ];
                    $statusClass = $statusColors[$lead['status'] ?? 'New'] ?? 'badge-new';
                ?>
                <tr>
                    <td>
                        <strong><?php echo $name; ?></strong>
                        <?php if ($company): ?><br><small style="color:var(--muted);"><?php echo $company; ?></small><?php endif; ?>
                    </td>
                    <td><a href="mailto:<?php echo e($lead['email'] ?? ''); ?>" style="color:var(--green2);text-decoration:none;"><?php echo e($lead['email'] ?? ''); ?></a></td>
                    <td>
                        <?php if (($lead['form_type'] ?? '') === 'register'): ?>
                        <span style="display:inline-block;background:rgba(46,125,50,0.2);color:#4ade80;font-size:0.7rem;padding:0.2rem 0.5rem;border-radius:4px;text-transform:uppercase;">Register</span>
                        <?php else: ?>
                        <span style="display:inline-block;background:rgba(96,165,250,0.2);color:#60a5fa;font-size:0.7rem;padding:0.2rem 0.5rem;border-radius:4px;text-transform:uppercase;">Contact</span>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:0.82rem;color:var(--muted);"><?php echo e($lead['date'] ?? ''); ?></td>
                    <td><span class="status-badge <?php echo $statusClass; ?>"><?php echo e($lead['status'] ?? 'New'); ?></span></td>
                    <td><a href="lead-detail.php?id=<?php echo e($lead['id']); ?>" class="btn-admin-outline" style="font-size:0.7rem;padding:0.3rem 0.6rem;">View</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:var(--muted);text-align:center;padding:2rem;">No enquiries yet. They will appear here when someone submits a form.</p>
    <?php endif; ?>
</div>

<!-- Quick Actions -->
<div class="row g-3 mt-2">
    <div class="col-md-4">
        <a href="leads.php" class="admin-card d-block text-center text-decoration-none" style="transition:transform 0.2s;">
            <div style="font-size:1.5rem;margin-bottom:0.5rem;">📋</div>
            <div style="font-family:var(--font-heading);font-size:0.85rem;text-transform:uppercase;color:var(--text);">View All Enquiries</div>
        </a>
    </div>
    <div class="col-md-4">
        <a href="exhibitors.php" class="admin-card d-block text-center text-decoration-none" style="transition:transform 0.2s;">
            <div style="font-size:1.5rem;margin-bottom:0.5rem;">🏢</div>
            <div style="font-family:var(--font-heading);font-size:0.85rem;text-transform:uppercase;color:var(--text);">Add Exhibitor</div>
        </a>
    </div>
    <div class="col-md-4">
        <a href="settings.php" class="admin-card d-block text-center text-decoration-none" style="transition:transform 0.2s;">
            <div style="font-size:1.5rem;margin-bottom:0.5rem;">⚙️</div>
            <div style="font-family:var(--font-heading);font-size:0.85rem;text-transform:uppercase;color:var(--text);">Site Settings</div>
        </a>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
