<?php
// Bu dosya sidebar.php - Kullanıcı panelinin yan menüsü
$current_page = basename($_SERVER['PHP_SELF']);
$user_id_sidebar = (int)$_SESSION['user_id'];

if (!isset($musteri) || empty($musteri)) {
    $usr_stmt = $db->prepare("SELECT name, surname, email FROM users WHERE id = ?");
    $usr_stmt->execute([$user_id_sidebar]);
    $musteri = $usr_stmt->fetch();
}

// Kupon Sayısı
$sidebar_kupon_stmt = $db->prepare("SELECT COUNT(id) FROM coupons WHERE status = 1 AND (valid_until IS NULL OR valid_until > NOW()) AND (user_id = ? OR user_id IS NULL)");
$sidebar_kupon_stmt->execute([$user_id_sidebar]);
$sidebar_kupon_count = $sidebar_kupon_stmt->fetchColumn();

// YENİ: Favori Sayısı
$sidebar_fav_stmt = $db->prepare("SELECT COUNT(id) FROM favorites WHERE user_id = ?");
$sidebar_fav_stmt->execute([$user_id_sidebar]);
$sidebar_fav_count = $sidebar_fav_stmt->fetchColumn();

// YENİ: Fiyat Alarmı Sayısı (Bekleyenler)
$sidebar_alert_stmt = $db->prepare("SELECT COUNT(id) FROM price_alerts WHERE user_id = ? AND status = 0");
$sidebar_alert_stmt->execute([$user_id_sidebar]);
$sidebar_alert_count = $sidebar_alert_stmt->fetchColumn();
?>

<div class="card shadow-sm border-0 position-sticky rounded-4" style="top: 80px;">
    <div class="card-body text-center pt-4 pb-2 border-bottom">
        <div class="bg-primary text-white rounded-circle d-inline-flex align-items-center justify-content-center mb-3 shadow" style="width: 70px; height: 70px; font-size: 24px; font-weight: bold;">
            <?= mb_strtoupper(mb_substr($musteri['name'], 0, 1) . mb_substr($musteri['surname'], 0, 1)) ?>
        </div>
        <h5 class="fw-bold mb-0 text-dark"><?= htmlspecialchars($musteri['name'] . ' ' . $musteri['surname']) ?></h5>
        <p class="text-muted small"><?= htmlspecialchars($musteri['email']) ?></p>
    </div>
    <div class="list-group list-group-flush rounded-bottom">
        <a href="index.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'index.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-grid-1x2-fill me-2"></i> Hesap Özeti
        </a>
        
        <a href="siparisler.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'siparisler.php' || $current_page == 'siparis-detay.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-box-seam me-2"></i> Siparişlerim
        </a>

        <a href="favorilerim.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'favorilerim.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-heart-fill me-2 text-danger"></i> Favorilerim
            <?php if($sidebar_fav_count > 0): ?>
                <span class="badge bg-danger rounded-pill float-end"><?= $sidebar_fav_count ?></span>
            <?php endif; ?>
        </a>

        <a href="fiyat-alarmlarim.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'fiyat-alarmlarim.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-bell-fill me-2 text-warning"></i> Fiyat Alarmları
            <?php if($sidebar_alert_count > 0): ?>
                <span class="badge bg-warning text-dark rounded-pill float-end"><?= $sidebar_alert_count ?></span>
            <?php endif; ?>
        </a>

        <a href="denemepaketim.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'denemepaketim.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-box2-heart-fill me-2 text-info"></i> Deneme Paketlerim
        </a>
        
        <a href="kuponlar.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'kuponlar.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-ticket-perforated me-2"></i> Kuponlarım 
            <?php if($sidebar_kupon_count > 0): ?>
                <span class="badge bg-primary rounded-pill float-end"><?= $sidebar_kupon_count ?></span>
            <?php endif; ?>
        </a>
        
        <a href="adresler.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'adresler.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-geo-alt me-2"></i> Adres Bilgilerim
        </a>
        
        <a href="destek.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'destek.php' || $current_page == 'destek-detay.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-chat-square-dots-fill me-2"></i> Destek Talebi
        </a>
        
        <a href="ayarlar.php" class="list-group-item list-group-item-action py-3 <?= ($current_page == 'ayarlar.php') ? 'active fw-bold' : 'text-secondary fw-semibold' ?>">
            <i class="bi bi-person-gear me-2"></i> Hesap Ayarları
        </a>
        
        <a href="../cikis.php" class="list-group-item list-group-item-action py-3 text-danger fw-bold bg-light">
            <i class="bi bi-box-arrow-right me-2"></i> Çıkış Yap
        </a>
    </div>
</div>