<?php
// ডাটাবেস কানেকশন নিশ্চিত করা (যদি প্যারেন্ট ফাইলে না থাকে)
if (!isset($conn)) {
    $db_paths = ['includes/db.php', '../includes/db.php'];
    foreach ($db_paths as $path) {
        if (file_exists($path)) { require $path; break; }
    }
}

// সেটিংস ফেচ করা (শুধুমাত্র পপআপের ডাটা)
$pop_query = $conn->query("SELECT popup_enabled, popup_title, popup_desc, popup_image FROM settings WHERE id=1");
$popup = $pop_query->fetch_assoc();

// লজিক: যদি পপআপ এনাবল থাকে এবং ডাটা পাওয়া যায় তবেই দেখাবে
if ($popup && $popup['popup_enabled'] == 1):

    // ইমেজের পাথ ঠিক করা (যদি সাব-ফোল্ডারে থাকে তবে ../ যোগ হবে)
    $img_path = $popup['popup_image'];
    if (!file_exists($img_path) && file_exists('../' . $img_path)) {
        $img_path = '../' . $img_path;
    }
?>

<div id="promoModal" class="fixed inset-0 z-[9999] hidden flex items-center justify-center bg-black/80 backdrop-blur-sm p-4">
    
    <div class="relative w-full max-w-sm bg-[#1e293b] rounded-lg shadow-2xl border border-gray-700 overflow-hidden transform transition-all scale-100">
        
        <div class="flex justify-between items-center bg-[#0f172a] px-4 py-3 border-b border-gray-700">
            <h3 class="text-white font-bold text-sm"><?php echo htmlspecialchars($popup['popup_title']); ?></h3>
            <button onclick="closePromoModal()" class="text-gray-400 hover:text-white transition">
                <i class="fas fa-times text-lg"></i>
            </button>
        </div>

        <div class="p-5 text-center">
            <p class="text-white text-xs font-bold mb-4">
                <?php echo htmlspecialchars($popup['popup_desc']); ?>
            </p>
            
            <div class="rounded-lg overflow-hidden border border-yellow-600/50 shadow-lg">
                <img src="<?php echo $img_path; ?>" 
                     onerror="this.src='https://placehold.co/400x300/1e293b/FACC15?text=OFFER+IMAGE'" 
                     alt="Offer" 
                     class="w-full h-auto object-cover">
            </div>
        </div>

        <div class="h-2 w-full bg-gradient-to-r from-yellow-600 via-yellow-400 to-yellow-600"></div>
    </div>
</div>

<script>
    // Show Modal Function
    function showPromoModal() {
        const modal = document.getElementById('promoModal');
        if(modal) {
            modal.classList.remove('hidden');
            const content = modal.querySelector('div');
            content.classList.remove('scale-95', 'opacity-0');
            content.classList.add('scale-100', 'opacity-100');
        }
    }

    // Close Modal Function
    function closePromoModal() {
        const modal = document.getElementById('promoModal');
        if(modal) modal.classList.add('hidden');
    }

    // Trigger on Page Load
    document.addEventListener("DOMContentLoaded", function() {
        setTimeout(showPromoModal, 1000); 
    });
</script>

<?php endif; // End check popup_enabled ?>