<?php $title = 'Announcements - Ghana Anaesthesiologists Society'; ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-bullhorn mr-2"></i>Announcements</h1>
    <?php if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?>
    <a href="<?= $baseUrl ?>/announcements/create" class="glass-btn glass-btn-primary"><i class="fas fa-plus mr-1"></i>New Post</a>
    <?php endif; ?>
</div>

<!-- Filter by type -->
<div class="glass-card mb-4">
    <form method="GET" class="form-inline">
        <label class="mr-2" style="color: rgba(255,255,255,0.7);">Filter by type:</label>
        <select name="type" class="form-control glass-input mr-2" style="width: auto;">
            <option value="">All Types</option>
            <option value="announcement" <?= ($filterType ?? '') === 'announcement' ? 'selected' : '' ?>>Announcement</option>
            <option value="news" <?= ($filterType ?? '') === 'news' ? 'selected' : '' ?>>News</option>
            <option value="article" <?= ($filterType ?? '') === 'article' ? 'selected' : '' ?>>Article</option>
            <option value="event_update" <?= ($filterType ?? '') === 'event_update' ? 'selected' : '' ?>>Event Update</option>
        </select>
        <button type="submit" class="glass-btn btn-sm"><i class="fas fa-filter mr-1"></i>Filter</button>
        <?php if ($filterType): ?>
        <a href="<?= $baseUrl ?>/announcements" class="glass-btn btn-sm ml-2"><i class="fas fa-times mr-1"></i>Clear</a>
        <?php endif; ?>
    </form>
</div>

<?php if (!empty($announcements)): ?>
    <?php foreach ($announcements as $ann): ?>
    <div class="glass-card mb-3">
        <div class="d-flex justify-content-between align-items-start">
            <div class="d-flex">
                <?php if ($ann->image): ?>
                <div class="mr-3" style="min-width: 100px; max-width: 100px;">
                    <img src="<?= $baseUrl ?>/<?= $ann->image ?>" style="width: 100px; height: 70px; object-fit: cover; border-radius: 8px;" alt="">
                </div>
                <?php endif; ?>
                <div>
                    <h5 class="font-weight-bold"><?= htmlspecialchars($ann->title) ?></h5>
                    <div class="d-flex flex-wrap gap-2 mb-1">
                        <span class="glass-badge" style="font-size: 0.7rem;"><?= htmlspecialchars($ann->post_type ?? 'announcement') ?></span>
                        <?php if ($ann->category_name): ?>
                        <span class="glass-badge" style="font-size: 0.7rem; background: rgba(246,194,62,0.2); color: #f6c23e;"><?= htmlspecialchars($ann->category_name) ?></span>
                        <?php endif; ?>
                    </div>
                    <p style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($ann->excerpt ?: substr(strip_tags($ann->content), 0, 200)) ?></p>
                    <?php if ($ann->link_url): ?>
                    <a href="<?= htmlspecialchars($ann->link_url) ?>" target="_blank" style="font-size: 0.85rem; color: #4e73df;"><i class="fas fa-external-link-alt mr-1"></i><?= htmlspecialchars($ann->link_url) ?></a>
                    <?php endif; ?>
                </div>
            </div>
            <div class="text-right ml-3" style="min-width: 120px;">
                <span class="glass-badge mb-1 d-block"><?= htmlspecialchars($ann->priority) ?></span>
                <span class="glass-badge mb-1 d-block" style="background: <?= ($ann->visibility ?? 'public') === 'private' ? 'rgba(231,74,59,0.2);color:#e74a3b' : 'rgba(28,200,138,0.2);color:#1cc88a' ?>;">
                    <?= htmlspecialchars($ann->visibility ?? 'public') ?>
                </span>
                <small style="color: rgba(255,255,255,0.5);"><?= date('M d, Y', strtotime($ann->published_at ?? $ann->created_at)) ?></small>
                <?php if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?>
                <div class="mt-2">
                    <a href="<?= $baseUrl ?>/announcements/<?= $ann->id ?>/edit" class="btn btn-sm btn-primary"><i class="fas fa-edit"></i></a>
                    <form method="POST" action="<?= $baseUrl ?>/announcements/<?= $ann->id ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this announcement?');">
                        <button type="submit" class="btn btn-sm btn-danger"><i class="fas fa-trash"></i></button>
                    </form>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
<?php else: ?>
<div class="empty-state"><i class="fas fa-bullhorn"></i><p>No announcements yet</p></div>
<?php endif; ?>
