<?php
/**
 * شريط العروض المتحرك - Announcement Bar
 * يظهر في أعلى جميع الصفحات
 */

// التأكد من وجود اتصال بقاعدة البيانات
if (!isset($conn) || !$conn) {
    return; // لا تعرض الشريط إذا لم يكن هناك اتصال
}

// جلب العرض النشط من جدول announcements
try {
    $stmt = $conn->prepare("
        SELECT id, title, message, background_color, text_color, 
               link_url, link_text, coupon_code
        FROM announcements 
        WHERE is_active = 1 
        AND (start_date IS NULL OR start_date <= NOW())
        AND (end_date IS NULL OR end_date >= NOW())
        ORDER BY display_order DESC, created_at DESC 
        LIMIT 1
    ");
    $stmt->execute();
    $active_offer = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $active_offer = null;
}

// إذا كان هناك عرض نشط، اعرضه
if ($active_offer):
    $banner_text = $active_offer['message'];
    $banner_color = $active_offer['background_color'] ?: 'linear-gradient(135deg, #E57393 0%, #D1537A 100%)';
    $text_color = $active_offer['text_color'] ?: '#ffffff';
    $title = $active_offer['title'];
    $coupon_code = $active_offer['coupon_code'];
    $link_url = $active_offer['link_url'];
    $link_text = $active_offer['link_text'] ?: 'تسوق الآن';
?>
<div class="announcement-bar" id="announcementBar" style="background: <?php echo strpos($banner_color, 'gradient') !== false ? $banner_color : htmlspecialchars($banner_color); ?>; color: <?php echo htmlspecialchars($text_color); ?>;">
    <div class="announcement-content">
        <div class="announcement-text">
            <span class="announcement-icon">🎉</span>
            <span class="announcement-title"><?php echo htmlspecialchars($title); ?></span>
            <span class="announcement-message"><?php echo htmlspecialchars($banner_text); ?></span>
            <?php if ($coupon_code): ?>
            <span class="announcement-code">كود: <strong><?php echo htmlspecialchars($coupon_code); ?></strong></span>
            <?php endif; ?>
            <?php if ($link_url): ?>
            <a href="<?php echo htmlspecialchars($link_url); ?>" class="announcement-link"><?php echo htmlspecialchars($link_text); ?> →</a>
            <?php endif; ?>
        </div>
        <button class="announcement-close" onclick="closeAnnouncementBar()" aria-label="إغلاق">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <line x1="18" y1="6" x2="6" y2="18"></line>
                <line x1="6" y1="6" x2="18" y2="18"></line>
            </svg>
        </button>
    </div>
</div>

<style>
    .announcement-bar {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 9999;
        padding: 12px 20px;
        color: white;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        animation: slideDown 0.5s ease-out, pulse 2s ease-in-out infinite;
    }
    
    @keyframes slideDown {
        from {
            transform: translateY(-100%);
            opacity: 0;
        }
        to {
            transform: translateY(0);
            opacity: 1;
        }
    }
    
    @keyframes pulse {
        0%, 100% {
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        50% {
            box-shadow: 0 4px 20px rgba(0,0,0,0.3);
        }
    }
    
    .announcement-content {
        max-width: 1200px;
        margin: 0 auto;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 20px;
    }
    
    .announcement-text {
        display: flex;
        align-items: center;
        gap: 12px;
        flex-wrap: wrap;
        font-size: 15px;
        font-weight: 500;
    }
    
    .announcement-icon {
        font-size: 20px;
        animation: bounce 1s ease-in-out infinite;
    }
    
    @keyframes bounce {
        0%, 100% {
            transform: translateY(0);
        }
        50% {
            transform: translateY(-5px);
        }
    }
    
    .announcement-title {
        font-weight: 700;
        font-size: 16px;
    }
    
    .announcement-message {
        font-weight: 500;
    }
    
    .announcement-link {
        background: rgba(255,255,255,0.25);
        padding: 8px 20px;
        border-radius: 25px;
        text-decoration: none;
        color: inherit;
        font-weight: 700;
        transition: all 0.3s;
        border: 2px solid rgba(255,255,255,0.5);
        backdrop-filter: blur(10px);
    }
    
    .announcement-link:hover {
        background: rgba(255,255,255,0.4);
        transform: translateX(-3px) scale(1.05);
        box-shadow: 0 4px 12px rgba(255,255,255,0.3);
    }
    
    .announcement-badge {
        background: rgba(255,255,255,0.3);
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 13px;
        font-weight: 700;
        backdrop-filter: blur(10px);
        animation: glow 2s ease-in-out infinite;
    }
    
    @keyframes glow {
        0%, 100% {
            box-shadow: 0 0 5px rgba(255,255,255,0.3);
        }
        50% {
            box-shadow: 0 0 20px rgba(255,255,255,0.6);
        }
    }
    
    .announcement-code {
        background: rgba(255,255,255,0.3);
        padding: 8px 16px;
        border-radius: 8px;
        font-size: 14px;
        backdrop-filter: blur(10px);
        border: 2px solid rgba(255,255,255,0.5);
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
    
    .announcement-code strong {
        font-weight: 900;
        letter-spacing: 2px;
        font-size: 15px;
    }
    
    .announcement-close {
        background: rgba(255,255,255,0.2);
        border: none;
        width: 32px;
        height: 32px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.3s;
        flex-shrink: 0;
    }
    
    .announcement-close:hover {
        background: rgba(255,255,255,0.3);
        transform: rotate(90deg);
    }
    
    /* تعديل padding للصفحة عند وجود الشريط */
    body.has-announcement {
        padding-top: 60px;
    }
    
    /* Responsive */
    @media (max-width: 768px) {
        .announcement-bar {
            padding: 6px 12px;
        }
        
        .announcement-text {
            font-size: 12px;
            gap: 6px;
        }
        
        .announcement-icon {
            font-size: 14px;
        }
        
        .announcement-badge {
            font-size: 10px;
            padding: 2px 8px;
        }
        
        .announcement-code {
            font-size: 11px;
            padding: 3px 8px;
        }
        
        .announcement-close {
            width: 24px;
            height: 24px;
        }
        
        .announcement-close svg {
            width: 12px;
            height: 12px;
        }
        
        body.has-announcement {
            padding-top: 38px;
        }
    }
    
    @media (max-width: 480px) {
        .announcement-text {
            flex-direction: column;
            align-items: flex-start;
            gap: 6px;
        }
        
        .announcement-content {
            gap: 10px;
        }
    }
</style>

<script>
    // إضافة class للـ body
    document.body.classList.add('has-announcement');
    
    // دالة إغلاق الشريط
    function closeAnnouncementBar() {
        const bar = document.getElementById('announcementBar');
        bar.style.animation = 'slideUp 0.3s ease-out';
        
        setTimeout(() => {
            bar.style.display = 'none';
            document.body.classList.remove('has-announcement');
            
            // حفظ في localStorage لعدم إظهاره مرة أخرى في هذه الجلسة
            localStorage.setItem('announcement_closed_<?php echo $active_offer['id']; ?>', 'true');
        }, 300);
    }
    
    // التحقق من localStorage
    if (localStorage.getItem('announcement_closed_<?php echo $active_offer['id']; ?>') === 'true') {
        document.getElementById('announcementBar').style.display = 'none';
        document.body.classList.remove('has-announcement');
    }
    
    // إضافة animation للإغلاق
    const style = document.createElement('style');
    style.textContent = `
        @keyframes slideUp {
            from {
                transform: translateY(0);
                opacity: 1;
            }
            to {
                transform: translateY(-100%);
                opacity: 0;
            }
        }
    `;
    document.head.appendChild(style);
</script>
<?php endif; ?>
