document.addEventListener("DOMContentLoaded", () => {
    const sUrl = "./styles/artery_2026/theme/homepage_images.php?ajax=1&section=slider";
    const tUrl = "./styles/artery_2026/theme/homepage_images.php?ajax=1&section=thumbs";
    const bar = document.getElementById("latest-images-sidebar");
    const status = document.getElementById("sidebar-status");
    const sc = bar.querySelector(".slides");
    const dc = bar.querySelector(".dots");
    
    let timer = null, cur = 0, slides = [], dots = [];
    
    const show = i => {
        if (!slides.length) return;
        cur = (i + slides.length) % slides.length;
        slides.forEach(x => x.classList.remove("active"));
        dots.forEach(x => x.classList.remove("active"));
        slides[cur].classList.add("active");
        dots[cur].classList.add("active");
    };
    
    const start = () => {
        clearInterval(timer);
        timer = setInterval(() => show(cur + 1), 5000);
    };
    
    fetch(sUrl)
        .then(r => r.text())
        .then(h => {
            if (!h.trim()) {
                sc.innerHTML = '<div style="padding:40px 0;color:#777;text-align:center;">暂无图片</div>';
                status.style.display = "none";
                return;
            }
            let tmp = document.createElement("div");
            tmp.innerHTML = h;
            sc.innerHTML = "";
            dc.innerHTML = "";
            slides = [];
            dots = [];
            [...tmp.children].forEach((it, i) => {
                let d = document.createElement("div");
                d.className = "mySlides";
                d.appendChild(it.cloneNode(true));
                sc.appendChild(d);
                slides.push(d);
                
                let dot = document.createElement("span");
                dot.className = "dot";
                dot.onclick = () => {
                    show(i);
                    start();
                };
                dc.appendChild(dot);
                dots.push(dot);
            });
            if (slides.length) {
                show(0);
                start();
            }
            status.style.display = "none";
        })
        .catch(() => {
            status.innerHTML = '加载失败，<a href="javascript:location.reload()" style="color:#ff8800">点击刷新</a>';
        });
    
    bar.addEventListener("mouseenter", () => clearInterval(timer));
    bar.addEventListener("mouseleave", () => slides.length && start());
    
    // 获取最新主题标题
    fetch("./feed.php?mode=topics")
        .then(r => r.text())
        .then(x => {
            let d = new DOMParser().parseFromString(x, "text/xml");
            let e = d.getElementsByTagName("entry");
            if (!e.length) e = d.getElementsByTagName("item");
            
            let b = document.getElementById("latest-topics-box");
            b.innerHTML = "";
            let visibleCount = 0;
            
            for (let i = 0; i < e.length && visibleCount < 10; i++) {  // 改为你想调用的条目数量
                let t = e[i];
                let n = t.getElementsByTagName("title")[0];
                let l = t.getElementsByTagName("link")[0];
                let c = n ? n.textContent.trim() : "无标题";
                
                if (c.includes("无权限") || c.includes("需要登录")) {
                    continue;
                }
                
                let s = c.indexOf(" • ");
                if (s !== -1) c = c.slice(s + 3);
                
                let h = l ? (l.getAttribute("href") || l.textContent) : "#";
                let li = document.createElement("li");
                li.innerHTML = '<a href="' + h + '"><i class="fa fa-genderless"></i> ' + c + "</a>";
                b.appendChild(li);
                visibleCount++;
            }
            
            if (visibleCount === 0) {
                b.innerHTML = "<li>暂无内容</li>";
            }
        })
        .catch(() => {
            document.getElementById("latest-topics-box").innerHTML = "<li>无法加载</li>";
        });
    
    // 获取缩略图网格
    fetch(tUrl)
        .then(r => r.text())
        .then(h => {
            let g = document.getElementById("latest-thumbs-grid");
            if (g) {
                g.innerHTML = h || '<div style="grid-column:1/-1;text-align:center;padding:40px;color:#999;">暂无图片</div>';
            }
        })
        .catch(() => {
            let g = document.getElementById("latest-thumbs-grid");
            if (g) {
                g.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:40px;color:#c66;">加载失败</div>';
            }
        });
});