<?php
/**
 * Müşteri Paneli - Tüm Bildirimler
 * Konum: /magaza/kullanici/bildirimler.php
 */

session_start();
require_once '../includes/db.php';
require_once '../includes/functions.php';

// 1. OTURUM GÜVENLİĞİ
if (!isset($_SESSION['user_id'])) {
    header("Location: ../giris.php");
    exit;
}

$user_id = (int)$_SESSION['user_id'];

// 2. "TÜMÜNÜ OKUNDU İŞARETLE" İŞLEMİ
if (isset($_GET['islem']) && $_GET['islem'] == 'tumunu_oku') {
    $update_stmt = $db->prepare("UPDATE user_notifications SET is_read = 1 WHERE user_id = ?");
    $update_stmt->execute([$user_id]);
    
    // İşlem bitince sayfanın temiz haline yönlendir
    header("Location: bildirimler.php");
    exit;
}

// 3. BİLDİRİMLERİ ÇEK (Son 50 Bildirim)
$stmt = $db->prepare("SELECT * FROM user_notifications WHERE user_id = ? ORDER BY id DESC LIMIT 50");
$stmt->execute([$user_id]);
$bildirimler = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Bildirimlerim | Sınav Koçu";
require_once '../header.php';
?>

<main class="container py-5 text-dark">
    <div class="row">
        
        <div class="col-lg-3 mb-4">
            <?php include 'sidebar.php'; ?>
        </div>

        <div class="col-lg-9">
            
            <div class="d-flex justify-content-between align-items-center mb-4 border-bottom pb-3">
                <div>
                    <h2 class="fw-bold mb-0"><i class="bi bi-bell-fill text-primary me-2"></i>Bildirimlerim</h2>
                    <p class="text-muted small mb-0 mt-1">Sipariş, kargo ve destek taleplerinizle ilgili güncel bilgiler.</p>
                </div>
                <?php if (count($bildirimler) > 0): ?>
                    <a href="bildirimler.php?islem=tumunu_oku" class="btn btn-sm btn-outline-secondary rounded-pill fw-bold shadow-sm px-3">
                        <i class="bi bi-check2-all me-1"></i> Tümünü Okundu İşaretle
                    </a>
                <?php endif; ?>
            </div>

            <div class="card shadow-sm border-0 rounded-4 overflow-hidden mb-5">
                <div class="list-group list-group-flush">
                    <?php if (count($bildirimler) > 0): ?>
                        <?php foreach ($bildirimler as $b): ?>
                            <a href="bildirim-oku.php?id=<?= $b['id'] ?>" class="list-group-item list-group-item-action p-4 <?= $b['is_read'] ? 'bg-white' : 'bg-primary bg-opacity-10 border-start border-primary border-4' ?>">
                                <div class="d-flex w-100 justify-content-between align-items-center mb-2">
                                    <h6 class="mb-0 fw-bold <?= $b['is_read'] ? 'text-dark' : 'text-primary' ?>">
                                        <i class="bi <?= htmlspecialchars($b['icon']) ?> me-2 fs-5 align-middle"></i><?= htmlspecialchars($b['title']) ?>
                                    </h6>
                                    <small class="text-muted"><i class="bi bi-clock me-1"></i><?= formatDate($b['created_at'], true) ?></small>
                                </div>
                                <p class="mb-0 text-secondary" style="font-size: 0.95rem; margin-left: 32px;">
                                    <?= htmlspecialchars($b['message']) ?>
                                </p>
                            </a>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="p-5 text-center text-muted">
                            <div class="bg-light rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width: 80px; height: 80px;">
                                <i class="bi bi-bell-slash fs-1 opacity-50"></i>
                            </div>
                            <h5 class="fw-bold text-dark">Henüz hiç bildiriminiz bulunmuyor.</h5>
                            <p class="small mb-0">Hesabınızla ilgili önemli güncellemeler burada yer alacaktır.</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

        </div>
    </div>
</main>

<?php require_once '../footer.php'; ?>