<?php
/**
 * AliTrade B2B Platform - Homepage
 * Alibaba-style B2B Marketplace Landing Page
 */

require_once __DIR__ . '/includes/header.php';

// Fetch featured products
$featured_products = db_fetch_all("
    SELECT p.*, c.name_ar AS cat_name_ar, c.name_en AS cat_name_en, u.company_name, u.country, u.membership_tier, u.years_in_business, u.is_verified
    FROM `products` p
    JOIN `categories` c ON p.category_id = c.id
    JOIN `users` u ON p.vendor_id = u.id
    WHERE p.status = 'approved' AND p.is_featured = 1
      AND (u.subscription_status IS NULL OR u.subscription_status = 'active')
    ORDER BY p.id DESC LIMIT 8
");

// Fetch verified suppliers
$suppliers = db_fetch_all("
    SELECT * FROM `users`
    WHERE `role` = 'vendor' AND `is_verified` = 1
      AND (subscription_status IS NULL OR subscription_status = 'active')
    ORDER BY `years_in_business` DESC LIMIT 4
");

// Fetch latest RFQs
$latest_rfqs = db_fetch_all("
    SELECT r.*, c.name_ar AS cat_name_ar, c.name_en AS cat_name_en, u.country AS buyer_country
    FROM `rfqs` r
    JOIN `categories` c ON r.category_id = c.id
    JOIN `users` u ON r.buyer_id = u.id
    WHERE r.status = 'open'
    ORDER BY r.id DESC LIMIT 3
");

// Personalized browsing recommendations
$recommended_products = get_recommended_products(4);

// Dynamic Homepage CMS Phrases
$hero_badge = __t(get_setting('hero_badge_ar', 'المنصة المعتمدة لتوريد المصانع العالمية مع حماية الضمان (Escrow)'), get_setting('hero_badge_en', 'Verified Factory Sourcing Platform with Escrow Protection'));
$hero_title = __t(get_setting('hero_title_ar', 'استورد مباشرة من كبرى المصانع العالمية بأسعار الجملة الحقيقية'), get_setting('hero_title_en', 'Source Direct from Global Factories at True Wholesale Prices'));
$hero_desc  = __t(get_setting('hero_desc_ar', 'تسوّق بأمان مع ضمان Trade Assurance، شرائح أسعار مخفضة للكميات الكبيرة، نظام تفاوض مباشر، وشحن دولي سريع حتى مستودعاتك.'), get_setting('hero_desc_en', 'Buy wholesale safely with Trade Assurance escrow, volume-tiered discounts, live price negotiation, and full logistics handling.'));
$hero_btn1  = __t(get_setting('hero_btn1_ar', 'ابدأ الآن - استكشف عروض المصانع'), get_setting('hero_btn1_en', 'Start Now - Explore Catalog'));
$hero_btn2  = __t(get_setting('hero_btn2_ar', 'اطلب تسعير خاص (RFQ)'), get_setting('hero_btn2_en', 'Post an RFQ Request'));

// Bottom "ابدأ الآن" CTA Phrases
$cta_badge  = __t(get_setting('cta_badge_ar', 'انضم الآن لأكبر شبكة توريد صناعي'), get_setting('cta_badge_en', 'Join The Largest Industrial Sourcing Network'));
$cta_title  = __t(get_setting('cta_title_ar', 'هل أنت مصنع أو مورد؟ ابدأ البيع بالجملة الآن وحقق نمواً دولياً'), get_setting('cta_title_en', 'Are you a manufacturer? Start wholesale selling now'));
$cta_desc   = __t(get_setting('cta_desc_ar', 'انضم إلى آلاف المصانع المعتمدة، اعرض منتجاتك، واستقبل طلبات الشراء الكبيرة والتفاوض المباشر مع التجار.'), get_setting('cta_desc_en', 'Join thousands of verified suppliers, showcase products, and receive bulk purchase orders with guaranteed Escrow security.'));
$cta_btn1   = __t(get_setting('cta_btn1_ar', 'ابدأ الآن - افتح متجر مصنع'), get_setting('cta_btn1_en', 'Start Now - Open Factory Store'));
$cta_btn2   = __t(get_setting('cta_btn2_ar', 'ابدأ الآن - تصفح المنتجات'), get_setting('cta_btn2_en', 'Start Now - Browse Catalog'));
?>

<!-- Hero Section: Alibaba B2B Style -->
<section class="bg-gradient-to-br from-slate-900 via-slate-800 to-orange-950 text-white py-6 sm:py-12 px-3 sm:px-4 relative overflow-hidden">
    <div class="max-w-7xl mx-auto grid grid-cols-1 lg:grid-cols-12 gap-6 sm:gap-8 items-center relative z-10">
        
        <!-- Hero Text & Value Proposition -->
        <div class="lg:col-span-7 space-y-4 sm:space-y-6">
            <div class="inline-flex items-center gap-2 bg-orange-500/20 text-orange-400 border border-orange-500/30 px-3 py-1 sm:px-3.5 sm:py-1.5 rounded-full text-[11px] sm:text-xs font-bold">
                <i class="fa-solid fa-award shrink-0"></i>
                <span class="truncate"><?= htmlspecialchars($hero_badge) ?></span>
            </div>
            
            <h1 class="text-xl sm:text-3xl md:text-5xl font-black text-white leading-snug sm:leading-tight">
                <?= htmlspecialchars($hero_title) ?>
            </h1>
            
            <p class="text-slate-300 text-xs sm:text-sm md:text-base leading-relaxed max-w-xl">
                <?= htmlspecialchars($hero_desc) ?>
            </p>

            <!-- Quick Action Buttons -->
            <div class="grid grid-cols-2 sm:flex sm:flex-wrap gap-2.5 sm:gap-4 pt-1 sm:pt-2">
                <a href="<?= BASE_URL ?>/products.php" class="bg-gradient-to-r from-orange-500 to-amber-500 hover:from-orange-600 hover:to-amber-600 text-white font-black px-3 sm:px-8 py-3 sm:py-3.5 rounded-xl shadow-lg shadow-orange-500/30 transition flex items-center justify-center gap-1.5 sm:gap-2 text-xs sm:text-sm text-center">
                    <i class="fa-solid fa-magnifying-glass"></i>
                    <span class="truncate"><?= htmlspecialchars($hero_btn1) ?></span>
                </a>
                
                <a href="<?= BASE_URL ?>/rfq.php?action=create" class="bg-slate-800 hover:bg-slate-700 text-white border border-slate-600 font-bold px-3 sm:px-6 py-3 sm:py-3.5 rounded-xl transition flex items-center justify-center gap-1.5 sm:gap-2 text-xs sm:text-sm text-center">
                    <i class="fa-solid fa-paper-plane text-orange-400"></i>
                    <span class="truncate"><?= htmlspecialchars($hero_btn2) ?></span>
                </a>
            </div>

            <!-- Key Trust Metrics -->
            <div class="grid grid-cols-3 gap-2 sm:gap-4 pt-4 sm:pt-6 border-t border-slate-800 text-slate-300 text-[11px] sm:text-xs">
                <div>
                    <div class="text-base sm:text-xl font-black text-amber-400">+15,000</div>
                    <div class="text-slate-400"><?= __t('مصنع معتمد', 'Verified Factories') ?></div>
                </div>
                <div>
                    <div class="text-base sm:text-xl font-black text-orange-400">100%</div>
                    <div class="text-slate-400"><?= __t('حماية أموال الضمان', 'Escrow Protected') ?></div>
                </div>
                <div>
                    <div class="text-base sm:text-xl font-black text-emerald-400">0$</div>
                    <div class="text-slate-400"><?= __t('رسوم فتح حساب المشتري', 'Free Buyer Access') ?></div>
                </div>
            </div>
        </div>

        <!-- Hero RFQ Fast Box -->
        <div class="lg:col-span-5">
            <div class="bg-white text-slate-800 rounded-2xl sm:rounded-3xl p-4 sm:p-6 shadow-2xl border border-slate-100">
                <div class="flex items-center justify-between pb-3 sm:pb-4 border-b border-slate-100 mb-3 sm:mb-4">
                    <div class="flex items-center gap-2">
                        <div class="w-8 h-8 rounded-lg bg-orange-100 text-orange-600 flex items-center justify-center font-bold">
                            <i class="fa-solid fa-bolt"></i>
                        </div>
                        <h3 class="font-extrabold text-slate-900 text-xs sm:text-sm"><?= __t('طلب تسعير فوري (Fast RFQ)', 'Instant RFQ Sourcing') ?></h3>
                    </div>
                    <span class="text-[10px] sm:text-[11px] bg-emerald-50 text-emerald-700 font-bold px-2 py-0.5 rounded border border-emerald-200">
                        <?= __t('عروض خلال 24 ساعة', 'Quotes in 24h') ?>
                    </span>
                </div>

                <form action="<?= BASE_URL ?>/rfq.php?action=create" method="POST" class="space-y-3">
                    <div>
                        <label class="block text-xs font-bold text-slate-700 mb-1"><?= __t('اسم المنتج أو السلعة المطلوبة', 'Product or Material Name') ?></label>
                        <input type="text" name="title" required placeholder="<?= __t('مثال: تيشيرتات قطنية 240 GSM مع تطريز شعار', 'e.g., 550W Tier 1 Solar Panels') ?>" class="w-full bg-slate-50 border border-slate-200 rounded-xl px-3.5 py-2 text-xs focus:ring-2 focus:ring-orange-500/20 focus:outline-none">
                    </div>

                    <div class="grid grid-cols-2 gap-3">
                        <div>
                            <label class="block text-xs font-bold text-slate-700 mb-1"><?= __t('الكمية المستهدفة', 'Target Quantity') ?></label>
                            <input type="number" name="target_quantity" required placeholder="500" min="1" class="w-full bg-slate-50 border border-slate-200 rounded-xl px-3.5 py-2 text-xs focus:ring-2 focus:ring-orange-500/20 focus:outline-none">
                        </div>
                        <div>
                            <label class="block text-xs font-bold text-slate-700 mb-1"><?= __t('الوحدة', 'Unit') ?></label>
                            <select name="target_unit" class="w-full bg-slate-50 border border-slate-200 rounded-xl px-3.5 py-2 text-xs focus:ring-2 focus:ring-orange-500/20 focus:outline-none">
                                <option value="pcs"><?= __t('قطعة (Pieces)', 'Pieces') ?></option>
                                <option value="sets"><?= __t('طقم / مجموعة (Sets)', 'Sets') ?></option>
                                <option value="boxes"><?= __t('صندوق / كرتون (Boxes)', 'Boxes') ?></option>
                                <option value="tons"><?= __t('طن (Tons)', 'Tons') ?></option>
                            </select>
                        </div>
                    </div>

                    <div>
                        <label class="block text-xs font-bold text-slate-700 mb-1"><?= __t('الدولة / المحافظة وجهة الشحن', 'Destination') ?></label>
                        <input type="text" name="destination_country" required value="العراق - بغداد" class="w-full bg-slate-50 border border-slate-200 rounded-xl px-3.5 py-2 text-xs focus:ring-2 focus:ring-orange-500/20 focus:outline-none">
                    </div>

                    <button type="submit" class="w-full bg-orange-600 hover:bg-orange-700 text-white font-bold py-2.5 sm:py-3 rounded-xl transition text-xs shadow-md shadow-orange-600/20 flex items-center justify-center gap-2 mt-2">
                        <i class="fa-solid fa-paper-plane"></i>
                        <span><?= __t('إرسال لكافة المصانع المعتمدة', 'Send to Verified Suppliers') ?></span>
                    </button>
                </form>
            </div>
        </div>

    </div>
</section>

<!-- Categories Section -->
<section class="max-w-7xl mx-auto px-3 sm:px-4 py-6 sm:py-12">
    <div class="flex items-center justify-between mb-4 sm:mb-8">
        <div>
            <h2 class="text-base sm:text-xl md:text-2xl font-black text-slate-900"><?= __t('استكشف حسب القطاع الصناعي', 'Explore by Industry Sector') ?></h2>
            <p class="text-slate-500 text-[11px] sm:text-xs mt-0.5 sm:mt-1"><?= __t('آلاف المصانع والموردين المباشرين في كافة التخصصات', 'Thousands of direct manufacturers across global supply chains') ?></p>
        </div>
        <a href="<?= BASE_URL ?>/products.php" class="text-xs font-bold text-orange-600 hover:text-orange-700 flex items-center gap-1 shrink-0">
            <span><?= __t('عرض الكل', 'View All') ?></span> <i class="fa-solid fa-chevron-left text-[10px]"></i>
        </a>
    </div>

    <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-2.5 sm:gap-4">
        <?php foreach ($nav_categories as $cat): ?>
            <a href="<?= BASE_URL ?>/products.php?category=<?= $cat['slug'] ?>" class="group bg-white rounded-2xl p-3 sm:p-4 border border-slate-200/80 hover:border-orange-400 hover:shadow-lg transition flex items-center gap-2.5 sm:gap-4">
                <div class="w-10 h-10 sm:w-12 sm:h-12 rounded-xl bg-orange-50 group-hover:bg-orange-500 text-orange-600 group-hover:text-white flex items-center justify-center text-base sm:text-xl shrink-0 transition">
                    <i class="fa-solid <?= $cat['icon'] ?>"></i>
                </div>
                <div class="min-w-0">
                    <h4 class="font-bold text-slate-800 text-[11px] sm:text-xs md:text-sm group-hover:text-orange-600 transition line-clamp-2">
                        <?= __t($cat['name_ar'], $cat['name_en']) ?>
                    </h4>
                    <span class="hidden sm:inline text-[11px] text-slate-400"><?= __t('تصفح العروض', 'Browse Deals') ?> &rarr;</span>
                </div>
            </a>
        <?php endforeach; ?>
    </div>
</section>

<!-- Featured Wholesale Products with Tiered Pricing -->
<section class="max-w-7xl mx-auto px-3 sm:px-4 py-6 sm:py-8">
    <div class="flex items-center justify-between mb-4 sm:mb-8">
        <div>
            <div class="flex items-center gap-2">
                <span class="w-2.5 h-2.5 rounded-full bg-orange-500 shrink-0"></span>
                <h2 class="text-base sm:text-xl md:text-2xl font-black text-slate-900"><?= __t('عروض الجملة المميزة (Featured Wholesale Deals)', 'Featured Wholesale Deals') ?></h2>
            </div>
            <p class="text-slate-500 text-[11px] sm:text-xs mt-0.5 sm:mt-1"><?= __t('أسعار مخفضة تعتمد على حجم الطلب مع شارة الضمان التجاري', 'Volume-based discounted pricing with Trade Assurance guarantee') ?></p>
        </div>
        <a href="<?= BASE_URL ?>/products.php" class="text-xs font-bold text-orange-600 hover:text-orange-700 flex items-center gap-1 shrink-0">
            <span><?= __t('تصفح الكل', 'All Products') ?></span> <i class="fa-solid fa-chevron-left text-[10px]"></i>
        </a>
    </div>

    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3 sm:gap-6">
        <?php foreach ($featured_products as $p): 
            $images = json_decode($p['images_json'], true) ?: ['https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=500'];
            $img = $images[0];
            $tiers = json_decode($p['tiered_pricing_json'], true) ?: [];
        ?>
            <div class="bg-white rounded-2xl border border-slate-200 overflow-hidden hover:shadow-xl hover:border-orange-400 transition flex flex-col justify-between group">
                <div>
                    <!-- Product Image -->
                    <a href="<?= BASE_URL ?>/product.php?id=<?= $p['id'] ?>" class="block relative aspect-square overflow-hidden bg-slate-100">
                        <img src="<?= htmlspecialchars($img) ?>" alt="<?= htmlspecialchars($p['title_ar']) ?>" class="w-full h-full object-cover group-hover:scale-105 transition duration-500">
                        <span class="absolute top-1.5 sm:top-2 <?= $is_ar ? 'right-1.5 sm:right-2' : 'left-1.5 sm:left-2' ?> bg-amber-500 text-white text-[9px] sm:text-[10px] font-black px-1.5 sm:px-2 py-0.5 rounded shadow">
                            <i class="fa-solid fa-award ml-0.5"></i> <?= __t('مورد ذهبي', 'Gold') ?>
                        </span>
                        <span class="absolute bottom-1.5 sm:bottom-2 <?= $is_ar ? 'left-1.5 sm:left-2' : 'right-1.5 sm:right-2' ?> bg-slate-900/80 backdrop-blur text-white text-[9px] sm:text-[10px] font-bold px-1.5 sm:px-2 py-0.5 rounded">
                            MOQ: <?= $p['moq'] ?>
                        </span>
                    </a>

                    <!-- Product Content -->
                    <div class="p-2.5 sm:p-4 space-y-1.5 sm:space-y-2">
                        <!-- Category & Title -->
                        <div class="text-[10px] sm:text-[11px] text-orange-600 font-bold truncate"><?= __t($p['cat_name_ar'], $p['cat_name_en']) ?></div>
                        <h3 class="text-xs sm:text-sm font-bold text-slate-900 line-clamp-2 group-hover:text-orange-600 transition min-h-[2.25rem]">
                            <a href="<?= BASE_URL ?>/product.php?id=<?= $p['id'] ?>">
                                <?= __t($p['title_ar'], $p['title_en']) ?>
                            </a>
                        </h3>

                        <!-- Tiered Price Preview -->
                        <div class="pt-1 sm:pt-2">
                            <div class="text-sm sm:text-lg font-black text-slate-900">
                                <?= format_price($p['base_price']) ?>
                                <span class="text-[10px] sm:text-xs font-normal text-slate-500">/ <?= $p['unit'] ?></span>
                            </div>
                            <?php if (!empty($tiers)): ?>
                                <div class="text-[10px] sm:text-[11px] text-emerald-600 font-semibold flex items-center gap-1 truncate">
                                    <i class="fa-solid fa-tags"></i>
                                    <span class="truncate"><?= __t('يبدأ من', 'From') ?> <?= format_price(end($tiers)['price']) ?></span>
                                </div>
                            <?php endif; ?>
                        </div>

                        <!-- Supplier info -->
                        <div class="pt-1.5 sm:pt-2 border-t border-slate-100 text-[10px] sm:text-[11px] text-slate-500 flex items-center justify-between gap-1">
                            <span class="truncate" title="<?= htmlspecialchars($p['company_name']) ?>">
                                <i class="fa-regular fa-building ml-0.5"></i> <?= htmlspecialchars($p['company_name']) ?>
                            </span>
                            <span class="text-slate-400 font-bold shrink-0 hidden sm:inline"><?= htmlspecialchars($p['country']) ?></span>
                        </div>
                    </div>
                </div>

                <!-- Card Footer CTA -->
                <div class="p-2.5 sm:p-4 pt-0 space-y-1.5">
                    <div class="grid grid-cols-2 gap-1.5 sm:gap-2">
                        <a href="<?= BASE_URL ?>/cart.php?quick_add=<?= $p['id'] ?>" class="w-full bg-orange-600 hover:bg-orange-700 text-white font-bold py-1.5 sm:py-2 rounded-xl text-[10px] sm:text-xs transition flex items-center justify-center gap-1 shadow-sm">
                            <i class="fa-solid fa-cart-plus text-[10px]"></i>
                            <span><?= __t('أضف للسلة', 'Add Cart') ?></span>
                        </a>
                        <a href="<?= BASE_URL ?>/chat.php?vendor_id=<?= $p['vendor_id'] ?>&product_id=<?= $p['id'] ?>" class="w-full bg-emerald-600 hover:bg-emerald-700 text-white font-bold py-1.5 sm:py-2 rounded-xl text-[10px] sm:text-xs transition flex items-center justify-center gap-1 shadow-sm">
                            <i class="fa-solid fa-comments text-[10px]"></i>
                            <span><?= __t('محادثة التاجر', 'Chat') ?></span>
                        </a>
                    </div>
                    <a href="<?= BASE_URL ?>/product.php?id=<?= $p['id'] ?>" class="w-full bg-slate-100 hover:bg-slate-800 hover:text-white text-slate-800 font-bold py-1.5 rounded-xl text-[10px] sm:text-xs transition flex items-center justify-center gap-1">
                        <span><?= __t('التفاصيل والتفاوض', 'Details & Negotiate') ?></span>
                    </a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>

<?php if (!empty($recommended_products)): ?>
<!-- Personalized Recommendations Based on Browsing -->
<section class="max-w-7xl mx-auto px-3 sm:px-4 py-6 sm:py-8 bg-slate-50/70 rounded-2xl sm:rounded-3xl my-4 sm:my-6 border border-slate-200/60">
    <div class="flex items-center justify-between mb-4 sm:mb-6">
        <div>
            <div class="flex items-center gap-2">
                <span class="w-2.5 h-2.5 rounded-full bg-blue-500 shrink-0"></span>
                <h2 class="text-base sm:text-xl md:text-2xl font-black text-slate-900"><?= __t('بضائع قد تهمك (بناءً على تصفحك الأخير)', 'Recommended For You') ?></h2>
            </div>
            <p class="text-slate-500 text-[11px] sm:text-xs mt-0.5 sm:mt-1"><?= __t('منتجات مخصصة لك وفقاً لاهتماماتك والقطاعات التي استعرضتها مؤخراً', 'Personalized recommendations based on categories you recently viewed') ?></p>
        </div>
        <a href="<?= BASE_URL ?>/products.php" class="text-xs font-bold text-blue-600 hover:text-blue-700 flex items-center gap-1 shrink-0">
            <span><?= __t('المزيد', 'More') ?></span> &rarr;
        </a>
    </div>

    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3 sm:gap-6">
        <?php foreach ($recommended_products as $rp): 
            $rp_imgs = json_decode($rp['images_json'], true) ?: ['https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=500'];
            $rp_img = $rp_imgs[0];
        ?>
            <div class="bg-white rounded-2xl border border-slate-200 overflow-hidden hover:shadow-xl hover:border-blue-400 transition flex flex-col justify-between group">
                <div>
                    <a href="<?= BASE_URL ?>/product.php?id=<?= $rp['id'] ?>" class="block relative aspect-square overflow-hidden bg-slate-100">
                        <img src="<?= htmlspecialchars($rp_img) ?>" alt="<?= htmlspecialchars($rp['title_ar']) ?>" class="w-full h-full object-cover group-hover:scale-105 transition duration-500">
                        <span class="absolute top-1.5 sm:top-2 <?= $is_ar ? 'right-1.5 sm:right-2' : 'left-1.5 sm:left-2' ?> bg-blue-600 text-white text-[9px] sm:text-[10px] font-black px-1.5 sm:px-2 py-0.5 rounded shadow">
                            <i class="fa-solid fa-sparkles ml-0.5"></i> <?= __t('مقترح لك', 'Suggested') ?>
                        </span>
                        <span class="absolute bottom-1.5 sm:bottom-2 <?= $is_ar ? 'left-1.5 sm:left-2' : 'right-1.5 sm:right-2' ?> bg-slate-900/80 backdrop-blur text-white text-[9px] sm:text-[10px] font-bold px-1.5 sm:px-2 py-0.5 rounded">
                            MOQ: <?= $rp['moq'] ?>
                        </span>
                    </a>
                    <div class="p-2.5 sm:p-4 space-y-1.5 sm:space-y-2">
                        <span class="text-[10px] sm:text-[11px] font-bold text-blue-600 block truncate"><?= __t($rp['cat_name_ar'] ?? '', $rp['cat_name_en'] ?? '') ?></span>
                        <h3 class="font-bold text-slate-800 text-xs sm:text-sm line-clamp-2 group-hover:text-blue-600 transition min-h-[2.25rem]">
                            <a href="<?= BASE_URL ?>/product.php?id=<?= $rp['id'] ?>">
                                <?= __t($rp['title_ar'], $rp['title_en']) ?>
                            </a>
                        </h3>
                        <div class="pt-1.5 sm:pt-2 border-t border-slate-100">
                            <span class="text-[10px] text-slate-400 block"><?= __t('السعر المبدئي', 'Starting Price') ?></span>
                            <span class="text-sm sm:text-base font-black text-slate-900"><?= format_price($rp['base_price']) ?></span>
                            <span class="text-[10px] sm:text-[11px] text-slate-500"> / <?= $rp['unit'] ?></span>
                        </div>
                    </div>
                </div>
                <div class="p-2.5 sm:p-4 pt-0 grid grid-cols-2 gap-1.5 sm:gap-2">
                    <a href="<?= BASE_URL ?>/cart.php?quick_add=<?= $rp['id'] ?>" class="w-full bg-orange-600 hover:bg-orange-700 text-white font-bold py-1.5 sm:py-2 rounded-xl text-[10px] sm:text-xs transition flex items-center justify-center gap-1 shadow-sm">
                        <i class="fa-solid fa-cart-plus text-[10px]"></i>
                        <span><?= __t('أضف للسلة', 'Add Cart') ?></span>
                    </a>
                    <a href="<?= BASE_URL ?>/product.php?id=<?= $rp['id'] ?>" class="w-full bg-blue-50 hover:bg-blue-600 hover:text-white text-blue-700 font-bold py-1.5 sm:py-2 rounded-xl text-[10px] sm:text-xs transition flex items-center justify-center gap-1">
                        <span><?= __t('التفاصيل', 'Details') ?></span>
                    </a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>
<?php endif; ?>

<!-- Top Verified Factories Spotlight -->
<section class="max-w-7xl mx-auto px-4 py-12">
    <div class="bg-gradient-to-r from-amber-50 to-orange-50 border border-amber-200/80 rounded-3xl p-6 md:p-8">
        <div class="flex flex-wrap items-center justify-between gap-4 mb-6">
            <div>
                <span class="bg-amber-500 text-white text-[11px] font-black px-2.5 py-1 rounded-full uppercase tracking-wider inline-block mb-1">
                    <i class="fa-solid fa-circle-check ml-1"></i> Verified Suppliers Spotlight
                </span>
                <h3 class="text-xl md:text-2xl font-black text-slate-900"><?= __t('أبرز المصانع المعتمدة بشارة Gold Supplier', 'Featured Verified Gold Suppliers') ?></h3>
            </div>
            <a href="<?= BASE_URL ?>/products.php?filter=gold" class="bg-white border border-amber-300 text-slate-800 hover:bg-amber-500 hover:text-white font-bold text-xs px-5 py-2.5 rounded-xl transition">
                <?= __t('تصفح كافة المصانع المعتمدة', 'View All Verified Suppliers') ?>
            </a>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <?php foreach ($suppliers as $sup): ?>
                <div class="bg-white p-5 rounded-2xl border border-amber-200/60 shadow-sm flex flex-col justify-between space-y-4">
                    <div>
                        <div class="flex items-start justify-between gap-3">
                            <div>
                                <h4 class="font-extrabold text-slate-900 text-base flex items-center gap-2">
                                    <?= htmlspecialchars($sup['company_name']) ?>
                                </h4>
                                <p class="text-xs text-slate-500 mt-0.5">
                                    <i class="fa-solid fa-location-dot ml-1 text-slate-400"></i> <?= htmlspecialchars($sup['country']) ?> &bull; <?= htmlspecialchars($sup['name']) ?>
                                </p>
                            </div>
                            <?= render_supplier_badge($sup['membership_tier'], $sup['years_in_business'], $sup['is_verified']) ?>
                        </div>

                        <!-- Metrics -->
                        <div class="grid grid-cols-3 gap-2 mt-4 bg-slate-50 p-3 rounded-xl text-center text-xs">
                            <div>
                                <span class="text-slate-400 text-[10px] block"><?= __t('سرعة الرد', 'Response Rate') ?></span>
                                <span class="font-bold text-emerald-600"><?= $sup['response_rate'] ?>%</span>
                            </div>
                            <div>
                                <span class="text-slate-400 text-[10px] block"><?= __t('الالتزام بالمواعيد', 'On-Time Delivery') ?></span>
                                <span class="font-bold text-blue-600"><?= $sup['on_time_delivery_rate'] ?>%</span>
                            </div>
                            <div>
                                <span class="text-slate-400 text-[10px] block"><?= __t('سقف الضمان', 'Assurance Limit') ?></span>
                                <span class="font-bold text-amber-600"><?= format_price($sup['trade_assurance_limit']) ?></span>
                            </div>
                        </div>
                    </div>

                    <div class="flex items-center justify-between pt-2 border-t border-slate-100 text-xs">
                        <span class="text-slate-500 text-[11px]"><i class="fa-solid fa-id-card ml-1"></i> <?= htmlspecialchars($sup['business_license'] ?? 'Verified CR') ?></span>
                        <a href="<?= BASE_URL ?>/chat.php?vendor_id=<?= $sup['id'] ?>" class="bg-orange-50 hover:bg-orange-100 text-orange-700 font-bold px-4 py-1.5 rounded-lg transition flex items-center gap-1 text-xs">
                            <i class="fa-regular fa-comment-dots"></i> <?= __t('تواصل وتفاوض', 'Contact Factory') ?>
                        </a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</section>

<!-- RFQ Marketplace Live Ticker -->
<section class="max-w-7xl mx-auto px-4 py-8 mb-12">
    <div class="flex items-center justify-between mb-6">
        <div>
            <h3 class="text-xl font-black text-slate-900"><?= __t('طلبات عروض الأسعار الحديثة من المشترين (RFQs)', 'Recent Buyer Sourcing Requests (RFQs)') ?></h3>
            <p class="text-slate-500 text-xs"><?= __t('فرص توريد مباشرة للمصانع والتجار لتقديم عروض أسعارهم', 'Direct opportunities for manufacturers to bid on buyer requests') ?></p>
        </div>
        <a href="<?= BASE_URL ?>/rfq.php" class="text-xs font-bold text-orange-600 hover:text-orange-700">
            <?= __t('تصفح كافة الطلبات', 'Browse RFQ Market') ?> &rarr;
        </a>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
        <?php foreach ($latest_rfqs as $rfq): ?>
            <div class="bg-white p-5 rounded-2xl border border-slate-200 hover:border-orange-400 hover:shadow-lg transition flex flex-col justify-between space-y-4">
                <div>
                    <div class="flex items-center justify-between text-[11px] text-slate-400 mb-2">
                        <span class="bg-slate-100 text-slate-700 px-2 py-0.5 rounded font-bold"><?= __t($rfq['cat_name_ar'], $rfq['cat_name_en']) ?></span>
                        <span><i class="fa-solid fa-location-dot ml-1 text-slate-400"></i> <?= htmlspecialchars($rfq['destination_country']) ?></span>
                    </div>
                    <h4 class="font-bold text-slate-900 text-sm line-clamp-2"><?= htmlspecialchars($rfq['title']) ?></h4>
                    <p class="text-xs text-slate-500 mt-2 line-clamp-2"><?= htmlspecialchars($rfq['details']) ?></p>
                </div>

                <div class="pt-3 border-t border-slate-100 flex items-center justify-between text-xs">
                    <div>
                        <span class="text-[10px] text-slate-400 block"><?= __t('الكمية المطلوبة', 'Required Qty') ?></span>
                        <span class="font-black text-slate-900"><?= number_format($rfq['target_quantity']) ?> <?= $rfq['target_unit'] ?></span>
                    </div>
                    <a href="<?= BASE_URL ?>/rfq.php?id=<?= $rfq['id'] ?>" class="bg-orange-600 hover:bg-orange-700 text-white font-bold px-4 py-2 rounded-xl text-xs transition">
                        <?= __t('تقديم عرض سعر', 'Submit Quote') ?>
                    </a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>

<!-- Final Bottom CTA Section: "ابدأ الآن" -->
<section class="max-w-7xl mx-auto px-4 pb-12">
    <div class="relative overflow-hidden rounded-3xl bg-gradient-to-r from-orange-600 via-amber-600 to-orange-700 text-white p-8 md:p-12 shadow-xl shadow-orange-500/20">
        <div class="relative z-10 max-w-3xl space-y-4">
            <span class="bg-black/20 text-white font-bold px-3 py-1 rounded-full text-xs uppercase tracking-wider inline-block">
                <?= htmlspecialchars($cta_badge) ?>
            </span>
            <h2 class="text-2xl md:text-4xl font-black leading-tight">
                <?= htmlspecialchars($cta_title) ?>
            </h2>
            <p class="text-orange-100 text-sm md:text-base leading-relaxed">
                <?= htmlspecialchars($cta_desc) ?>
            </p>
            <div class="flex flex-wrap items-center gap-4 pt-4">
                <a href="<?= BASE_URL ?>/register.php?role=vendor" class="bg-white hover:bg-slate-100 text-slate-900 font-black px-8 py-3.5 rounded-2xl shadow-lg transition flex items-center gap-2 text-sm">
                    <i class="fa-solid fa-store text-orange-600"></i>
                    <span><?= htmlspecialchars($cta_btn1) ?></span>
                </a>
                <a href="<?= BASE_URL ?>/products.php" class="bg-orange-800/60 hover:bg-orange-800 text-white border border-white/20 font-bold px-6 py-3.5 rounded-2xl transition flex items-center gap-2 text-sm">
                    <i class="fa-solid fa-magnifying-glass"></i>
                    <span><?= htmlspecialchars($cta_btn2) ?></span>
                </a>
            </div>
        </div>
        <!-- Decorative bg icon -->
        <div class="absolute -left-10 -bottom-10 text-white/10 text-[180px] pointer-events-none">
            <i class="fa-solid fa-rocket"></i>
        </div>
    </div>
</section>

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