<?php
/**
 * Semua Notifikasi
 * Koperasi Management System
 */

require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/auth.php';
requireLogin();

$pageTitle = 'Notifikasi';
$pageSubtitle = 'Pemberitahuan aktivitas sistem Anda';

$db = getDB();
$userId = $_SESSION['user_id'];

// Mark all as read if requested
if (isset($_POST['mark_all_read'])) {
    $stmt = $db->prepare("UPDATE notifications SET is_read = 1 WHERE user_id = ? OR user_id IS NULL");
    $stmt->execute([$userId]);
    setFlash('success', 'Semua notifikasi telah ditandai sebagai dibaca.');
    header('Location: notifications.php');
    exit;
}

// Fetch all notifications with pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 15;
$offset = ($page - 1) * $limit;

$totalStmt = $db->prepare("SELECT COUNT(*) FROM notifications WHERE user_id = ? OR user_id IS NULL");
$totalStmt->execute([$userId]);
$totalNotifs = $totalStmt->fetchColumn();
$totalPages = ceil($totalNotifs / $limit);

$stmt = $db->prepare("SELECT * FROM notifications WHERE user_id = ? OR user_id IS NULL ORDER BY created_at DESC LIMIT ? OFFSET ?");
$stmt->bindParam(1, $userId, PDO::PARAM_INT);
$stmt->bindValue(2, $limit, PDO::PARAM_INT);
$stmt->bindValue(3, $offset, PDO::PARAM_INT);
$stmt->execute();
$notifications = $stmt->fetchAll();

include __DIR__ . '/includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <h2><i class="fas fa-bell" style="color: var(--primary-500); margin-right: 8px;"></i>Pusat Pemberitahuan</h2>
        <?php if ($totalNotifs > 0): ?>
        <form method="POST" style="display:inline;">
            <input type="hidden" name="mark_all_read" value="1">
            <button type="submit" class="btn btn-outline btn-sm">
                <i class="fas fa-check-double"></i> Tandai Semua Dibaca
            </button>
        </form>
        <?php endif; ?>
    </div>

    <div class="card-body" style="padding: 0;">
        <div class="notification-full-list">
            <?php if (empty($notifications)): ?>
                <div class="notification-empty" style="padding: 80px 20px;">
                    <i class="fas fa-bell-slash"></i>
                    <p>Selamat! Anda tidak memiliki notifikasi baru.</p>
                </div>
            <?php else: ?>
                <?php foreach ($notifications as $n): ?>
                    <div class="notif-full-item <?= !$n['is_read'] ? 'unread' : '' ?>">
                        <div class="notif-icon-large <?= $n['type'] ?>">
                            <i class="fas fa-<?= $n['type'] === 'success' ? 'check-circle' : ($n['type'] === 'warning' ? 'exclamation-triangle' : ($n['type'] === 'danger' ? 'times-circle' : 'info-circle')) ?>"></i>
                        </div>
                        <div class="notif-full-content">
                            <div class="notif-head">
                                <h4 class="notif-title-text"><?= htmlspecialchars($n['title']) ?></h4>
                                <span class="notif-date"><?= formatTanggal($n['created_at']) ?> <?= date('H:i', strtotime($n['created_at'])) ?> WIB</span>
                            </div>
                            <p class="notif-desc"><?= htmlspecialchars($n['message']) ?></p>
                            <?php if (!$n['is_read']): ?>
                                <span class="badge badge-primary">Baru</span>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="card-footer" style="display: flex; justify-content: center; gap: 10px; background: white;">
        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
            <a href="notifications.php?page=<?= $i ?>" class="btn btn-sm <?= $page === $i ? 'btn-primary' : 'btn-outline' ?>">
                <?= $i ?>
            </a>
        <?php endfor; ?>
    </div>
    <?php endif; ?>
</div>

<style>
.notification-full-list {
    display: flex;
    flex-direction: column;
}

.notif-full-item {
    display: flex;
    gap: 20px;
    padding: 24px 32px;
    border-bottom: 1px solid var(--gray-100);
    transition: all var(--transition-base);
}

.notif-full-item:hover {
    background: var(--gray-50);
}

.notif-full-item.unread {
    background: rgba(99, 102, 241, 0.02);
    border-left: 4px solid var(--primary-500);
}

.notif-icon-large {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1.4rem;
}

.notif-icon-large.info { background: var(--info-100); color: var(--info-600); }
.notif-icon-large.success { background: var(--success-100); color: var(--success-600); }
.notif-icon-large.warning { background: var(--warning-100); color: var(--warning-600); }
.notif-icon-large.danger { background: var(--danger-100); color: var(--danger-600); }

.notif-full-content {
    flex: 1;
}

.notif-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
}

.notif-title-text {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--gray-900);
}

.notif-date {
    font-size: 0.8rem;
    color: var(--gray-400);
}

.notif-desc {
    font-size: 0.92rem;
    color: var(--gray-600);
    line-height: 1.5;
    margin-bottom: 8px;
}
</style>

<?php include __DIR__ . '/includes/footer.php'; ?>
