<?php $editing = !empty($editAnnouncement); ?>
<main class="container-fluid py-4">
    <div class="row g-4">
        <div class="col-lg-3"><?php require APP_PATH.'/Views/admin/_sidebar.php'; ?></div>
        <div class="col-lg-9">
            <div class="d-flex flex-column flex-md-row justify-content-between gap-3 align-items-md-center mb-4">
                <div><h1 class="h3 fw-bold text-cfc-dark mb-1">Quadro de avisos</h1><p class="text-muted mb-0">Publique comunicados que aparecem na Dashboard dos alunos.</p></div>
                <?php if($editing): ?><a class="btn btn-outline-secondary rounded-pill" href="<?= e(url('admin/announcements')) ?>">Cancelar edição</a><?php endif; ?>
            </div>
            <div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4">
                <h2 class="h5 fw-bold mb-3"><?= $editing ? 'Editar aviso' : 'Novo aviso' ?></h2>
                <form method="post" action="<?= e(url('admin/announcements/save')) ?>" class="row g-3">
                    <?= CSRF::input() ?>
                    <?php if($editing): ?><input type="hidden" name="announcement_id" value="<?= (int)$editAnnouncement['id'] ?>"><?php endif; ?>
                    <div class="col-md-8"><label class="form-label">Título</label><input name="title" maxlength="180" class="form-control" value="<?= e($editAnnouncement['title'] ?? '') ?>" required></div>
                    <div class="col-md-4"><label class="form-label">Cor do aviso</label><select name="style" class="form-select"><?php foreach(['info'=>'Azul','success'=>'Verde','warning'=>'Amarelo','danger'=>'Vermelho','primary'=>'Azul forte'] as $value=>$label): ?><option value="<?= e($value) ?>" <?= ($editAnnouncement['style']??'info')===$value?'selected':'' ?>><?= e($label) ?></option><?php endforeach; ?></select></div>
                    <div class="col-12"><label class="form-label">Mensagem</label><textarea name="message" rows="4" maxlength="5000" class="form-control" required><?= e($editAnnouncement['message'] ?? '') ?></textarea></div>
                    <div class="col-md-6"><label class="form-label">Exibir a partir de (opcional)</label><input type="datetime-local" name="starts_at" class="form-control" value="<?= !empty($editAnnouncement['starts_at']) ? e(date('Y-m-d\TH:i',strtotime($editAnnouncement['starts_at']))) : '' ?>"></div>
                    <div class="col-md-6"><label class="form-label">Encerrar em (opcional)</label><input type="datetime-local" name="ends_at" class="form-control" value="<?= !empty($editAnnouncement['ends_at']) ? e(date('Y-m-d\TH:i',strtotime($editAnnouncement['ends_at']))) : '' ?>"></div>
                    <div class="col-12"><button class="btn btn-primary rounded-pill px-4"><i class="bi bi-megaphone"></i> <?= $editing ? 'Atualizar aviso' : 'Publicar aviso' ?></button></div>
                </form>
            </div></div>
            <div class="row g-3">
                <?php foreach($announcements as $notice): ?>
                    <div class="col-xl-6"><div class="card border-0 shadow-soft rounded-4 h-100"><div class="card-body p-4">
                        <div class="d-flex justify-content-between gap-2 mb-2"><h2 class="h5 fw-bold mb-0"><?= e($notice['title']) ?></h2><span class="badge <?= !empty($notice['is_active'])?'text-bg-success':'text-bg-secondary' ?>"><?= !empty($notice['is_active'])?'Ativo':'Inativo' ?></span></div>
                        <div class="alert alert-<?= e($notice['style'] ?: 'info') ?> rounded-4"><?= nl2br(e($notice['message'])) ?></div>
                        <small class="text-muted d-block mb-3"><?php if(!empty($notice['starts_at'])): ?>Início: <?= e(date('d/m/Y H:i',strtotime($notice['starts_at']))) ?> · <?php endif; ?><?php if(!empty($notice['ends_at'])): ?>Fim: <?= e(date('d/m/Y H:i',strtotime($notice['ends_at']))) ?><?php else: ?>Sem data final<?php endif; ?></small>
                        <div class="d-flex flex-wrap gap-2"><a class="btn btn-sm btn-outline-primary rounded-pill" href="<?= e(url('admin/announcements',['edit'=>(int)$notice['id']])) ?>">Editar</a><form method="post" action="<?= e(url('admin/announcements/toggle')) ?>"><?= CSRF::input() ?><input type="hidden" name="announcement_id" value="<?= (int)$notice['id'] ?>"><button class="btn btn-sm btn-outline-secondary rounded-pill"><?= !empty($notice['is_active'])?'Desativar':'Ativar' ?></button></form><form method="post" action="<?= e(url('admin/announcements/delete')) ?>" onsubmit="return confirm('Apagar este aviso?');"><?= CSRF::input() ?><input type="hidden" name="announcement_id" value="<?= (int)$notice['id'] ?>"><button class="btn btn-sm btn-outline-danger rounded-pill">Apagar</button></form></div>
                    </div></div></div>
                <?php endforeach; ?>
                <?php if(empty($announcements)): ?><div class="col-12"><div class="alert alert-info rounded-4">Nenhum aviso cadastrado.</div></div><?php endif; ?>
            </div>
        </div>
    </div>
</main>
