<?php
// File: /admin/subscribers.php
include 'header.php'; // Includes security check and DB connection

// Fetch all subscribers
$subscribers = [];
$result = $conn->query("SELECT email, subscribed_at FROM newsletter_subscribers ORDER BY subscribed_at DESC");
if ($result) {
    $subscribers = $result->fetch_all(MYSQLI_ASSOC);
}
?>

<div class="p-6 md:p-8">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-6">Newsletter Subscribers</h1>

    <div class="bg-white p-6 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">Subscriber List</h2>
        
        <!-- Add export button or search later if needed -->

        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email Address</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Subscribed On</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php if (!empty($subscribers)): ?>
                        <?php foreach ($subscribers as $subscriber): ?>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?php echo htmlspecialchars($subscriber['email']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('M d, Y, h:i A', strtotime($subscriber['subscribed_at'])); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="2" class="px-6 py-12 text-center text-gray-500">No one has subscribed to the newsletter yet.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
