<?php
session_start();
require_once '../includes/product-card.php';
require_once '../config/database.php';
require_once '../models/user.php';

$database = new Database();
$db = $database->getConnection();
$conn = $db; // للتوافق مع unified-header

// Initialize user model for footer
$user_model = new User($conn);

$page_title = 'العروض الخاصة';
$base_path = '../';

// جلب العروض النشطة
$now = date('Y-m-d H:i:s');
$stmt = $db->prepare("SELECT * FROM offers 
                      WHERE is_active = 1 
                      AND start_date <= ? 
                      AND end_date >= ?
                      ORDER BY priority ASC, created_at DESC");
$stmt->execute([$now, $now]);
$offers = $stmt->fetchAll();

include '../includes/unified-header.php';
?>

<!-- Hero Section -->
<section class="relative bg-gradient-to-br from-pink-50 to-purple-50 py-16">
    <div class="container">
        <div class="text-center max-w-3xl mx-auto">
            <h1 class="text-4xl md:text-5xl font-bold text-gray-900 mb-4">
                🎁 العروض الخاصة
            </h1>
            <p class="text-lg text-gray-600">
                اكتشف أفضل العروض والخصومات على منتجاتنا المميزة
            </p>
        </div>
    </div>
</section>

<!-- Offers Grid -->
<section class="py-12">
    <div class="container">
        <?php if (empty($offers)): ?>
            <div class="text-center py-16">
                <div class="w-24 h-24 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i class="fas fa-tag text-4xl text-gray-400"></i>
                </div>
                <h3 class="text-xl font-bold text-gray-800 mb-2">لا توجد عروض حالياً</h3>
                <p class="text-gray-600 mb-6">تابعنا للحصول على أحدث العروض والخصومات</p>
                <a href="products.php" class="btn-primary">
                    تصفح المنتجات
                </a>
            </div>
        <?php else: ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                <?php foreach ($offers as $offer): ?>
                    <?php
                    $days_left = ceil((strtotime($offer['end_date']) - time()) / 86400);
                    ?>
                    <div class="bg-white rounded-2xl shadow-lg overflow-hidden hover:shadow-xl transition-all duration-300 transform hover:-translate-y-1">
                        <!-- صورة العرض -->
                        <div class="relative h-48 bg-gradient-to-br from-pink-400 to-purple-500">
                            <?php if ($offer['image']): ?>
                                <img src="../<?php echo $offer['image']; ?>" 
                                     alt="<?php echo htmlspecialchars($offer['title']); ?>"
                                     class="w-full h-full object-cover">
                            <?php else: ?>
                                <div class="w-full h-full flex items-center justify-center">
                                    <i class="fas fa-tag text-white text-6xl opacity-50"></i>
                                </div>
                            <?php endif; ?>
                            
                            <!-- شارة الخصم -->
                            <div class="absolute top-4 right-4 bg-white rounded-full px-4 py-2 shadow-lg">
                                <span class="text-2xl font-bold text-pink-600">
                                    <?php echo $offer['discount_value']; ?>
                                    <?php echo $offer['discount_type'] === 'percentage' ? '%' : 'ج.م'; ?>
                                </span>
                            </div>
                            
                            <!-- الوقت المتبقي -->
                            <?php if ($days_left <= 7): ?>
                                <div class="absolute bottom-4 left-4 bg-red-500 text-white px-3 py-1 rounded-full text-sm font-bold">
                                    <i class="fas fa-clock ml-1"></i>
                                    <?php echo $days_left; ?> يوم متبقي
                                </div>
                            <?php endif; ?>
                        </div>
                        
                        <!-- محتوى العرض -->
                        <div class="p-6">
                            <h3 class="text-xl font-bold text-gray-900 mb-2">
                                <?php echo htmlspecialchars($offer['title']); ?>
                            </h3>
                            
                            <p class="text-gray-600 mb-4">
                                <?php echo htmlspecialchars($offer['description']); ?>
                            </p>
                            
                            <!-- تفاصيل العرض -->
                            <div class="space-y-2 mb-4 text-sm">
                                <?php if ($offer['min_purchase'] > 0): ?>
                                    <div class="flex items-center text-gray-600">
                                        <i class="fas fa-shopping-cart ml-2 text-pink-500"></i>
                                        الحد الأدنى: <?php echo $offer['min_purchase']; ?> ج.م
                                    </div>
                                <?php endif; ?>
                                
                                <?php if ($offer['max_discount']): ?>
                                    <div class="flex items-center text-gray-600">
                                        <i class="fas fa-percentage ml-2 text-pink-500"></i>
                                        الحد الأقصى للخصم: <?php echo $offer['max_discount']; ?> ج.م
                                    </div>
                                <?php endif; ?>
                                
                                <div class="flex items-center text-gray-600">
                                    <i class="fas fa-calendar ml-2 text-pink-500"></i>
                                    ينتهي في: <?php echo date('Y-m-d', strtotime($offer['end_date'])); ?>
                                </div>
                            </div>
                            
                            <!-- زر التسوق -->
                            <a href="products.php" class="block w-full text-center py-3 bg-gradient-to-r from-pink-500 to-purple-500 text-white font-bold rounded-lg hover:from-pink-600 hover:to-purple-600 transition-all">
                                <i class="fas fa-shopping-bag ml-2"></i>
                                تسوق الآن
                            </a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

<!-- CTA Section -->
<section class="py-16 bg-gradient-to-br from-pink-500 to-purple-600">
    <div class="container text-center">
        <h2 class="text-3xl font-bold text-white mb-4">
            لا تفوت العروض الحصرية!
        </h2>
        <p class="text-white/90 text-lg mb-6">
            اشترك في نشرتنا البريدية للحصول على أحدث العروض والخصومات
        </p>
        <div class="max-w-md mx-auto flex gap-2">
            <input type="email" placeholder="بريدك الإلكتروني" 
                   class="flex-1 px-4 py-3 rounded-lg focus:outline-none">
            <button class="px-6 py-3 bg-white text-pink-600 font-bold rounded-lg hover:bg-gray-100">
                اشترك
            </button>
        </div>
    </div>
</section>

<?php include '../includes/unified-footer.php'; ?>
