<?php
/**
 * الصفحة الرئيسية
 */
require_once __DIR__ . '/config/config.php';

$page_title = 'الرئيسية';
$hero_title = getSetting('hero_title', 'حلول تقنية متكاملة لعملك');
$hero_subtitle = getSetting('hero_subtitle', 'برامج - كاميرات مراقبة - تطبيقات - دعم فني');
$hero_description = getSetting('hero_description', 'نقدم لكم حلولًا تقنية احترافية وشاملة لجميع احتياجاتكم');

// جلب الخدمات المميزة
$services = db()->fetchAll("SELECT * FROM services WHERE is_active = 1 ORDER BY is_featured DESC, sort_order ASC LIMIT 6");

// جلب المنتجات المميزة
$products = db()->fetchAll("SELECT * FROM products WHERE is_active = 1 ORDER BY is_featured DESC, created_at DESC LIMIT 8");

// إحصائيات
$total_orders = db()->count("SELECT COUNT(*) FROM orders");
$total_customers = db()->count("SELECT COUNT(*) FROM users");
$total_services = db()->count("SELECT COUNT(*) FROM services WHERE is_active = 1");
$total_products = db()->count("SELECT COUNT(*) FROM products WHERE is_active = 1");

include ROOT_PATH . '/includes/header.php';
?>

<!-- Hero Section -->
<section class="hero">
    <div class="container">
        <div class="hero-badge">
            <span class="hero-badge-dot"></span>
            <span>نعمل حالياً - خدمات متاحة</span>
        </div>
        <h1>
            <?= clean($hero_title) ?>
        </h1>
        <p class="hero-subtitle"><?= clean($hero_subtitle) ?></p>
        <p class="hero-description"><?= clean($hero_description) ?></p>
        <div class="hero-actions">
            <a href="<?= SITE_URL ?>/services" class="btn btn-primary btn-lg">
                استعرض خدماتنا
                <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M5 12h14M12 5l7 7-7 7"/>
                </svg>
            </a>
            <a href="<?= SITE_URL ?>/contact.php" class="btn btn-outline btn-lg">تواصل معنا</a>
        </div>
        <div class="hero-stats">
            <div class="hero-stat">
                <div class="hero-stat-value"><?= $total_services ?></div>
                <div class="hero-stat-label">خدمة</div>
            </div>
            <div class="hero-stat">
                <div class="hero-stat-value"><?= $total_products ?></div>
                <div class="hero-stat-label">منتج</div>
            </div>
            <div class="hero-stat">
                <div class="hero-stat-value"><?= $total_customers ?></div>
                <div class="hero-stat-label">عميل</div>
            </div>
            <div class="hero-stat">
                <div class="hero-stat-value"><?= $total_orders ?></div>
                <div class="hero-stat-label">طلب</div>
            </div>
        </div>
    </div>
</section>

<!-- Services Section -->
<section class="section" id="services">
    <div class="container">
        <div class="section-header">
            <span class="section-badge">خدماتنا</span>
            <h2 class="section-title">حلول متنوعة لكل احتياجاتك التقنية</h2>
            <p class="section-subtitle">نقدم باقة متكاملة من الخدمات التقنية المتميزة بأعلى مستوى من الاحترافية والجودة</p>
        </div>

        <?php if (empty($services)): ?>
            <div class="empty-state">
                <div class="empty-state-icon">🛠️</div>
                <h3>لا توجد خدمات متاحة حالياً</h3>
                <p>يتم تحديث الخدمات. يرجى التحقق لاحقاً</p>
            </div>
        <?php else: ?>
            <div class="grid grid-3">
                <?php foreach ($services as $service): ?>
                    <div class="card service-card" data-tilt>
                        <div class="service-icon">
                            <?php if (!empty($service['icon'])): ?>
                                <img src="<?= getImageUrl($service['icon']) ?>" alt="<?= clean($service['name']) ?>" style="width:36px;height:36px;">
                            <?php else: ?>
                                <svg viewBox="0 0 24 24" width="32" height="32" fill="none" stroke="currentColor" stroke-width="2">
                                    <path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/>
                                </svg>
                            <?php endif; ?>
                        </div>
                        <h3><?= clean($service['name']) ?></h3>
                        <p class="service-desc"><?= clean($service['short_description'] ?: $service['description']) ?></p>
                        <div class="service-price">
                            <?php if ($service['price_starting'] > 0): ?>
                                <?= formatPrice($service['price_starting']) ?>
                                <span class="price-label">يبدأ من</span>
                            <?php else: ?>
                                <span class="price-label">السعر حسب الطلب</span>
                            <?php endif; ?>
                        </div>
                        <a href="<?= SITE_URL ?>/orders/create_service.php?service_id=<?= $service['id'] ?>" class="btn btn-primary btn-block">
                            اطلب الخدمة
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
            <div style="text-align:center; margin-top: 40px;">
                <a href="<?= SITE_URL ?>/services" class="btn btn-outline btn-lg">عرض جميع الخدمات</a>
            </div>
        <?php endif; ?>
    </div>
</section>

<!-- Products Section -->
<section class="section" id="products">
    <div class="container">
        <div class="section-header">
            <span class="section-badge">منتجاتنا</span>
            <h2 class="section-title">منتجات تقنية بأعلى جودة</h2>
            <p class="section-subtitle">تشكيلة واسعة من المنتجات الرقمية والفعلية من برامج وكاميرات وأجهزة</p>
        </div>

        <?php if (empty($products)): ?>
            <div class="empty-state">
                <div class="empty-state-icon">📦</div>
                <h3>لا توجد منتجات متاحة حالياً</h3>
                <p>يتم تحديث المنتجات. يرجى التحقق لاحقاً</p>
            </div>
        <?php else: ?>
            <div class="grid grid-4">
                <?php foreach ($products as $product): ?>
                    <div class="card product-card" data-tilt>
                        <div class="product-image">
                            <?php if (!empty($product['image'])): ?>
                                <img src="<?= getImageUrl($product['image']) ?>" alt="<?= clean($product['name']) ?>">
                            <?php else: ?>
                                <span style="font-size:3rem;color:var(--text-muted)">📦</span>
                            <?php endif; ?>
                            <span class="product-type-badge <?= $product['type'] ?>">
                                <?= $product['type'] === 'digital' ? 'رقمي' : 'فعلي' ?>
                            </span>
                        </div>
                        <div class="product-info">
                            <h3><?= clean($product['name']) ?></h3>
                            <p class="product-desc"><?= clean($product['short_description']) ?></p>
                            <div class="product-footer">
                                <div>
                                    <div class="product-price">
                                        <?= $product['discount_price'] > 0 ? formatPrice($product['discount_price']) : formatPrice($product['price']) ?>
                                    </div>
                                    <div class="product-stock <?= $product['stock'] > 0 ? 'in-stock' : 'out-stock' ?>">
                                        <?= $product['type'] === 'digital' ? 'متوفر فوراً' : ($product['stock'] > 0 ? "متوفر ({$product['stock']})" : 'غير متوفر') ?>
                                    </div>
                                </div>
                                <a href="<?= SITE_URL ?>/products/view.php?id=<?= $product['id'] ?>" class="btn btn-primary btn-sm">
                                    تفاصيل
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <div style="text-align:center; margin-top: 40px;">
                <a href="<?= SITE_URL ?>/products" class="btn btn-outline btn-lg">عرض جميع المنتجات</a>
            </div>
        <?php endif; ?>
    </div>
</section>

<!-- Features Section -->
<section class="section" style="padding: 60px 0;">
    <div class="container">
        <div class="grid grid-3">
            <div class="card" style="text-align:center;">
                <div class="service-icon" style="background:var(--gradient-secondary)">
                    <svg viewBox="0 0 24 24" width="32" height="32" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M9 12l2 2 4-4M12 2a10 10 0 100 20 10 10 0 000-20z"/>
                    </svg>
                </div>
                <h3>دفع آمن</h3>
                <p style="color:var(--text-secondary)">طرق دفع متعددة آمنة مع التحقق الكامل من كل عملية</p>
            </div>
            <div class="card" style="text-align:center;">
                <div class="service-icon" style="background:var(--gradient-primary)">
                    <svg viewBox="0 0 24 24" width="32" height="32" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"/>
                    </svg>
                </div>
                <h3>تسليم سريع</h3>
                <p style="color:var(--text-secondary)">منتجات رقمية فورية وخدمات سريعة بأعلى جودة</p>
            </div>
            <div class="card" style="text-align:center;">
                <div class="service-icon" style="background:var(--gradient-secondary)">
                    <svg viewBox="0 0 24 24" width="32" height="32" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M18 8h1a4 4 0 010 8h-1M2 8h16v9a4 4 0 01-4 4H6a4 4 0 01-4-4V8z"/>
                        <path d="M6 1v3M10 1v3M14 1v3"/>
                    </svg>
                </div>
                <h3>دعم فني</h3>
                <p style="color:var(--text-secondary)">فريق دعم فني محترف متاح لمساعدتك في أي وقت</p>
            </div>
        </div>
    </div>
</section>

<!-- CTA Section -->
<section class="section" style="padding: 80px 0; text-align:center;">
    <div class="container">
        <div class="card" style="background:var(--gradient-card); padding: 60px 40px;">
            <h2 class="section-title">جاهز لبدء مشروعك القادم؟</h2>
            <p class="section-subtitle" style="margin-bottom:32px;">تواصل معنا اليوم واحصل على استشارة مجانية وحلول مخصصة لاحتياجاتك</p>
            <div class="hero-actions">
                <a href="<?= SITE_URL ?>/auth/register.php" class="btn btn-primary btn-lg">ابدأ الآن</a>
                <a href="<?= SITE_URL ?>/contact.php" class="btn btn-outline btn-lg">تواصل معنا</a>
            </div>
        </div>
    </div>
</section>

<?php include ROOT_PATH . '/includes/footer.php'; ?>
