<?php
/**
 * Newsletter Management
 */

session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/includes/auth.php';

checkAdminAuth();

$pdo = getDBConnection();

// Get all subscribers
$subscribers = $pdo->query("
    SELECT id, email, status, subscribed_at
    FROM newsletter_subscribers
    ORDER BY subscribed_at DESC
")->fetchAll();

$pageTitle = 'Newsletter';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - Admin Panel</title>
    <link rel="stylesheet" href="assets/admin-modern.css">
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>
    
    <div class="admin-main">
        <?php include 'includes/header-new.php'; ?>
        
        <div class="admin-container">
            <div class="card slide-up">
                <div class="card-header">
                    <h3 class="card-title">Newsletter Subscribers (<?php echo count($subscribers); ?>)</h3>
                </div>
        
                <div style="overflow-x: auto;">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>Email</th>
                                <th>Status</th>
                                <th>Subscribed</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($subscribers as $subscriber): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($subscriber['email']); ?></td>
                                <td>
                                    <span class="badge badge-<?php echo $subscriber['status'] === 'active' ? 'success' : 'danger'; ?>">
                                        <?php echo ucfirst($subscriber['status']); ?>
                                    </span>
                                </td>
                                <td><?php echo date('M d, Y', strtotime($subscriber['subscribed_at'])); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <script src="assets/admin.js"></script>
</body>
</html>

