<?php

include "../includes/member_auth.php";
include "../config/database.php";

$stmt = $pdo->prepare("
SELECT *
FROM notifications
WHERE user_id=?
ORDER BY created_at DESC
");

$stmt->execute([$_SESSION['user_id']]);

$notifications = $stmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| Mark all as read
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
UPDATE notifications
SET is_read=1
WHERE user_id=?
");

$stmt->execute([$_SESSION['user_id']]);

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Notifications</title>

<link rel="stylesheet" href="../assets/css/admin.css">
<link rel="stylesheet" href="../assets/css/member.css">
<link rel="stylesheet" href="../assets/css/responsive.css">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">

</head>

<body>

<?php include "../includes/member_sidebar.php"; ?>

<div class="sidebar-overlay"></div>

<?php include "../includes/member_topbar.php"; ?>

<div class="main-content">

<div class="notifications-card">

<h2>

<i class="fas fa-bell"></i>

Notifications

</h2>

<?php if(count($notifications)>0): ?>

<?php foreach($notifications as $note): ?>

<div class="notification-item <?= $note['type']; ?>">

<h4>

<?= htmlspecialchars($note['title']); ?>

</h4>

<p>

<?= htmlspecialchars($note['message']); ?>

</p>

<small>

<?= date("d M Y h:i A",strtotime($note['created_at'])); ?>

</small>

</div>

<?php endforeach; ?>

<?php else: ?>

<p>No notifications available.</p>

<?php endif; ?>

</div>

</div>

<script src="../assets/js/admin.js"></script>

</body>

</html>