<?php
// File: /admin/newsletter_compose.php
include 'header.php'; // Includes security check and DB connection

// Count subscribers for info
$count_result = $conn->query("SELECT COUNT(*) as total FROM newsletter_subscribers");
$subscriber_count = $count_result ? $count_result->fetch_assoc()['total'] : 0;
?>

<div class="p-6 md:p-8">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-6">Compose & Send Newsletter</h1>

    <div class="bg-white p-6 rounded-lg shadow-md max-w-3xl mx-auto">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">Create Newsletter</h2>

        <?php if (isset($_GET['status'])): ?>
            <div class="mb-4 p-4 rounded-md <?php echo $_GET['status'] == 'success' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'; ?>" role="alert">
                <?php if ($_GET['status'] == 'success'): ?>
                    Newsletter sent successfully to <?php echo htmlspecialchars($_GET['count']); ?> subscribers.
                <?php else: ?>
                    Error sending newsletter. Please try again or check server logs.
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <form action="newsletter_send_handler.php" method="POST" class="space-y-4">
            <div>
                <label for="subject" class="block text-sm font-medium text-gray-700">Subject</label>
                <input type="text" name="subject" id="subject" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-ucf-green focus:ring-ucf-green">
            </div>
            <div>
                <label for="message" class="block text-sm font-medium text-gray-700">Message (HTML is allowed)</label>
                <textarea name="message" id="message" rows="15" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-ucf-green focus:ring-ucf-green" placeholder="Write your newsletter content here... You can use HTML tags like <p>, <strong>, <a> etc."></textarea>
            </div>
            <div class="pt-4 flex justify-end">
                <button type="submit" class="bg-ucf-green text-white font-bold py-3 px-6 rounded-lg hover:bg-ucf-green-dark transition-colors flex items-center" onclick="return confirm('Send this newsletter to <?php echo $subscriber_count; ?> subscribers?');">
                    <i class="fas fa-paper-plane mr-2"></i> Send to All Subscribers
                </button>
            </div>
        </form>
    </div>
</div>

<?php include 'footer.php'; ?>
