<?php
/**
 * Super Admin — Announcements List
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$announcements = $announcements ?? [];
$scopeFilter = $scopeFilter ?? '';

$typeColors = ['general' => '#3498db', 'urgent' => '#e84118', 'info' => '#f39c12'];
?>
<div class="admin-section">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;flex-wrap:wrap;gap:12px;">
        <form method="GET" action="<?= $bp ?>/super-admin/announcements" style="display:flex;gap:8px;">
            <select name="scope" onchange="this.form.submit()" style="padding:8px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.85rem;">
                <option value="">All Announcements</option>
                <option value="global" <?= $scopeFilter === 'global' ? 'selected' : '' ?>>Global Only</option>
                <option value="branch" <?= $scopeFilter === 'branch' ? 'selected' : '' ?>>Branch-Specific Only</option>
            </select>
        </form>
        <a href="<?= $bp ?>/super-admin/announcements/create" class="admin-btn" style="background:#27ae60;color:#fff;text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.88rem;font-weight:600;">+ New Announcement</a>
    </div>

    <div class="admin-card" style="background:#fff;border-radius:10px;padding:0;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <?php if (empty($announcements)): ?>
            <div style="text-align:center;padding:60px 20px;color:#999;">
                <p style="font-size:16px;margin:0 0 6px;">No announcements yet</p>
                <p style="font-size:13px;margin:0;">Create your first announcement — it'll appear in branch dashboards instantly.</p>
            </div>
        <?php else: ?>
            <div style="overflow-x:auto;">
                <table class="admin-table" style="width:100%;border-collapse:collapse;font-size:0.86rem;">
                    <thead>
                        <tr style="background:#f8f9fa;border-bottom:2px solid #dee2e6;">
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Title</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Type</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Scope</th>
                            <th style="padding:12px;text-align:center;font-weight:600;color:#495057;">Status</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Created</th>
                            <th style="padding:12px;text-align:right;font-weight:600;color:#495057;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($announcements as $a): ?>
                            <tr style="border-bottom:1px solid #f0f0f0;" onmouseover="this.style.background='#fafafa'" onmouseout="this.style.background=''">
                                <td style="padding:12px;">
                                    <div style="font-weight:600;color:#2c1e4a;"><?= htmlspecialchars($a['title'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                    <div style="font-size:0.76rem;color:#999;margin-top:2px;"><?= htmlspecialchars(mb_substr($a['text'] ?? '', 0, 80), ENT_QUOTES, 'UTF-8') ?><?= mb_strlen($a['text'] ?? '') > 80 ? '...' : '' ?></div>
                                </td>
                                <td style="padding:12px;">
                                    <span style="display:inline-block;padding:3px 10px;border-radius:20px;font-size:0.72rem;font-weight:600;color:#fff;background:<?= $typeColors[$a['type']] ?? '#999' ?>;text-transform:uppercase;"><?= htmlspecialchars($a['type'] ?? 'general', ENT_QUOTES, 'UTF-8') ?></span>
                                </td>
                                <td style="padding:12px;color:#666;font-size:0.82rem;">
                                    <?php if (empty($a['branch_id'])): ?>
                                        <span style="color:#6c3fe8;font-weight:600;">🌍 Global</span>
                                    <?php else: ?>
                                        <span style="color:#16a085;font-weight:600;">📍 <?= htmlspecialchars($a['branch_code'] ?? 'Branch', ENT_QUOTES, 'UTF-8') ?></span>
                                        <div style="font-size:0.72rem;color:#999;"><?= htmlspecialchars($a['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                    <?php endif; ?>
                                </td>
                                <td style="padding:12px;text-align:center;">
                                    <?php if (!empty($a['is_active'])): ?>
                                        <span style="color:#27ae60;font-weight:600;font-size:0.78rem;">✓ Active</span>
                                    <?php else: ?>
                                        <span style="color:#999;font-size:0.78rem;">Inactive</span>
                                    <?php endif; ?>
                                </td>
                                <td style="padding:12px;color:#666;font-size:0.8rem;"><?= date('M j, Y', strtotime($a['created_at'] ?? 'now')) ?></td>
                                <td style="padding:12px;text-align:right;">
                                    <a href="<?= $bp ?>/super-admin/announcements/<?= (int) $a['id'] ?>/edit" class="admin-btn" style="background:#f39c12;color:#fff;text-decoration:none;padding:5px 12px;border-radius:4px;font-size:0.74rem;">Edit</a>
                                    <form method="POST" action="<?= $bp ?>/super-admin/announcements/<?= (int) $a['id'] ?>/delete" onsubmit="return confirm('Delete this announcement?');" style="display:inline;">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" style="background:#e84118;color:#fff;border:none;padding:5px 12px;border-radius:4px;font-size:0.74rem;cursor:pointer;">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
