<?php
require_once '../includes/config.php';
require_once '../includes/functions.php';
require_once '../includes/header.php';

// Get favorite products for logged in users
$favorite_product_ids = [];
if (isLoggedIn()) {
    $favorite_products = getFavoriteProducts($pdo, $_SESSION['user_id']);
    foreach ($favorite_products as $fav) {
        $favorite_product_ids[] = $fav['id'];
    }
}

// Get category filter
$category_filter = isset($_GET['category']) ? sanitizeInput($_GET['category']) : '';

// Get price range filters
$min_price = isset($_GET['min_price']) ? floatval($_GET['min_price']) : 0;
$max_price = isset($_GET['max_price']) ? floatval($_GET['max_price']) : 0;

// Get products based on filters
if ($category_filter) {
    $products = getProductsByCategory($pdo, $category_filter);
} else {
    $products = getAllProducts($pdo);
}

// Apply price filtering
if ($min_price > 0 || $max_price > 0) {
    $filtered_products = [];
    foreach ($products as $product) {
        if (($min_price == 0 || $product['price'] >= $min_price) && 
            ($max_price == 0 || $product['price'] <= $max_price)) {
            $filtered_products[] = $product;
        }
    }
    $products = $filtered_products;
}

// Get categories for display
$categories = getCategories($pdo);

?>

<div class="container products-page">
    <div class="section-title">
        <h2><?php echo $category_filter ? htmlspecialchars(translateCategoryToArabic($category_filter)) : 'منتجاتنا'; ?></h2>
        <?php if ($category_filter): ?>
            <p class="category-description">اكتشف أحدث مجموعتنا من <?php echo strtolower(htmlspecialchars(translateCategoryToArabic($category_filter))); ?></p>
        <?php endif; ?>
    </div>
    
    <!-- Filters -->
    <div class="filters">
        <form method="GET" action="" class="filter-form">
            <div class="filter-row">
                <div class="filter-group">
                    <label for="category">التصنيف:</label>
                    <select name="category" id="category">
                        <option value="">جميع التصنيفات</option>
                        <?php foreach ($categories as $category): ?>
                            <option value="<?php echo htmlspecialchars($category['category']); ?>" <?php echo ($category_filter === $category['category']) ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars(translateCategoryToArabic($category['category'])); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="filter-group">
                    <label for="min_price">الحد الأدنى للسعر:</label>
                    <input type="number" name="min_price" id="min_price" step="0.01" min="0" value="<?php echo $min_price; ?>" placeholder="0$">
                </div>
                
                <div class="filter-group">
                    <label for="max_price">الحد الأقصى للسعر:</label>
                    <input type="number" name="max_price" id="max_price" step="0.01" min="0" value="<?php echo $max_price; ?>" placeholder="1000$">
                </div>
                
                <div class="filter-buttons">
                    <button type="submit" class="btn btn-primary">تصفية</button>
                    <?php if ($category_filter || $min_price > 0 || $max_price > 0): ?>
                        <a href="<?php echo SITE_URL; ?>/products/index.php" class="btn btn-outline">مسح</a>
                    <?php endif; ?>
                </div>
            </div>
        </form>
    </div>
    
    <?php if (empty($products)): ?>
        <div class="empty-products">
            <i class="fas fa-box-open"></i>
            <h3>لم يتم العثور على منتجات</h3>
            <p>لم نتمكن من العثور على أي منتجات تطابق معاييرك. حاول تعديل عوامل التصفية.</p>
        </div>
    <?php else: ?>
        <div class="products-grid">
            <?php foreach ($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: ?>
                            <div class="image-placeholder">
                                <i class="fas fa-image"></i>
                            </div>
                        <?php endif; ?>
                        <?php if (isLoggedIn()): ?>
                            <button class="favorite-btn <?php echo in_array($product['id'], $favorite_product_ids) ? 'active' : ''; ?>" data-product-id="<?php echo $product['id']; ?>">
                                <i class="fas fa-heart"></i>
                            </button>
                        <?php endif; ?>
                    </div>
                    <div class="product-info">
                        <h3><?php echo htmlspecialchars($product['name']); ?></h3>
                        <p class="product-category"><?php echo htmlspecialchars(translateCategoryToArabic($product['category'])); ?></p>
                        <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>
.filters {
    direction: rtl;
    text-align: right;
}

.filter-row {
    direction: rtl;
    text-align: right;
}

.filter-group label {
    direction: rtl;
    text-align: right;
}

.filter-group input,
.filter-group select {
    direction: rtl;
}

.filter-buttons {
    direction: rtl;
    text-align: right;
}

.category-description {
    direction: rtl;
    text-align: center;
}

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

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

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

.empty-products {
    direction: rtl;
    text-align: center;
}

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

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

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