document.addEventListener('DOMContentLoaded', function() {
    if (!localStorage.getItem('popupShown')) {
        setTimeout(function() {
            document.getElementById('email-popup').style.display = 'flex';
        }, 2000); // Show popup after 2 seconds
    }

    // Dodaj poslušalca za promocijsko kodo v popupu
    const popupPromoInput = document.getElementById('popup-promo-code');
    const popupApplyButton = document.getElementById('popup-apply-promo');
    const popupPromoMessage = document.getElementById('popup-promo-message');
    let promoCodeApplied = false;
    
    if (popupPromoInput && popupApplyButton) {
        popupApplyButton.addEventListener('click', function(e) {
            e.preventDefault();
            const promoCode = popupPromoInput.value.trim();
            
            if (promoCode === 'HOLIDAYS25' && !promoCodeApplied) {
                // Pridobi trenutni znesek
                const totalElement = document.getElementById('popupTotalPrice');
                if (totalElement) {
                    const currentAmount = parseFloat(totalElement.textContent);
                    const newAmount = currentAmount - 100;
                    
                    // Posodobi prikaz
                    totalElement.textContent = newAmount.toFixed(2);
                    
                    // Posodobi tudi zneske za delno plačilo
                    const fullPaymentAmount = document.getElementById('full-payment-amount');
                    const depositAmount = document.getElementById('deposit-amount');
                    if (fullPaymentAmount) {
                        fullPaymentAmount.textContent = newAmount.toFixed(2);
                    }
                    if (depositAmount) {
                        depositAmount.textContent = (newAmount * 0.3).toFixed(2);
                    }
                    
                    // Dodaj sporočilo o uspehu
                    popupPromoMessage.innerHTML = '<p class="promo-success">Promotional code successfully applied! We deducted €100.</p>';
                    
                    // Onemogoči nadaljnje vnose
                    popupPromoInput.disabled = true;
                    popupApplyButton.disabled = true;
                    promoCodeApplied = true;
                }
            } else {
                // Prikaži napako
                popupPromoMessage.innerHTML = '<p class="promo-error">Neveljavna promocijska koda.</p>';
                
                // Odstrani sporočilo o napaki po 3 sekundah
                setTimeout(() => {
                    popupPromoMessage.innerHTML = '';
                }, 3000);
            }
        });
    }

    // Pridobi podatke o vodiču
    const guideImage = document.getElementById('guide-image-data')?.getAttribute('data-guide-image');
    const guideName = document.getElementById('guide-image-data')?.getAttribute('data-guide-name');
    const guideTitle = document.getElementById('guide-image-data')?.getAttribute('data-guide-title');

    // Posodobi HTML v obrazcu
    if (guideImage && guideName) {
        const guideContainer = document.querySelector('.guide-info-container');
        if (guideContainer) {
            guideContainer.innerHTML = `
                <div class="guide-info">
                    <div class="guide-left">
                        <div class="guide-image-container">
                            <img src="${guideImage}" alt="${guideName}" class="guide-image">
                            <h4 class="guide-name">${guideName}</h4>
                        </div>
                    </div>
                    <div class="guide-contact">
                        <a href="mailto:info@europewonder.com" class="contact-item">
                            <i class="fas fa-envelope"></i>
                            <span>info@europewonder.com</span>
                        </a>
                        <a href="tel:+38671548893" class="contact-item">
                            <i class="fas fa-phone"></i>
                            <span>+386 (0) 71 548 893</span>
                        </a>
                        <a href="https://wa.me/38671548893" class="contact-item" target="_blank">
                            <i class="fab fa-whatsapp"></i>
                            <span>WhatsApp chat</span>
                        </a>
                    </div>
                </div>
            `;
        }
    }
});

function closePopup() {
    document.getElementById('email-popup').style.display = 'none';
    localStorage.setItem('popupShown', 'true');
}

async function submitEmail(event) {
    event.preventDefault();
    const email = document.getElementById('popup-email').value;
    
    try {
        const response = await fetch('/wp-admin/admin-ajax.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: `action=save_popup_email&email=${encodeURIComponent(email)}`
        });

        if (response.ok) {
            document.getElementById('success-message').style.display = 'block';
            document.getElementById('email-form').style.display = 'none';
        }
    } catch (error) {
        console.error('Error:', error);
    }
} 