<?php
require_once '../includes/config.php';
requireAdmin();
$success = '';

if ($_SERVER['REQUEST_METHOD']==='POST') {
    $action = $_POST['action']??'';
    if ($action==='add') {
        $title = sanitize($_POST['title']??'');
        $content = sanitize($_POST['content']??'');
        $priority = in_array($_POST['priority']??'',['low','normal','high'])?$_POST['priority']:'normal';
        if ($title && $content) {
            $pdo->prepare("INSERT INTO announcements (title,content,priority) VALUES (?,?,?)")->execute([$title,$content,$priority]);
            $success = 'Announcement posted!';
        }
    }
    if ($action==='toggle') {
        $pdo->prepare("UPDATE announcements SET is_active=NOT is_active WHERE id=?")->execute([(int)$_POST['id']]);
        $success = 'Updated!';
    }
    if ($action==='delete') {
        $pdo->prepare("DELETE FROM announcements WHERE id=?")->execute([(int)$_POST['id']]);
        $success = 'Deleted!';
    }
}

$anns = $pdo->query("SELECT * FROM announcements ORDER BY created_at DESC")->fetchAll();
require_once 'includes/header.php';
renderAdminHeader('Announcements', 'announcements');
?>
<?php if ($success): ?><div class="alert alert-success" data-auto-dismiss>✅ <?= $success ?></div><?php endif; ?>

<div class="grid-2" style="align-items:start">
<div class="card">
  <div class="card-header"><div class="card-title">📢 Post Announcement</div></div>
  <form method="POST">
    <input type="hidden" name="action" value="add">
    <div class="form-group"><label class="form-label">Title</label><input type="text" name="title" class="form-control" placeholder="Announcement title" required></div>
    <div class="form-group"><label class="form-label">Content</label><textarea name="content" class="form-control" rows="3" placeholder="Write announcement..." required></textarea></div>
    <div class="form-group"><label class="form-label">Priority</label>
      <select name="priority" class="form-control">
        <option value="low">Low</option><option value="normal" selected>Normal</option><option value="high">High 🔔</option>
      </select></div>
    <button type="submit" class="btn btn-primary btn-full">Post Announcement</button>
  </form>
</div>

<div class="card">
  <div class="card-header"><div class="card-title">📋 All Announcements (<?= count($anns) ?>)</div></div>
  <?php if ($anns): ?>
  <div style="display:flex;flex-direction:column;gap:12px">
  <?php foreach ($anns as $a): $pBadge = match($a['priority']){'high'=>'red','low'=>'cyan',default=>'purple'}; ?>
  <div style="background:var(--bg-secondary);border-radius:12px;padding:16px;border:1px solid var(--border);<?= $a['is_active']?'border-left:3px solid var(--accent)':'' ?>">
    <div style="display:flex;justify-content:space-between;align-items:flex-start;gap:12px">
      <div style="flex:1">
        <div style="font-weight:600;color:var(--text-primary)"><?= htmlspecialchars($a['title']) ?></div>
        <div style="font-size:13px;color:var(--text-secondary);margin-top:4px"><?= htmlspecialchars($a['content']) ?></div>
        <div style="margin-top:8px;display:flex;gap:6px">
          <span class="badge badge-<?= $pBadge ?>"><?= ucfirst($a['priority']) ?></span>
          <?= $a['is_active']?'<span class="badge badge-green">Active</span>':'<span class="badge" style="background:var(--bg-card);color:var(--text-muted)">Hidden</span>' ?>
          <span style="font-size:11px;color:var(--text-muted)"><?= date('M j, Y',strtotime($a['created_at'])) ?></span>
        </div>
      </div>
      <div style="display:flex;gap:6px;flex-shrink:0">
        <form method="POST" style="display:inline"><input type="hidden" name="action" value="toggle"><input type="hidden" name="id" value="<?= $a['id'] ?>"><button type="submit" class="btn btn-secondary btn-sm"><?= $a['is_active']?'Hide':'Show' ?></button></form>
        <form method="POST" style="display:inline" onsubmit="return confirm('Delete?')"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= $a['id'] ?>"><button type="submit" class="btn btn-danger btn-sm btn-icon">🗑️</button></form>
      </div>
    </div>
  </div>
  <?php endforeach; ?>
  </div>
  <?php else: ?><div class="empty-state"><div class="empty-icon">📢</div><p>No announcements yet</p></div><?php endif; ?>
</div>
</div>
<?php require_once 'includes/footer.php'; renderAdminFooter(); ?>
