<?php
// Check if user is logged in
require_once 'includes/config.php';
require_once 'includes/functions.php';
if (!isLoggedIn()) {
    $_SESSION['redirect_after_login'] = SITE_URL . '/favorites.php';
    redirect(SITE_URL . '/auth/login.php');
}

require_once 'includes/header.php';

// Get favorite products for logged in user
$favorite_products = getFavoriteProducts($pdo, $_SESSION['user_id']);
?>

<div class="container favorites-page">
    <div class="section-title">
        <h2>مفضلاتي</h2>
    </div>
    
    <?php if (empty($favorite_products)): ?>
        <div class="message info">
            <p>لم تقم بإضافة أي منتجات إلى المفضلة بعد.</p>
            <p><a href="<?php echo SITE_URL; ?>/products/index.php" class="btn btn-primary">تصفح المنتجات</a></p>
        </div>
    <?php else: ?>
        <div class="products-grid">
            <?php foreach ($favorite_products as $product): ?>
                <div class="product-card" data-product-id="<?php echo $product['id']; ?>">
                    <div class="product-image">
                        <?php if (!empty($product['image'])): ?>
                            <img src="<?php echo SITE_URL . '/' . htmlspecialchars($product['image']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                        <?php else: ?>
                            <i class="fas fa-image" style="font-size: 3rem; color: #ccc;"></i>
                        <?php endif; ?>
                        <button class="favorite-btn active" data-product-id="<?php echo $product['id']; ?>">
                            <i class="fas fa-heart"></i>
                        </button>
                    </div>
                    <div class="product-info">
                        <h3><?php echo htmlspecialchars($product['name']); ?></h3>
                        <div class="product-price">$<?php echo number_format($product['price'], 2); ?></div>
                        <a href="<?php echo SITE_URL; ?>/products/view.php?id=<?php echo $product['id']; ?>" class="btn btn-primary btn-block">عرض التفاصيل</a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.section-title {
    direction: rtl;
    text-align: center;
}

.section-title h2 {
    font-family: 'Tajawal', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.message.info p {
    direction: rtl;
    text-align: center;
}

.product-card .product-info {
    direction: rtl;
    text-align: right;
}

.product-card .product-price {
    direction: rtl;
    text-align: right;
}
</style>

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