<?php
/**
 * Announcements List — Gmind Institute ERP
 * Lists all announcements with CRUD and toggle status
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';

$typeColors = [
    'general'   => ['bg' => '#e3f2fd', 'text' => '#1565c0'],
    'exam'      => ['bg' => '#fff3e0', 'text' => '#e65100'],
    'admission' => ['bg' => '#e8f5e9', 'text' => '#2e7d32'],
    'event'     => ['bg' => '#f3e5f5', 'text' => '#7b1fa2'],
];
?>
<div class="admin-page-header" style="display:flex;justify-content:space-between;align-items:center;margin-bottom:24px;">
    <h1 style="margin:0;font-size:22px;font-weight:700;">Announcements</h1>
    <a href="<?= $bp ?>/admin/announcements/create" class="admin-btn admin-btn-primary">+ Create Announcement</a>
</div>

<div class="admin-table-wrapper">
    <table class="admin-table">
        <thead>
            <tr>
                <th>Title</th>
                <th>Type</th>
                <th>Branch</th>
                <th>Active</th>
                <th>Created</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($announcements)): ?>
                <?php foreach ($announcements as $ann): ?>
                    <?php
                    $type = $ann['type'] ?? 'general';
                    $tc = $typeColors[$type] ?? $typeColors['general'];
                    ?>
                <tr>
                    <td><?= htmlspecialchars($ann['title'], ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:12px;font-size:12px;font-weight:600;background:<?= $tc['bg'] ?>;color:<?= $tc['text'] ?>;">
                            <?= ucfirst(htmlspecialchars($type, ENT_QUOTES, 'UTF-8')) ?>
                        </span>
                    </td>
                    <td><?= htmlspecialchars($ann['branch_name'] ?? 'All Branches', ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <?php if (!empty($ann['is_active'])): ?>
                            <span class="admin-badge admin-badge-success">Active</span>
                        <?php else: ?>
                            <span class="admin-badge admin-badge-danger">Inactive</span>
                        <?php endif; ?>
                    </td>
                    <td><?= htmlspecialchars($ann['created_at'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <a href="<?= $bp ?>/admin/announcements/edit/<?= (int)$ann['id'] ?>" class="admin-btn admin-btn-sm admin-btn-secondary" title="Edit">Edit</a>
                        <form method="POST" action="<?= $bp ?>/admin/announcements/toggle/<?= (int)$ann['id'] ?>" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" class="admin-btn admin-btn-sm admin-btn-secondary" title="<?= !empty($ann['is_active']) ? 'Deactivate' : 'Activate' ?>"><?= !empty($ann['is_active']) ? 'Deactivate' : 'Activate' ?></button>
                        </form>
                        <form method="POST" action="<?= $bp ?>/admin/announcements/delete/<?= (int)$ann['id'] ?>" style="display:inline;" onsubmit="return confirm('Are you sure you want to delete this announcement?');">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                            <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" title="Delete">Delete</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="6" style="text-align:center;color:#999;">No announcements found. Create your first announcement.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
