<?php
/**
 * Konum: /magaza/indirimli-urunler.php
 * Sınav Koçu - İndirimli Ürünler ve Fırsatlar Sayfası
 */
require_once 'includes/db.php';
require_once 'includes/functions.php';

$page_title = "İndirimli Kaynaklar ve Fırsatlar | Sınav Koçu";

// Aktif Kategorileri Çek (Filtreleme Menüsü İçin)
$kategoriler = $db->query("SELECT id, name, slug FROM categories WHERE status = 1 ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);

// URL'den seçili kategori var mı kontrol et
$secili_kategori = isset($_GET['kategori']) ? trim($_GET['kategori']) : '';

// SADECE İNDİRİMLİ ÜRÜNLERİ ÇEK (discount_price > 0 olanlar)
$params = [];
$where_sql = "WHERE p.status = 1 AND p.discount_price > 0";

if ($secili_kategori !== '') {
    // Çoklu Kategori Desteği
    $where_sql .= " AND (
        c.slug = ? OR 
        p.id IN (
            SELECT pc.product_id FROM product_categories pc 
            JOIN categories c2 ON pc.category_id = c2.id 
            WHERE c2.slug = ?
        )
    )";
    $params[] = $secili_kategori;
    $params[] = $secili_kategori;
}

$sql = "
    SELECT p.id, p.name, p.slug, p.type, p.price, p.discount_price, p.is_free_shipping, p.exam_publication_date, p.stock_quantity,
    b.logo as brand_logo, b.name as brand_name, c.name as cat_name,
    (SELECT GROUP_CONCAT(c3.name SEPARATOR ', ') FROM product_categories pc3 JOIN categories c3 ON pc3.category_id = c3.id WHERE pc3.product_id = p.id) as sub_categories,
    COALESCE(
        (SELECT image_path FROM product_images WHERE product_id = p.id AND is_main = 1 LIMIT 1),
        (SELECT default_cover FROM brands WHERE id = p.brand_id),
        'assets/img/no-image.jpg'
    ) as main_image
    FROM products p 
    LEFT JOIN brands b ON p.brand_id = b.id
    LEFT JOIN categories c ON p.category_id = c.id
    $where_sql 
    ORDER BY (p.price - p.discount_price) DESC 
    LIMIT 48
";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$indirimli_urunler = $stmt->fetchAll(PDO::FETCH_ASSOC);

require_once 'header.php';
?>

<main class="bg-light pb-5">
    
    <section class="bg-danger text-white py-5 position-relative overflow-hidden border-bottom border-dark border-5 mb-5">
        <div class="position-absolute top-0 start-0 w-100 h-100 opacity-25" style="background: repeating-linear-gradient(45deg, #000, #000 10px, transparent 10px, transparent 20px);"></div>
        <div class="container position-relative z-index-1 text-center">
            <h1 class="display-4 fw-black tracking-tight mb-3"><i class="bi bi-tags-fill text-warning"></i> Süper Fırsatlar</h1>
            <p class="lead fw-bold text-white-50 mb-0">En çok satan kaynaklarda ve deneme paketlerinde kısa süreli dev indirimler. Bu fırsatları kaçırmayın!</p>
        </div>
    </section>

    <div class="container text-dark">
        
        <?php $mevcut_sayfa = basename(__FILE__); ?>
        <div class="d-flex flex-wrap justify-content-center gap-2 mb-5">
            <a href="<?= $mevcut_sayfa ?>" class="btn rounded-pill px-4 fw-bold <?= ($secili_kategori == '') ? 'btn-danger shadow' : 'btn-filter' ?>">
                🔥 Tüm Fırsatlar
            </a>
            <?php foreach($kategoriler as $kat): ?>
                <a href="<?= $mevcut_sayfa ?>?kategori=<?= $kat['slug'] ?>" class="btn rounded-pill px-4 fw-bold <?= ($secili_kategori == $kat['slug']) ? 'btn-danger shadow' : 'btn-filter' ?>">
                    <?= htmlspecialchars($kat['name']) ?>
                </a>
            <?php endforeach; ?>
        </div>

        <?php if(empty($indirimli_urunler)): ?>
            <div class="text-center py-5 bg-white rounded-4 shadow-sm border border-light">
                <i class="bi bi-emoji-frown text-muted" style="font-size: 4rem;"></i>
                <h3 class="fw-bold mt-3">Şu an bu kategoride indirimli ürün yok</h3>
                <p class="text-muted">Diğer kategorilerdeki fırsatlara veya tüm indirimli ürünlerimize göz atabilirsiniz.</p>
                <a href="indirimli-urunler.php" class="btn btn-danger fw-bold mt-2">Tüm Fırsatları Gör</a>
            </div>
        <?php else: ?>
            
            <div class="row row-cols-2 row-cols-md-3 row-cols-lg-4 g-3 g-lg-4">
                <?php foreach($indirimli_urunler as $urun): 
                    $stokta_yok = ($urun['type'] != 'digital' && $urun['stock_quantity'] <= 0);
                    $kazanc = $urun['price'] - $urun['discount_price']; // Kazanç hesaplaması
                ?>
                    <div class="col">
                        <div class="card h-100 border-0 shadow-sm product-card transition rounded-4 overflow-hidden position-relative <?= $stokta_yok ? 'opacity-75' : '' ?>">
                            
                            <span class="badge bg-danger text-white position-absolute top-0 end-0 m-2 shadow-sm px-3 py-2 fw-bold" style="z-index: 5; font-size:0.75rem; letter-spacing: 1px;"><i class="bi bi-fire me-1"></i>FIRSAT</span>

                            <?php if(!empty($urun['brand_logo'])): ?>
                                <div class="position-absolute top-0 start-0 m-2 bg-white rounded shadow-sm d-flex align-items-center justify-content-center p-1" style="width: 45px; height: 30px; z-index: 5;">
                                    <img src="<?= htmlspecialchars($urun['brand_logo']) ?>" class="img-fluid" style="max-height: 100%; max-width: 100%; object-fit: contain;">
                                </div>
                            <?php endif; ?>

                            <div class="position-relative overflow-hidden bg-light p-3 text-center">
                                <a href="urun-detay.php?slug=<?= $urun['slug'] ?>">
                                    <img src="<?= htmlspecialchars($urun['main_image']) ?>" 
                                         class="card-img-top p-2 hover-zoom" alt="<?= htmlspecialchars($urun['name']) ?>" 
                                         style="height: 200px; object-fit: contain; transition: transform 0.3s ease;">
                                </a>
                                
                                <span class="badge bg-danger position-absolute bottom-0 start-0 m-2 shadow-sm px-2 py-1 fs-6 fw-black" style="z-index: 2;">-%<?= round(($kazanc / $urun['price']) * 100) ?></span>
                                
                                <?php if ($urun['is_free_shipping'] == 1): ?>
                                    <span class="badge bg-success position-absolute bottom-0 end-0 m-2 shadow-sm px-2 py-1" style="z-index: 2; font-size:0.7rem;"><i class="bi bi-truck me-1"></i>Kargo Bedava</span>
                                <?php endif; ?>
                            </div>

                            <div class="card-body text-center d-flex flex-column bg-white border-top p-3">
                                <?php 
                                    // Eğer çoklu kategori varsa onu göster, yoksa ana kategoriyi göster
                                    $kategori_isimleri = $urun['sub_categories'] ? $urun['sub_categories'] : $urun['cat_name'];
                                    if($kategori_isimleri): 
                                ?>
                                    <small class="text-muted fw-bold text-uppercase text-truncate" style="font-size: 0.60rem; letter-spacing: 0.5px; display: block;" title="<?= htmlspecialchars($kategori_isimleri) ?>">
                                        <?= htmlspecialchars($kategori_isimleri) ?>
                                    </small>
                                <?php endif; ?>
                                
                                <h5 class="card-title fw-bold mt-1 mb-2" style="height: 40px; overflow: hidden; font-size: 0.95rem; line-height: 1.3;">
                                    <a href="urun-detay.php?slug=<?= $urun['slug'] ?>" class="text-dark text-decoration-none" title="<?= htmlspecialchars($urun['name']) ?>">
                                        <?= htmlspecialchars($urun['name']) ?>
                                    </a>
                                </h5>

                                <div class="mt-auto pt-2 mb-3">
                                    <div class="d-flex justify-content-center align-items-center gap-2 mb-1">
                                        <span class="text-muted small text-decoration-line-through fw-bold"><?= number_format($urun['price'], 2, ',', '.') ?> ₺</span>
                                        <span class="text-danger fw-black fs-5"><?= number_format($urun['discount_price'], 2, ',', '.') ?> ₺</span>
                                    </div>
                                    <div class="bg-success bg-opacity-10 text-success fw-bold rounded-pill py-1 px-2 mx-auto d-inline-block" style="font-size: 0.70rem;">
                                        <i class="bi bi-wallet2 me-1"></i> Kazancınız: <?= number_format($kazanc, 2, ',', '.') ?> ₺
                                    </div>
                                </div>
                                
                                <?php if($stokta_yok): ?>
                                    <button class="btn btn-secondary w-100 fw-bold rounded-pill" disabled>Tükendi</button>
                                <?php else: ?>
                                    <button type="button" class="btn btn-outline-danger w-100 fw-bold rounded-pill sepete-ekle-btn btn-hover-fill" data-id="<?= $urun['id'] ?>">
                                        <i class="bi bi-cart-plus me-1"></i> Sepete Ekle
                                    </button>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            
        <?php endif; ?>

    </div>
</main>

<style>
.fw-black { font-weight: 900; }
.tracking-tight { letter-spacing: -1px; }
.product-card { transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); border: 2px solid transparent; }
.product-card:hover { transform: translateY(-8px); box-shadow: 0 15px 30px rgba(0,0,0,0.1) !important; border-color: rgba(220, 53, 69, 0.15); }
.hover-zoom:hover { transform: scale(1.08); }

.btn-hover-fill { transition: all 0.3s ease; color: #dc3545; border-color: #dc3545; }
.btn-hover-fill:hover { background-color: #dc3545; color: white; transform: scale(1.02); }

.btn-filter {
    background-color: #ffffff;
    color: #6c757d;
    border: 1px solid #dee2e6;
    transition: all 0.3s ease;
}
.btn-filter:hover {
    background-color: #ffffff;
    color: #dc3545; /* İndirim sayfasına özel üzerine gelince Kırmızı yazı */
    border-color: #dc3545;
    box-shadow: 0 4px 6px rgba(220, 53, 69, 0.1);
}
</style>

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