<?php
// gallery.php
$imageDir = 'images/';
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];

function getImagesFromDirectory($dir, $extensions) {
    $images = [];
    if (!is_dir($dir)) return $images;
    
    $files = scandir($dir);
    foreach ($files as $file) {
        if ($file === '.' || $file === '..') continue;
        
        $filePath = $dir . $file;
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        
        if (in_array($extension, $extensions)) {
            $fileSize = filesize($filePath);
            $images[] = [
                'src' => $filePath,
                'name' => pathinfo($file, PATHINFO_FILENAME),
                'size' => formatFileSize($fileSize)
            ];
        }
    }
    return $images;
}

function formatFileSize($bytes) {
    if ($bytes === 0) return '0 Bytes';
    $k = 1024;
    $sizes = ['Bytes', 'KB', 'MB', 'GB'];
    $i = floor(log($bytes) / log($k));
    return round($bytes / pow($k, $i), 2) . ' ' . $sizes[$i];
}

$images = getImagesFromDirectory($imageDir, $allowedExtensions);
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Konvinka - Фанфы</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #CCCCCC 0%, #555555 100%);
	    min-height: 100vh;
            padding: 20px;
        }

        .gallery {
            max-width: 1400px;
            margin: 0 auto;
        }

        .gallery-title {
            text-align: center;
            margin-bottom: 30px;
            color: white;
            font-size: 2.5em;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .images-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 25px;
            padding: 20px 0;
        }

        .image-card {
            background: white;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .image-card:hover {
            transform: translateY(-10px) scale(1.02);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
        }

        .image-card img {
            width: 100%;
            height: 220px;
            object-fit: cover;
            display: block;
            transition: transform 0.3s ease;
        }

        .image-card:hover img {
            transform: scale(1.1);
        }

        .image-info {
            padding: 20px;
/*
            background: linear-gradient(45deg, #f8f9fa, #e9ecef);
*/
            background: linear-gradient(135deg, #CCCCCC 0%, #555555 100%);
        }

        .image-name {
            font-weight: bold;
            margin-bottom: 8px;
            color: #495057;
            font-size: 1.1em;
        }

        .image-size {
            font-size: 0.9em;
            color: #6c757d;
        }

        .image-count {
            text-align: center;
            color: white;
            margin-bottom: 20px;
            font-size: 1.2em;
        }

        /* Модальное окно */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
/*
            background-color: rgba(0, 0, 0, 0.95);
*/
            background: linear-gradient(135deg, #CCCCCC 0%, #555555 100%);
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            max-width: 95%;
            max-height: 95%;
            border-radius: 10px;
            box-shadow: 0 0 50px rgba(255,255,255,0.1);
        }

        .close {
            position: absolute;
            top: 20px;
            right: 35px;
            color: white;
            font-size: 50px;
            font-weight: bold;
            cursor: pointer;
            transition: color 0.3s ease;
        }

        .close:hover {
            color: #ff6b6b;
        }

        .modal-info {
            color: white;
            text-align: center;
            margin-top: 20px;
            font-size: 1.2em;
        }

        .nav-buttons {
            position: absolute;
            width: 100%;
            display: flex;
            justify-content: space-between;
            padding: 0 30px;
            top: 50%;
            transform: translateY(-50%);
        }

        .nav-button {
            background: rgba(255,255,255,0.2);
            border: none;
            color: white;
            font-size: 30px;
            padding: 15px 20px;
            border-radius: 50%;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        .nav-button:hover {
            background: rgba(255,255,255,0.3);
        }

        @media (max-width: 768px) {
            .images-grid {
                grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
                gap: 15px;
            }
            
            .gallery-title {
                font-size: 2em;
            }
        }
    </style>
</head>
<body>
    <div class="gallery">
        <h1 class="gallery-title">ФАНФИКИ</h1>
<!--        <div class="image-count">Найдено изображений: <?php echo count($images); ?></div> -->
        <div class="images-grid" id="imagesGrid">
            <?php foreach ($images as $index => $image): ?>
                <div class="image-card" onclick="openModal(<?php echo $index; ?>)">
                    <img src="<?php echo $image['src']; ?>" 
                         alt="<?php echo htmlspecialchars($image['name']); ?>" 
                         loading="lazy">
                    <div class="image-info">
<!--
                        <div class="image-name"><?php echo htmlspecialchars($image['name']); ?></div>
                        <div class="image-size"><?php echo $image['size']; ?></div>
-->
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- Модальное окно -->
    <div class="modal" id="imageModal">
        <span class="close" id="closeModal">&times;</span>
        <div class="nav-buttons">
            <button class="nav-button" id="prevButton">&lt;</button>
            <button class="nav-button" id="nextButton">&gt;</button>
        </div>
        <img class="modal-content" id="modalImage">
        <div class="modal-info" id="modalInfo"></div>
    </div>

    <script>
        const images = <?php echo json_encode($images); ?>;
        let currentImageIndex = 0;

        const imageModal = document.getElementById('imageModal');
        const modalImage = document.getElementById('modalImage');
        const modalInfo = document.getElementById('modalInfo');
        const closeModal = document.getElementById('closeModal');
        const prevButton = document.getElementById('prevButton');
        const nextButton = document.getElementById('nextButton');

        function openModal(index) {
            currentImageIndex = index;
            showImage();
            imageModal.style.display = 'flex';
        }

        function showImage() {
            const image = images[currentImageIndex];
            modalImage.src = image.src;
//	    modalInfo.textContent = `${image.name} (${image.size})`; 
        }

        function closeModalFunc() {
            imageModal.style.display = 'none';
        }

        function showNextImage() {
            currentImageIndex = (currentImageIndex + 1) % images.length;
            showImage();
        }

        function showPrevImage() {
            currentImageIndex = (currentImageIndex - 1 + images.length) % images.length;
            showImage();
        }

        // Обработчики событий
        closeModal.addEventListener('click', closeModalFunc);
        prevButton.addEventListener('click', showPrevImage);
        nextButton.addEventListener('click', showNextImage);

        imageModal.addEventListener('click', (e) => {
            if (e.target === imageModal) closeModalFunc();
        });

        document.addEventListener('keydown', (e) => {
            if (e.key === 'Escape') closeModalFunc();
            if (e.key === 'ArrowLeft') showPrevImage();
            if (e.key === 'ArrowRight') showNextImage();
        });
    </script>
</body>
</html>