<?php
/** @var \BotGuard\Core\View $this @var \BotGuard\Core\Csrf $csrf @var array $announcements @var array $levels */
$this->layout('layouts.app');
?>
<h1 class="h4 mb-3">Announcements</h1>

<div class="row g-4">
    <div class="col-lg-5">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h6 class="mb-3">New announcement</h6>
                <form method="post" action="/admin/announcements">
                    <?= $csrf->field() ?>
                    <div class="mb-2">
                        <label class="form-label">Title</label>
                        <input type="text" name="title" class="form-control" maxlength="160" required>
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Message</label>
                        <textarea name="body" class="form-control" rows="3" maxlength="2000"></textarea>
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Level</label>
                        <select name="level" class="form-select">
                            <?php foreach ($levels as $lvl): ?>
                                <option value="<?= e($lvl) ?>"><?= e(ucfirst($lvl)) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="row g-2">
                        <div class="col-6 mb-2">
                            <label class="form-label small">Starts (optional)</label>
                            <input type="datetime-local" name="starts_at" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 mb-2">
                            <label class="form-label small">Ends (optional)</label>
                            <input type="datetime-local" name="ends_at" class="form-control form-control-sm">
                        </div>
                    </div>
                    <button class="btn btn-primary mt-2" type="submit"><i class="bi bi-megaphone"></i> Publish</button>
                </form>
            </div>
        </div>
    </div>
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h6 class="mb-3">Existing</h6>
                <?php if (empty($announcements)): ?>
                    <p class="text-secondary small mb-0">No announcements yet.</p>
                <?php else: foreach ($announcements as $a): ?>
                    <div class="border rounded p-3 mb-2">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <span class="badge text-bg-<?= e($a['level']) ?>"><?= e(ucfirst((string) $a['level'])) ?></span>
                                <span class="fw-semibold ms-1"><?= e($a['title']) ?></span>
                                <?php if (! $a['active']): ?><span class="badge text-bg-secondary ms-1">inactive</span><?php endif; ?>
                                <div class="small text-secondary mt-1"><?= e($a['body']) ?></div>
                            </div>
                            <div class="d-flex gap-1">
                                <form method="post" action="/admin/announcements/toggle" class="d-inline">
                                    <?= $csrf->field() ?><input type="hidden" name="id" value="<?= e($a['id']) ?>">
                                    <button class="btn btn-outline-secondary btn-sm" title="Toggle active"><i class="bi bi-power"></i></button>
                                </form>
                                <form method="post" action="/admin/announcements/delete" class="d-inline" onsubmit="return confirm('Delete this announcement?');">
                                    <?= $csrf->field() ?><input type="hidden" name="id" value="<?= e($a['id']) ?>">
                                    <button class="btn btn-outline-danger btn-sm" title="Delete"><i class="bi bi-trash"></i></button>
                                </form>
                            </div>
                        </div>
                    </div>
                <?php endforeach; endif; ?>
            </div>
        </div>
    </div>
</div>
