<?php
require_once '../includes/init.php';

// Check admin access
if (!isAuthenticated() || !$_SESSION['is_admin']) {
    redirect(APP_URL . '/login.php');
}

$pageTitle = 'Announcements';
$db = getDB();
$message = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'create') {
        $title = sanitizeInput($_POST['title'] ?? '');
        $content = sanitizeInput($_POST['content'] ?? '');
        
        if (!empty($title) && !empty($content)) {
            $stmt = $db->prepare("INSERT INTO announcements (admin_id, title, message) VALUES (?, ?, ?)");
            $stmt->execute([$_SESSION['user_id'], $title, $content]);
            $message = 'Announcement created successfully';
        }
    } elseif ($action === 'delete') {
        $id = intval($_POST['id'] ?? 0);
        $stmt = $db->prepare("DELETE FROM announcements WHERE id = ?");
        $stmt->execute([$id]);
        $message = 'Announcement deleted';
    } elseif ($action === 'toggle') {
        $id = intval($_POST['id'] ?? 0);
        $stmt = $db->prepare("UPDATE announcements SET is_active = NOT is_active WHERE id = ?");
        $stmt->execute([$id]);
        $message = 'Announcement status updated';
    }
}

// Get all announcements
$stmt = $db->query("SELECT a.*, u.name as admin_name FROM announcements a JOIN users u ON a.admin_id = u.id ORDER BY a.created_at DESC");
$announcements = $stmt->fetchAll();

include '../includes/header.php';
?>

<div class="min-h-screen bg-slate-50 dark:bg-slate-900">
    <!-- Admin Header -->
    <header class="bg-white dark:bg-slate-800 border-b border-slate-200 dark:border-slate-700 sticky top-0 z-20">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex items-center justify-between h-16">
                <div class="flex items-center gap-4">
                    <a href="dashboard.php" class="text-slate-600 dark:text-slate-400 hover:text-primary">
                        <span class="material-symbols-outlined">arrow_back</span>
                    </a>
                    <div class="text-primary flex items-center gap-2">
                        <span class="material-symbols-outlined text-2xl">campaign</span>
                        <span class="font-bold text-lg">Announcements</span>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <?php if ($message): ?>
        <div class="bg-green-100 dark:bg-green-900/20 border border-green-200 dark:border-green-800 text-green-700 dark:text-green-400 px-4 py-3 rounded-xl mb-6">
            <?php echo $message; ?>
        </div>
        <?php endif; ?>

        <!-- Create Announcement -->
        <div class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 p-6 mb-8">
            <h3 class="font-semibold text-slate-900 dark:text-white mb-4">Create New Announcement</h3>
            <form method="POST" action="">
                <input type="hidden" name="action" value="create">
                <div class="mb-4">
                    <label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">Title</label>
                    <input type="text" name="title" required class="w-full px-4 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-700 text-slate-900 dark:text-white focus:ring-2 focus:ring-primary">
                </div>
                <div class="mb-4">
                    <label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">Message</label>
                    <textarea name="content" rows="4" required class="w-full px-4 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-700 text-slate-900 dark:text-white focus:ring-2 focus:ring-primary"></textarea>
                </div>
                <button type="submit" class="px-6 py-2 bg-primary text-white rounded-lg font-medium hover:bg-primary/90 transition-colors">
                    Post Announcement
                </button>
            </form>
        </div>

        <!-- Announcements List -->
        <div class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 overflow-hidden">
            <div class="p-4 border-b border-slate-200 dark:border-slate-700">
                <h3 class="font-semibold text-slate-900 dark:text-white">All Announcements</h3>
            </div>
            <div class="divide-y divide-slate-200 dark:divide-slate-700">
                <?php if (empty($announcements)): ?>
                <div class="p-8 text-center text-slate-500">
                    No announcements yet
                </div>
                <?php else: ?>
                    <?php foreach ($announcements as $announcement): ?>
                    <div class="p-4 hover:bg-slate-50 dark:hover:bg-slate-700/50">
                        <div class="flex items-start justify-between">
                            <div class="flex-1">
                                <div class="flex items-center gap-2 mb-1">
                                    <h4 class="font-semibold text-slate-900 dark:text-white"><?php echo htmlspecialchars($announcement['title']); ?></h4>
                                    <span class="px-2 py-0.5 text-xs rounded-full <?php echo $announcement['is_active'] ? 'bg-green-100 text-green-700' : 'bg-slate-100 text-slate-500'; ?>">
                                        <?php echo $announcement['is_active'] ? 'Active' : 'Inactive'; ?>
                                    </span>
                                </div>
                                <p class="text-sm text-slate-600 dark:text-slate-400 mb-2"><?php echo htmlspecialchars($announcement['message']); ?></p>
                                <p class="text-xs text-slate-400">
                                    Posted by <?php echo htmlspecialchars($announcement['admin_name']); ?> on <?php echo formatDateTime($announcement['created_at']); ?>
                                </p>
                            </div>
                            <div class="flex gap-2 ml-4">
                                <form method="POST" action="" class="inline">
                                    <input type="hidden" name="action" value="toggle">
                                    <input type="hidden" name="id" value="<?php echo $announcement['id']; ?>">
                                    <button type="submit" class="p-2 text-slate-400 hover:text-primary rounded-lg">
                                        <span class="material-symbols-outlined"><?php echo $announcement['is_active'] ? 'visibility_off' : 'visibility'; ?></span>
                                    </button>
                                </form>
                                <form method="POST" action="" class="inline" onsubmit="return confirm('Delete this announcement?')">
                                    <input type="hidden" name="action" value="delete">
                                    <input type="hidden" name="id" value="<?php echo $announcement['id']; ?>">
                                    <button type="submit" class="p-2 text-slate-400 hover:text-red-500 rounded-lg">
                                        <span class="material-symbols-outlined">delete</span>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </main>
</div>

<?php $hideFooter = true; include '../includes/footer.php'; ?>
