<?php
session_start();
require_once 'config2.php'; // ملف يحتوي على اتصال قاعدة البيانات

// جلب المنتجات المميزة
$featured_products = [];
$sql = "SELECT * FROM products WHERE is_featured = 1 AND is_active = 1 LIMIT 8";
$stmt = $conn->query($sql);
$featured_products = $stmt->fetchAll();

// جلب الفئات
$categories = [];
$sql = "SELECT * FROM categories";
$stmt = $conn->query($sql);
$categories = $stmt->fetchAll();

// جلب المنتجات المفضلة من الجلسة
$favorites = $_SESSION['favorites'] ?? [];
?>

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>متجرنا - منتجات مميزة</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #4e73df;
            --secondary-color: #f8f9fc;
            --accent-color: #ff6b6b;
            --text-color: #333;
            --light-text: #858796;
            --border-color: #e3e6f0;
            --discount-color: #e74a3b;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Tajawal', sans-serif;
        }
        
        body {
            background-color: #f8f9fc;
            color: var(--text-color);
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 15px;
        }
        
        /* Header */
        header {
            background-color: white;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            position: sticky;
            top: 0;
            z-index: 100;
        }
        
        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0;
        }
        
        .logo {
            font-size: 24px;
            font-weight: bold;
            color: var(--primary-color);
        }
        
        .search-bar {
            display: flex;
            flex-grow: 1;
            margin: 0 20px;
        }
        
        .search-bar input {
            width: 100%;
            padding: 10px 15px;
            border: 1px solid var(--border-color);
            border-radius: 5px 0 0 5px;
            outline: none;
        }
        
        .search-bar button {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 0 5px 5px 0;
            cursor: pointer;
        }
        
        .nav-icons a {
            color: var(--text-color);
            margin-left: 15px;
            font-size: 18px;
            position: relative;
        }
        
        .nav-icons a .badge {
            position: absolute;
            top: -8px;
            right: -8px;
            background-color: var(--accent-color);
            color: white;
            border-radius: 50%;
            width: 18px;
            height: 18px;
            font-size: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        /* Hero Section */
        .hero {
            background: linear-gradient(rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), url('images/hero-bg.jpg');
            background-size: cover;
            background-position: center;
            color: white;
            padding: 80px 0;
            text-align: center;
            margin-bottom: 30px;
        }
        
        .hero h1 {
            font-size: 2.5rem;
            margin-bottom: 15px;
        }
        
        .hero p {
            font-size: 1.2rem;
            margin-bottom: 25px;
            max-width: 700px;
            margin-left: auto;
            margin-right: auto;
        }
        
        .discount-badge {
            background-color: var(--discount-color);
            color: white;
            padding: 5px 15px;
            border-radius: 20px;
            font-weight: bold;
            display: inline-block;
            margin-bottom: 20px;
            animation: pulse 2s infinite;
        }
        
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
        
        /* Main Content */
        .main-content {
            display: flex;
            margin-bottom: 50px;
        }
        
        /* Sidebar */
        .sidebar {
            width: 250px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.1);
            padding: 20px;
            margin-left: 20px;
            height: fit-content;
        }
        
        .sidebar h3 {
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid var(--border-color);
            font-size: 18px;
        }
        
        .filter-section {
            margin-bottom: 20px;
        }
        
        .filter-section h4 {
            margin-bottom: 10px;
            font-size: 16px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            cursor: pointer;
        }
        
        .filter-section h4 i {
            transition: transform 0.3s;
        }
        
        .filter-section.collapsed h4 i {
            transform: rotate(180deg);
        }
        
        .filter-section.collapsed .filter-content {
            display: none;
        }
        
        .price-range {
            margin-top: 10px;
        }
        
        .price-range-values {
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
        }
        
        .price-range-values input {
            width: 45%;
            padding: 5px;
            border: 1px solid var(--border-color);
            border-radius: 3px;
        }
        
        .category-list {
            list-style: none;
        }
        
        .category-list li {
            margin-bottom: 8px;
        }
        
        .category-list a {
            color: var(--text-color);
            text-decoration: none;
            display: flex;
            align-items: center;
        }
        
        .category-list a:hover {
            color: var(--primary-color);
        }
        
        .category-list a i {
            margin-left: 5px;
        }
        
        .filter-btn {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 5px;
            width: 100%;
            cursor: pointer;
            font-weight: bold;
            margin-top: 10px;
        }
        
        /* Products Grid */
        .products-grid {
            flex-grow: 1;
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
        }
        
        .product-card {
            background-color: white;
            border-radius: 5px;
            overflow: hidden;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.1);
            transition: transform 0.3s, box-shadow 0.3s;
        }
        
        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 0.5rem 1.5rem 0 rgba(58, 59, 69, 0.2);
        }
        
        .product-image {
            position: relative;
            height: 200px;
            overflow: hidden;
        }
        
        .product-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s;
        }
        
        .product-card:hover .product-image img {
            transform: scale(1.05);
        }
        
        .product-badges {
            position: absolute;
            top: 10px;
            left: 10px;
            display: flex;
            flex-direction: column;
            gap: 5px;
        }
        
        .badge {
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 12px;
            font-weight: bold;
            color: white;
        }
        
        .badge-featured {
            background-color: var(--primary-color);
        }
        
        .badge-discount {
            background-color: var(--discount-color);
        }
        
        .favorite-btn {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: white;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: var(--light-text);
            transition: all 0.3s;
        }
        
        .favorite-btn.active {
            color: var(--accent-color);
        }
        
        .favorite-btn:hover {
            color: var(--accent-color);
            transform: scale(1.1);
        }
        
        .product-info {
            padding: 15px;
        }
        
        .product-category {
            color: var(--light-text);
            font-size: 12px;
            margin-bottom: 5px;
        }
        
        .product-title {
            font-size: 16px;
            margin-bottom: 10px;
            font-weight: bold;
            height: 40px;
            overflow: hidden;
        }
        
        .product-price {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 10px;
        }
        
        .current-price {
            font-weight: bold;
            font-size: 18px;
            color: var(--primary-color);
        }
        
        .old-price {
            text-decoration: line-through;
            color: var(--light-text);
            font-size: 14px;
        }
        
        .discount-percentage {
            background-color: var(--secondary-color);
            color: var(--discount-color);
            padding: 2px 5px;
            border-radius: 3px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .add-to-cart {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            width: 100%;
            cursor: pointer;
            font-weight: bold;
            transition: background-color 0.3s;
        }
        
        .add-to-cart:hover {
            background-color: #2e59d9;
        }
        
        /* Responsive */
        @media (max-width: 992px) {
            .main-content {
                flex-direction: column;
            }
            
            .sidebar {
                width: 100%;
                margin-left: 0;
                margin-bottom: 20px;
            }
            
            .products-grid {
                grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            }
        }
        
        @media (max-width: 576px) {
            .navbar {
                flex-direction: column;
                gap: 15px;
            }
            
            .search-bar {
                margin: 0;
                width: 100%;
            }
            
            .products-grid {
                grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            }
            
            .product-image {
                height: 150px;
            }
        }
    </style>
</head>
<body>
    <!-- Header -->
    <header>
        <div class="container">
            <div class="navbar">
                <div class="logo">متجرنا</div>
                
                <div class="search-bar">
                    <input type="text" placeholder="ابحث عن منتجات...">
                    <button><i class="fas fa-search"></i></button>
                </div>
                
                <div class="nav-icons">
                    <a href="#"><i class="fas fa-user"></i></a>
                    <a href="#"><i class="fas fa-heart"></i> <span class="badge"><?php echo count($favorites); ?></span></a>
                    <a href="#"><i class="fas fa-shopping-cart"></i> <span class="badge">0</span></a>
                </div>
            </div>
        </div>
    </header>
    
    <!-- Hero Section -->
    <section class="hero">
        <div class="container">
            <div class="discount-badge">خصومات تصل إلى 50%</div>
            <h1>أهلاً بكم في متجرنا</h1>
            <p>اكتشف أحدث المنتجات بأفضل الأسعار والعروض الحصرية</p>
            <button class="add-to-cart" style="max-width: 200px;">تسوق الآن</button>
        </div>
    </section>
    
    <!-- Main Content -->
    <div class="container">
        <div class="main-content">
            <!-- Sidebar Filters -->
            <aside class="sidebar">
                <h3><i class="fas fa-filter"></i> تصفية المنتجات</h3>
                
                <div class="filter-section">
                    <h4>الفئة <i class="fas fa-chevron-down"></i></h4>
                    <div class="filter-content">
                        <ul class="category-list">
                            <?php foreach ($categories as $category): ?>
                            <li>
                                <a href="#">
                                    <span><?php echo htmlspecialchars($category['name']); ?></span>
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
                
                <div class="filter-section">
                    <h4>السعر <i class="fas fa-chevron-down"></i></h4>
                    <div class="filter-content">
                        <input type="range" class="price-range" min="0" max="1000" value="1000">
                        <div class="price-range-values">
                            <input type="number" placeholder="الحد الأدنى" min="0">
                            <input type="number" placeholder="الحد الأقصى" max="1000">
                        </div>
                    </div>
                </div>
                
                <div class="filter-section">
                    <h4>الخصومات <i class="fas fa-chevron-down"></i></h4>
                    <div class="filter-content">
                        <label style="display: block; margin-bottom: 8px;">
                            <input type="checkbox"> منتجات مخفضة
                        </label>
                        <label style="display: block;">
                            <input type="checkbox"> منتجات مميزة
                        </label>
                    </div>
                </div>
                
                <button class="filter-btn">تطبيق الفلتر</button>
            </aside>
            
            <!-- Products Grid -->
            <div class="products-grid">
                <?php foreach ($featured_products as $product): 
    // جلب صورة المنتج الرئيسية
    $image_sql = "SELECT image_url FROM product_images WHERE product_id = ? AND is_primary = 1 LIMIT 1";
    $stmt = $conn->prepare($image_sql);
    $stmt->execute([$product['id']]);
    $image = $stmt->fetch();
    $image_url = $image ? $image['image_url'] : 'images/default-product.jpg';
    
    // جلب اسم الفئة
    $category_name = '';
    if ($product['category_id']) {
        $cat_sql = "SELECT name FROM categories WHERE id = ?";
        $stmt = $conn->prepare($cat_sql);
        $stmt->execute([$product['category_id']]);
        $category = $stmt->fetch();
        $category_name = $category ? $category['name'] : '';
    }
    
    // حساب نسبة الخصم إن وجدت
    $discount_percentage = 0;
    if ($product['discounted_price'] && $product['price'] > 0) {
        $discount_percentage = round((($product['price'] - $product['discounted_price']) / $product['price'] * 100));
    }
    
    // التحقق إذا كان المنتج في المفضلة
    $is_favorite = in_array($product['id'], $favorites);
?>
                <div class="product-card">
                    <div class="product-image">
                        <img src="<?php echo htmlspecialchars($image_url); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                        
                        <div class="product-badges">
                            <?php if ($product['is_featured']): ?>
                            <span class="badge badge-featured">مميز</span>
                            <?php endif; ?>
                            <?php if ($discount_percentage > 0): ?>
                            <span class="badge badge-discount">-<?php echo $discount_percentage; ?>%</span>
                            <?php endif; ?>
                        </div>
                        
                        <button class="favorite-btn <?php echo $is_favorite ? 'active' : ''; ?>" data-product-id="<?php echo $product['id']; ?>">
                            <i class="fas fa-heart"></i>
                        </button>
                    </div>
                    
                    <div class="product-info">
                        <div class="product-category"><?php echo htmlspecialchars($category_name); ?></div>
                        <h3 class="product-title"><?php echo htmlspecialchars($product['name']); ?></h3>
                        
                        <div class="product-price">
                            <?php if ($product['discounted_price']): ?>
                            <span class="current-price"><?php echo number_format($product['discounted_price'], 2); ?> د.ج</span>
                            <span class="old-price"><?php echo number_format($product['price'], 2); ?> د.ج</span>
                            <?php else: ?>
                            <span class="current-price"><?php echo number_format($product['price'], 2); ?> د.ج</span>
                            <?php endif; ?>
                        </div>
                        
                        <button class="add-to-cart">أضف إلى السلة</button>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // تفعيل/تعطيل الفلتر
            $('.filter-section h4').click(function() {
                $(this).parent().toggleClass('collapsed');
            });
            
            // إضافة/إزالة من المفضلة
            $('.favorite-btn').click(function(e) {
                e.preventDefault();
                const productId = $(this).data('product-id');
                const isActive = $(this).hasClass('active');
                
                // إرسال طلب AJAX لتحديث الجلسة
                $.ajax({
                    url: 'update_favorite.php',
                    method: 'POST',
                    data: { product_id: productId, action: isActive ? 'remove' : 'add' },
                    success: function(response) {
                        if (response.success) {
                            // تحديث الزر
                            const btn = $(`.favorite-btn[data-product-id="${productId}"]`);
                            btn.toggleClass('active');
                            
                            // تحديث العداد
                            const favoriteCount = response.favorites_count || 0;
                            $('.nav-icons .fa-heart').siblings('.badge').text(favoriteCount);
                        }
                    }
                });
            });
            
            // البحث عن المنتجات
            $('.search-bar button').click(function() {
                const searchTerm = $('.search-bar input').val().trim();
                if (searchTerm) {
                    // هنا يمكنك إرسال طلب البحث أو تصفية المنتجات
                    alert('سيتم البحث عن: ' + searchTerm);
                }
            });
            
            // يمكنك إضافة المزيد من التفاعلات هنا
        });
    </script>
</body>
</html>