<?php
// csr/announcements.php — Clean announcement inbox
if (session_status() === PHP_SESSION_NONE) session_start();
if (empty($_SESSION['csr_id'])) { header('Location: ../login.php'); exit; }
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/announcement_system.php';
$db = (new Database())->getConnection();
$csrId = (int)$_SESSION['csr_id'];
$me = annGetCsr($db, $csrId);
if (!$me || ($me['role'] ?? 'csr') !== 'csr') { header('Location: ../login.php'); exit; }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['mark_read'])) {
    try { annMarkRead($db, (int)$_POST['announcement_id'], $csrId); $_SESSION['flash_message']='Announcement marked as read.'; }
    catch(Throwable $e){ $_SESSION['flash_error']=$e->getMessage(); }
    header('Location: announcements.php'); exit;
}
$rows = annGetInboxForUser($db, $csrId, 150);
$unread = count(array_filter($rows, fn($r)=>empty($r['is_read'])));
$title = 'Announcements';
ob_start();
?>
<style>
.ann-page{max-width:1050px;margin:0 auto}.ann-head{display:flex;justify-content:space-between;gap:12px;align-items:flex-start;margin-bottom:16px;flex-wrap:wrap}.ann-card{background:var(--cm-card);border:1px solid var(--cm-border);border-radius:16px;padding:16px;margin-bottom:12px}.ann-card.unread{border-color:oklch(0.62 0.22 255/.55);box-shadow:0 0 0 1px oklch(0.62 0.22 255/.12)}.ann-title{font-family:'Space Grotesk',system-ui,sans-serif;font-weight:900;font-size:1rem}.ann-meta{color:var(--cm-muted);font-size:.78rem;margin-top:4px}.ann-msg{white-space:pre-wrap;margin-top:12px;color:var(--cm-text)}.ann-empty{text-align:center;padding:42px;border:1px dashed var(--cm-border);border-radius:16px;color:var(--cm-muted)}
</style>
<div class="ann-page">
  <div class="ann-head">
    <div><h2 style="font-family:'Space Grotesk';font-weight:900;margin:0;">Announcements</h2><div class="text-muted">Messages sent to you by Admin, CSR Manager, or your Team Leader.</div></div>
    <span class="badge bg-danger" style="<?= $unread ? '' : 'display:none' ?>"><?= $unread ?> unread</span>
  </div>
  <?php if (!empty($_SESSION['flash_message'])): ?><div class="alert alert-success"><?= ann_h($_SESSION['flash_message']); unset($_SESSION['flash_message']); ?></div><?php endif; ?>
  <?php if (!empty($_SESSION['flash_error'])): ?><div class="alert alert-danger"><?= ann_h($_SESSION['flash_error']); unset($_SESSION['flash_error']); ?></div><?php endif; ?>
  <?php if (!$rows): ?>
    <div class="ann-empty"><i class="fas fa-bullhorn mb-3" style="font-size:2rem;"></i><div>No announcements yet.</div></div>
  <?php endif; ?>
  <?php foreach ($rows as $a): $pri = in_array($a['priority'], ['info','success','warning','danger'], true) ? $a['priority'] : 'info'; ?>
    <div class="ann-card <?= empty($a['is_read']) ? 'unread' : '' ?>">
      <div class="d-flex justify-content-between align-items-start gap-2 flex-wrap">
        <div><div class="ann-title"><?= ann_h($a['title']) ?></div><div class="ann-meta">From <?= ann_h($a['created_by_name'] ?: annSenderLabel($a['sender_type'])) ?> • <?= date('M j, Y g:i A', strtotime($a['created_at'])) ?></div></div>
        <div class="d-flex gap-2 align-items-center"><span class="badge bg-<?= ann_h($pri) ?>"><?= ucfirst($pri) ?></span><?php if (empty($a['is_read'])): ?><form method="post"><input type="hidden" name="announcement_id" value="<?= (int)$a['id'] ?>"><button class="btn btn-sm btn-primary" name="mark_read" value="1"><i class="fas fa-check me-1"></i>Read</button></form><?php else: ?><span class="badge bg-success">Read</span><?php endif; ?></div>
      </div>
      <div class="ann-msg"><?= nl2br(ann_h($a['message'])) ?></div>
    </div>
  <?php endforeach; ?>
</div>
<?php
$content = ob_get_clean();
require_once __DIR__ . '/layout.php';
