document.addEventListener("DOMContentLoaded", () => {
    const themePath = './styles/artery_2026/theme';
    const defaultAvatar = './styles/artery_2026/theme/images/no_avatar.png';

    document.querySelectorAll(".topic-images-placeholder").forEach(e => {
        let t = e.dataset.topicId;
        if (t) {
            const n = new XMLHttpRequest();
            n.open("GET", themePath + "/forum_topic_images.php?t=" + t, true);
            n.onload = () => {
                if (n.status === 200 && n.responseText.trim()) {
                    e.innerHTML = n.responseText;
                    e.style.display = "block";
                } else {
                    e.style.display = "none";
                }
            };
            n.send();
        }
    });

    document.addEventListener("click", e => {
        const t = e.target.closest(".topic-thumb");
        if (t) {
            e.preventDefault();
            e.stopPropagation();
            const n = t.getAttribute("data-full");
            if (n) {
                const o = document.createElement("div");
                o.className = "img-lightbox";
                o.innerHTML = '<span class="close-btn"><i class="fa ti-close"></i></span><div class="img-lightbox-inner"><img src="' + n + '" alt="大图预览"></div>';
                document.body.appendChild(o);
                setTimeout(() => o.classList.add("show"), 10);
                o.addEventListener("click", () => {
                    o.classList.remove("show");
                    setTimeout(() => o.remove(), 300);
                });
            }
        }
    }, true);

    document.querySelectorAll('.user_vpic').forEach(div => {
        let username = div.getAttribute('data-poster-name');
        let userId = div.getAttribute('data-user-id');

        if (!username) {
            console.warn('没有用户名:', div);
            return;
        }

        const url = themePath + '/get_avatar.php?username=' + encodeURIComponent(username);
        
        const createAvatarWithLink = (src, userId, username) => {
            // 创建链接
            const link = document.createElement('a');
            link.href = './memberlist.php?mode=viewprofile&u=' + (userId || 0);
            link.title = username;
            link.style.display = 'inline-block';
            link.style.textDecoration = 'none';
            
            // 创建图片
            const img = document.createElement('img');
            img.src = src;
            img.alt = username;
            img.style.cssText = 'width:32px;height:32px;border-radius:50%;vertical-align:middle';
            
            link.appendChild(img);
            return link;
        };

        fetch(url)
            .then(r => r.json())
            .then(data => {
                let src = defaultAvatar;
                let finalUserId = userId || data.user_id || 0;
                let finalUsername = data.username || username;
                
                if (data.success && data.avatar_url) {
                    src = data.avatar_url;
                }
                
                div.innerHTML = '';
                div.appendChild(createAvatarWithLink(src, finalUserId, finalUsername));
            })
            .catch(err => {
                console.error('头像加载失败:', err);
                div.innerHTML = '';
                div.appendChild(createAvatarWithLink(defaultAvatar, userId || 0, username));
            });
    });
});