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

// Proje ana dizinini belirle
$kok_dizin = "/magaza/";

if (!isset($_SESSION['user_id'])) { 
    header("Location: " . $kok_dizin . "giris.php"); 
    exit; 
}

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

// Favori ürünleri çek
$stmt = $db->prepare("
    SELECT f.id as fav_id, p.*, b.name as brand_name,
    COALESCE(
        (SELECT image_path FROM product_images WHERE product_id = p.id AND is_main = 1 LIMIT 1),
        b.default_cover,
        'assets/img/no-image.jpg'
    ) as image
    FROM favorites f
    JOIN products p ON f.product_id = p.id
    LEFT JOIN brands b ON p.brand_id = b.id
    WHERE f.user_id = ?
    ORDER BY f.created_at DESC
");
$stmt->execute([$user_id]);
$favoriler = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Favorilerim";
require_once '../header.php';
?>

<main class="container py-5 text-dark">
    <div class="row">
        <div class="col-lg-3 mb-4">
            <?php require_once 'sidebar.php'; ?>
        </div>
        <div class="col-lg-9">
            <div class="card shadow-sm border-0 rounded-4 p-4">
                <h4 class="fw-bold mb-4"><i class="bi bi-heart-fill text-danger me-2"></i>Favori Ürünlerim</h4>
                
                <?php if(empty($favoriler)): ?>
                    <div class="text-center py-5">
                        <i class="bi bi-heartbreak text-muted mb-3" style="font-size: 4rem;"></i>
                        <h5 class="text-muted">Favorilerinizde henüz ürün bulunmuyor.</h5>
                        <a href="index.php" class="btn btn-primary mt-3 rounded-pill px-4 fw-bold">Alışverişe Başla</a>
                    </div>
                <?php else: ?>
                    <div class="row row-cols-1 row-cols-md-2 g-3">
                        <?php foreach($favoriler as $f): 
                            $fiyat = $f['discount_price'] > 0 ? $f['discount_price'] : $f['price'];
                        ?>
                            <div class="col" id="fav-row-<?= $f['id'] ?>">
                                <div class="card h-100 border rounded-4 overflow-hidden shadow-sm">
                                    <div class="row g-0">
                                        <div class="col-4 bg-light d-flex align-items-center justify-content-center p-2">
                                            <img src="<?= $f['image'] ?>" class="img-fluid rounded" style="max-height: 120px; object-fit: contain;">
                                        </div>
                                        <div class="col-8">
                                            <div class="card-body p-3">
                                                <small class="text-muted text-uppercase fw-bold"><?= $f['brand_name'] ?></small>
                                                <h6 class="fw-bold text-truncate mb-1"><?= $f['name'] ?></h6>
                                                <div class="mb-3">
                                                    <span class="text-primary fw-bold fs-5"><?= number_format($fiyat, 2, ',', '.') ?> ₺</span>
                                                </div>
                                                <div class="d-flex gap-2">
                                                    <a href="urun/<?= $f['slug'] ?>" class="btn btn-sm btn-outline-secondary px-3">İncele</a>
                                                    <button type="button" class="btn btn-sm btn-danger px-3 remove-fav" data-pid="<?= $f['id'] ?>">
                                                        <i class="bi bi-trash"></i>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</main>

<script>
document.querySelectorAll('.remove-fav').forEach(btn => {
    btn.addEventListener('click', function() {
        let pid = this.getAttribute('data-pid');
        let fd = new FormData();
        fd.append('action', 'toggle_fav');
        fd.append('product_id', pid);

        // Fetch adresi güncellendi
        fetch('ajax-user-actions.php', { method: 'POST', body: fd })
        .then(res => res.json())
        .then(data => {
            if(data.status === 'success') location.reload();
        });
    });
});
</script>

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