// ✅ Hero Banner Slider — GetBannerList.php থেকে Dynamic
(function() {
    const container = document.getElementById('bannerContainer');
    const dotsWrap = document.getElementById('bannerDots');
    if (!container || !dotsWrap) return;

    let slides = [];
    let current = 0;
    let timer = null;

    fetch('/deepanshu/GetBannerList.php')
        .then(res => res.json())
        .then(json => {
            const data = (json && json.data) ? json.data : [];
            if (!data.length) {
                container.innerHTML = '<div class="absolute inset-0 flex items-center justify-center text-gray-500 text-xs">কোনো ব্যানার নেই</div>';
                return;
            }
            slides = data;
            renderSlides();
            renderDots();
            showSlide(0);
            if (slides.length > 1) {
                timer = setInterval(() => showSlide((current + 1) % slides.length), 3500);
            }
        })
        .catch(() => {
            container.innerHTML = '<div class="absolute inset-0 flex items-center justify-center text-gray-500 text-xs">Banner লোড করা যায়নি</div>';
        });

    function renderSlides() {
        container.innerHTML = slides.map((b, i) => {
            const img = `<img src="${b.bannerUrl}" alt="banner" class="absolute inset-0 w-full h-full object-cover transition-opacity duration-500" data-slide="${i}" style="opacity:0;">`;
            return b.url
                ? `<a href="${b.url}">${img}</a>`
                : img;
        }).join('');
    }

    function renderDots() {
        dotsWrap.innerHTML = slides.map((_, i) =>
            `<span class="w-1.5 h-1.5 rounded-full ${i === 0 ? 'bg-white' : 'bg-gray-400'}" data-dot="${i}"></span>`
        ).join('');
    }

    function showSlide(index) {
        current = index;
        container.querySelectorAll('img[data-slide]').forEach(img => {
            img.style.opacity = (parseInt(img.dataset.slide) === index) ? '1' : '0';
        });
        dotsWrap.querySelectorAll('span[data-dot]').forEach(dot => {
            dot.className = 'w-1.5 h-1.5 rounded-full ' + (parseInt(dot.dataset.dot) === index ? 'bg-white' : 'bg-gray-400');
        });
    }
})();
