<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/middleware/auth.php';
$pdo = require __DIR__ . '/../../app/config/database.php';

require_login();
$u = current_user();

$pdo->prepare("UPDATE notifications SET is_read=1 WHERE user_id=?")->execute([(int)$u['id']]);

$st = $pdo->prepare("SELECT * FROM notifications WHERE user_id=? ORDER BY id DESC LIMIT 200");
$st->execute([(int)$u['id']]);
$items = $st->fetchAll();

$title = 'Notifications';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/nav_portal.php';
?>
<div class="container py-4">
  <h3 class="fw-bold">Notifications</h3>
  <div class="card ab-card p-3 mt-3">
    <?php if (!$items): ?>
      <div class="ab-muted">No notifications yet.</div>
    <?php else: ?>
      <div class="list-group list-group-flush">
        <?php foreach ($items as $n): ?>
          <div class="list-group-item">
            <div class="fw-semibold"><?= e($n['title']) ?></div>
            <div class="ab-muted small"><?= e($n['message']) ?></div>
            <div class="text-slate-500 small mt-1"><?= e($n['created_at']) ?></div>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</div>
<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
