/* Main JS for Veluno Industries (B2B Lead Portal) */

document.addEventListener('DOMContentLoaded', function () {
    // 1. Sticky Navigation Scroll Effect
    const navbar = document.querySelector('.navbar');
    if (navbar) {
        window.addEventListener('scroll', function () {
            if (window.scrollY > 50) {
                navbar.classList.add('scrolled');
            } else {
                navbar.classList.remove('scrolled');
            }
        });
    }

    // 2. Auto-fill Product Name in Enquiry Modal
    // If a user clicks an enquiry button for a specific product, we prefill the product input
    const enquiryModal = document.getElementById('enquiryModal');
    if (enquiryModal) {
        enquiryModal.addEventListener('show.bs.modal', function (event) {
            // Button that triggered the modal
            const button = event.relatedTarget;
            // Extract info from data-bs-* attributes
            const productName = button.getAttribute('data-bs-product') || '';
            const productSeries = button.getAttribute('data-bs-series') || '';

            // Update the modal's content
            const modalProductInput = enquiryModal.querySelector('#modalProductName');
            const modalTitle = enquiryModal.querySelector('#enquiryModalLabel');
            
            if (modalProductInput) {
                modalProductInput.value = productName ? `${productName} (${productSeries})` : '';
            }
            if (modalTitle && productName) {
                modalTitle.textContent = `Enquire About ${productName}`;
            } else if (modalTitle) {
                modalTitle.textContent = 'Product Inquiry';
            }
        });
    }

    // 3. Dynamic WhatsApp Query Parameter Builder
    // Optional helper to append dynamic user info to WhatsApp click triggers if needed
    const waTriggerButtons = document.querySelectorAll('.wa-dynamic-link');
    waTriggerButtons.forEach(button => {
        button.addEventListener('click', function(e) {
            const productName = this.getAttribute('data-product-name') || '';
            const baseText = "Hello Veluno, I am interested in your " + productName + ". Please send details & price list.";
            const encodedText = encodeURIComponent(baseText);
            this.href = "https://wa.me/917003549432?text=" + encodedText;
        });
    });

    // 4. Form Validation Helper
    const forms = document.querySelectorAll('.needs-validation');
    Array.from(forms).forEach(form => {
        form.addEventListener('submit', event => {
            if (!form.checkValidity()) {
                event.preventDefault();
                event.stopPropagation();
            }
            form.classList.add('was-validated');
        }, false);
    });

    // 5. Initialize Hero Carousel with Auto-sliding
    const heroCarouselEl = document.getElementById('heroCarousel');
    if (heroCarouselEl) {
        new bootstrap.Carousel(heroCarouselEl, {
            interval: 3000,
            ride: 'carousel',
            pause: 'hover'
        });
    }
});

