document.addEventListener('DOMContentLoaded', function () {
    // 支持首页 .thumb-img + 帖子内嵌 .postimage（覆盖正文内嵌和附件列表预览图）
    var images = document.querySelectorAll('.thumb-img, .postimage');
    
    for (var i = 0; i < images.length; i++) {
        var img = images[i];
        
        img.addEventListener('click', function (e) {
            e.preventDefault();        // 阻止默认在本窗口打开缩略图
            e.stopPropagation();

            // 优先用 data-full（原图），如果没有则从 src 自动修正
            var fullSrc = this.getAttribute('data-full');
            
            if (!fullSrc || fullSrc.trim() === '') {
                var src = this.src || '';
                fullSrc = src;
                
                // 自动去掉缩略图参数，得到原图
                if (src) {
                    fullSrc = src
                        .replace(/&?t=1/g, '')                     // 去掉 t=1 参数（常见缩略图）
                        .replace(/&?mode=thumbnail/g, '')          // 去掉 mode=thumbnail
                        .replace(/&?thumb=[^&]*/g, '')             // 去掉 thumb=xx
                        .replace(/[?&]$/, '');                     // 清理尾部 ? 或 &
                }
            }

            // 调试：打印实际加载的原图地址（F12 Console 查看）
            console.log('弹窗加载的原图地址:', fullSrc);

            if (!fullSrc) return;

            var overlay = document.createElement('div');
            overlay.style.position = 'fixed';
            overlay.style.top = '0';
            overlay.style.left = '0';
            overlay.style.width = '100%';
            overlay.style.height = '100%';
            overlay.style.background = 'rgba(0, 0, 0, 0.7)';
            overlay.style.zIndex = '9999';
            overlay.style.display = 'flex';
            overlay.style.alignItems = 'center';
            overlay.style.justifyContent = 'center';
            overlay.style.opacity = '0';
            overlay.style.transition = 'opacity 0.3s ease';
            overlay.style.cursor = 'zoom-out';

            var bigImg = document.createElement('img');
            bigImg.src = fullSrc;
            bigImg.style.maxWidth = '90%';
            bigImg.style.maxHeight = '90%';
            bigImg.style.borderRadius = '10px';
            bigImg.style.objectFit = 'contain';
            bigImg.style.pointerEvents = 'auto';
            bigImg.style.cursor = 'zoom-out';

            var closeBtn = document.createElement('i');
            closeBtn.className = 'icon ti-close';
            closeBtn.style.position = 'absolute';
            closeBtn.style.top = '20px';
            closeBtn.style.right = '20px';
            closeBtn.style.fontSize = '3em';
            closeBtn.style.color = '#fff';
            closeBtn.style.cursor = 'pointer';
            closeBtn.style.transition = 'transform 0.3s ease';
            closeBtn.style.zIndex = '10000';

            closeBtn.addEventListener('mouseenter', function () {
                closeBtn.style.transform = 'rotate(90deg)';
            });
            closeBtn.addEventListener('mouseleave', function () {
                closeBtn.style.transform = 'rotate(0deg)';
            });

            var closeModal = function () {
                overlay.style.opacity = '0';
                setTimeout(function () {
                    overlay.remove();
                }, 300);
            };

            overlay.addEventListener('click', function (ev) {
                if (ev.target === overlay || ev.target === bigImg) closeModal();
            });
            closeBtn.addEventListener('click', closeModal);

            overlay.appendChild(bigImg);
            overlay.appendChild(closeBtn);
            document.body.appendChild(overlay);

            setTimeout(function () {
                overlay.style.opacity = '1';
            }, 10);
        });
    }
});