<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>记忆</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="/css/index.css">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600&display=swap" rel="stylesheet">
</head>
<body>
<div class="container">
    <h1 class="main-title">我们的相册</h1>

    <!-- 相册详情模块 -->
    <div class="album-detail" id="albumDetail">
        <button class="back-button" id="backButton">
            <i class="fas fa-arrow-left"></i> 返回相册列表
        </button>

        <div class="detail-header">
            <h2 class="detail-title" id="detailTitle">相册标题</h2>
        </div>

        <div class="photo-grid" id="photoGrid">
            <!-- 照片将通过JS动态生成 -->
        </div>
    </div>
</div>

<!-- 图片预览模态框 -->
<div class="preview-modal" id="previewModal">
    <button class="close-modal" id="closeModal">
        <i class="fas fa-times"></i>
    </button>

    <button class="nav-btn prev-btn" id="prevBtn">
        <i class="fas fa-chevron-left"></i>
    </button>

    <div class="modal-content">
        <img class="modal-img" id="modalImg" src="" alt="预览图片">

        <div class="modal-tools">
            <button class="tool-btn" id="rotateLeft" title="向左旋转">
                <i class="fas fa-undo"></i>
            </button>
            <button class="tool-btn" id="rotateRight" title="向右旋转">
                <i class="fas fa-redo"></i>
            </button>
            <button class="tool-btn" id="downloadBtn" title="下载图片">
                <i class="fas fa-download"></i>
            </button>
        </div>
    </div>

    <button class="nav-btn next-btn" id="nextBtn">
        <i class="fas fa-chevron-right"></i>
    </button>
</div>

<script>
    // DOM元素
    const albumSelection = document.getElementById('albumSelection');
    const albumDetail = document.getElementById('albumDetail');
    const detailTitle = document.getElementById('detailTitle');
    const photoGrid = document.getElementById('photoGrid');
    const backButton = document.getElementById('backButton');
    const previewModal = document.getElementById('previewModal');
    const modalImg = document.getElementById('modalImg');
    const closeModal = document.getElementById('closeModal');
    const rotateLeft = document.getElementById('rotateLeft');
    const rotateRight = document.getElementById('rotateRight');
    const downloadBtn = document.getElementById('downloadBtn');
    const prevBtn = document.getElementById('prevBtn');
    const nextBtn = document.getElementById('nextBtn');

    // 当前状态
    let currentAlbum = null;
    let currentPhotoIndex = 0;
    let currentPhotos = [];
    let currentRotation = 0;

    // 显示相册详情
    // 显示相册详情
    function showAlbumDetail(album) {
        currentAlbum = album;
        currentPhotos = album.photos;
        detailTitle.textContent = album.title;
        photoGrid.innerHTML = '';

        album.photos.forEach((photo, index) => {
            const photoItem = document.createElement('div');
            photoItem.className = 'photo-item';

            // 创建加载指示器
            const spinner = document.createElement('div');
            spinner.className = 'loading-spinner';

            // 创建图片元素
            const img = new Image();
            img.className = 'photo-img';
            img.alt = `照片 ${index + 1}`;
            img.dataset.index = index;

            // 设置data-src属性用于懒加载
            img.dataset.src = photo.compressed;

            // 添加点击事件
            photoItem.addEventListener('click', () => {
                currentPhotoIndex = index;
                showPhotoPreview(photo.compressed);
            });

            // 观察图片是否进入视口
            const observer = new IntersectionObserver((entries) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        const lazyImage = entry.target;
                        const src = lazyImage.dataset.src;

                        // 移除data-src属性确保只加载一次
                        lazyImage.removeAttribute('data-src');

                        // 设置src属性开始加载
                        lazyImage.src = src;

                        // 图片加载完成后显示
                        lazyImage.onload = () => {
                            lazyImage.classList.add('loaded');
                            if (spinner.parentNode === photoItem) {
                                photoItem.removeChild(spinner);
                            }
                        };

                        // 停止观察
                        observer.unobserve(lazyImage);
                    }
                });
            }, {
                rootMargin: '200px'
            });

            photoItem.appendChild(spinner);
            photoItem.appendChild(img);
            photoGrid.appendChild(photoItem);

            // 开始观察图片
            observer.observe(img);
        });

        // 滚动到顶部
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    }

    // 返回相册列表
    function backToAlbums() {
        window.location.href = '/';
    }

    // 显示图片预览
    function showPhotoPreview(photo) {
        modalImg.src = photo;
        currentRotation = 0;
        modalImg.style.transform = 'rotate(0deg)';

        previewModal.classList.add('active');
        setTimeout(() => {
            previewModal.style.opacity = '1';
            setTimeout(() => {
                modalImg.classList.add('active');
            }, 50);
        }, 10);
    }

    // 关闭图片预览
    function closePhotoPreview() {
        modalImg.classList.remove('active');
        previewModal.style.opacity = '0';

        setTimeout(() => {
            previewModal.classList.remove('active');
        }, 300);
    }

    // 显示上一张图片
    function showPrevPhoto() {
        if (currentPhotoIndex > 0) {
            currentPhotoIndex--;
            modalImg.classList.remove('active');
            setTimeout(() => {
                // modalImg.src = currentPhotos[currentPhotoIndex].original;
                modalImg.src = currentPhotos[currentPhotoIndex].compressed;
                modalImg.style.transform = 'rotate(0deg)';
                currentRotation = 0;
                setTimeout(() => {
                    modalImg.classList.add('active');
                }, 50);
            }, 300);
        }
    }

    // 显示下一张图片
    function showNextPhoto() {
        if (currentPhotoIndex < currentPhotos.length - 1) {
            currentPhotoIndex++;
            modalImg.classList.remove('active');
            setTimeout(() => {
                // modalImg.src = currentPhotos[currentPhotoIndex].original;
                modalImg.src = currentPhotos[currentPhotoIndex].compressed;
                modalImg.style.transform = 'rotate(0deg)';
                currentRotation = 0;
                setTimeout(() => {
                    modalImg.classList.add('active');
                }, 50);
            }, 300);
        }
    }

    // 旋转图片
    function rotatePhoto(direction) {
        if (direction === 'left') {
            currentRotation -= 90;
        } else {
            currentRotation += 90;
        }

        modalImg.style.transform = `rotate(${currentRotation}deg)`;
    }

    // 下载图片
    function downloadPhoto() {
        if (!currentAlbum || currentPhotoIndex === null) return;

        const link = document.createElement('a');
        link.href = currentPhotos[currentPhotoIndex].original;
        link.download = `photo_${currentAlbum.title}_${Date.now()}.jpg`;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }

    // 事件监听
    backButton.addEventListener('click', backToAlbums);
    closeModal.addEventListener('click', closePhotoPreview);
    rotateLeft.addEventListener('click', () => rotatePhoto('left'));
    rotateRight.addEventListener('click', () => rotatePhoto('right'));
    downloadBtn.addEventListener('click', downloadPhoto);
    prevBtn.addEventListener('click', showPrevPhoto);
    nextBtn.addEventListener('click', showNextPhoto);

    // 键盘导航
    document.addEventListener('keydown', (e) => {
        if (previewModal.classList.contains('active')) {
            if (e.key === 'ArrowLeft') {
                showPrevPhoto();
            } else if (e.key === 'ArrowRight') {
                showNextPhoto();
            } else if (e.key === 'Escape') {
                closePhotoPreview();
            }
        }
    });

    // 初始化雪花效果
    function createSnowflakes() {
        const width = window.innerWidth;
        const height = window.innerHeight;
        const count = Math.min(50, Math.floor(width * height / 20000));

        for (let i = 0; i < count; i++) {
            createSnowflake();
        }
    }

    function createSnowflake() {
        const snowflake = document.createElement('div');
        snowflake.className = 'snowflake';

        const shapes = ['❄', '❅', '❆', '✻', '✼', '✾', '✵'];
        snowflake.textContent = shapes[Math.floor(Math.random() * shapes.length)];

        const size = Math.random() * 0.8 + 0.2;
        snowflake.style.fontSize = `${size}em`;
        snowflake.style.opacity = Math.random() * 0.6 + 0.3;
        snowflake.style.left = `${Math.random() * window.innerWidth}px`;
        snowflake.style.top = '-20px';

        const duration = Math.random() * 5 + 5;
        snowflake.style.animationDuration = `${duration}s`;
        snowflake.style.animationDelay = `${Math.random() * 5}s`;

        document.body.appendChild(snowflake);

        snowflake.addEventListener('animationiteration', () => {
            snowflake.style.left = `${Math.random() * window.innerWidth}px`;
            snowflake.style.top = '-20px';
        });
    }

    // 窗口大小改变时重新创建雪花
    window.addEventListener('resize', () => {
        const snowflakes = document.querySelectorAll('.snowflake');
        snowflakes.forEach(snowflake => snowflake.remove());
        createSnowflakes();
    });

    // 初始化应用
    window.addEventListener('load', () => {
        showAlbumDetail(<?php echo json_encode($photo_album); ?>)
        createSnowflakes();
    });
</script>
</body>
</html>
