<?php
$titre = 'Galerie SVG';

function getSvgFiles($dir) {
    $files = [];
    $items = scandir($dir);
    foreach ($items as $item) {
        if ($item == '.' || $item == '..') continue;
        $path = $dir . '/' . $item;
        if (is_dir($path)) {
            $files[$item] = getSvgFiles($path);
        } elseif (pathinfo($path, PATHINFO_EXTENSION) == 'svg') {
            $files[] = $path;
        }
    }
    return $files;
}

$files = getSvgFiles('.');

function renderGallery($files, $folder = null) {
    $html = '<div class="gallery">';
    foreach ($files as $key => $file) {
        if (is_array($file)) continue; // Ignorer les dossiers dans cette fonction
        $filePath = is_numeric($key) ? $file : $key . '/' . $file;
        $fileName = basename($filePath);
        $html .= '<div class="gallery__item" data-src="' . $filePath . '">
                    <img src="' . $filePath . '" alt="' . $fileName . '">
                    <p class="filename">' . $fileName . '</p>
                </div>';
    }
    $html .= '</div>';
    return $html;
}

$foldersHtml = '<div class="folders">';
$imagesHtml = '<div class="images">';

// Afficher les fichiers SVG dans le dossier racine
$imagesHtml .= renderGallery($files);

// Afficher les dossiers et leurs contenus
foreach ($files as $folder => $contents) {
    if (is_array($contents)) {
        $foldersHtml .= '<div class="folder" data-folder="' . $folder . '">
                            <div class="folder__header">
                                <img src="./dossier.png" alt="Dossier">
                                <h2>' . $folder . '</h2>
                            </div>
                            <div class="folder__gallery" id="' . $folder . '" style="display:none;">' . renderGallery($contents, $folder) . '</div>
                        </div>';
    }
}

$foldersHtml .= '</div>';
$imagesHtml .= '</div>';

$vue = '<div class="main">
            <h1>' . $titre . '</h1>
            <div class="content">
                ' . $foldersHtml . '
                ' . $imagesHtml . '
            </div>
        </div>';
?>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script async src="https://kit.fontawesome.com/6cc05e1e8e.js" crossorigin="anonymous"></script>
    <meta charset="UTF-8" />
    <title><?php echo $titre; ?></title>
    <style>
        /* Styles CSS ici... */
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        body {
            font-family: sans-serif;
            background-color: #f4f4f4;
        }

        .main {
            width: 95%;
            max-width: 1200px;
            margin: 20px auto;
        }

        h1 {
            margin-bottom: 30px;
            color: #333;
            font-size: 2.5em;
            text-align: center;
        }

        .content {
            display: grid;
            grid-template-columns: 250px 1fr; /* Dossiers à gauche, images à droite */
            gap: 20px;
        }

        .folders {
            display: flex;
            flex-direction: column;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 10px;
        }

        .folder {
            margin: 10px 0;
        }

        .folder__header {
            padding: 10px;
            background-color: #f0f0f0;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: left;
        }

        .folder__header img {
            width: 30px;
            height: 30px;
            margin-right: 10px;
        }

        .folder__header h2 {
            color: #333;
            font-size: 1em;
        }

        .gallery {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            grid-gap: 10px;
            padding: 10px;
        }

        .gallery__item {
            cursor: pointer;
            overflow: hidden;
            border-radius: 4px;
            text-align: center;
        }

        .gallery__item img {
            width: 100%;
            height: 150px;
            object-fit: contain;
            transition: transform 0.3s ease;
        }

        .gallery__item img:hover {
            transform: scale(1.1);
        }

        .filename {
            font-size: 0.9em;
            margin-top: 5px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        /* Modal */
        .modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(250, 250, 250, 0.9);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }

        .modal img {
            width: 50%;
            height: 50%;
        }

        .modal .closeBtn {
            margin-top: -35%;
            color: #333;
            font-size: 2em;
            cursor: pointer;
        }

        .modal .copyBtn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            margin-top: 20px;
			margin-left: 30px;
            border-radius: 5px;
            cursor: pointer;
        }

        /* Bouton retour en haut */
        .back-to-top {
            position: fixed;
            bottom: 50px;
            right: 50px;
            background-color: #333;
            color: white;
            border: none;
            border-radius: 50%;
            padding: 10px;
            cursor: pointer;
            display: none;
            z-index: 1000;
        }
    </style>
</head>
<body>
    <?php echo $vue; ?>
    <button class="back-to-top" id="backToTopBtn">
        <i class="fas fa-arrow-up"></i>
    </button>
    <script>
        document.querySelectorAll('.folder__header').forEach(header => {
            header.addEventListener('click', () => {
                const folder = header.parentElement;
                const folderGallery = folder.querySelector('.folder__gallery');
                const imagesGallery = document.querySelector('.images .gallery');

                imagesGallery.innerHTML = folderGallery.innerHTML;

                // Ré-appliquer les écouteurs d'événements après avoir mis à jour le contenu de la galerie
                imagesGallery.querySelectorAll('.gallery__item').forEach(item => {
                    item.addEventListener('click', () => {
                        const src = item.dataset.src;
                        createModal(src);
                    });
                });
            });
        });

        // Écouteurs d'événements pour les images initiales
        document.querySelectorAll('.images .gallery__item').forEach(item => {
            item.addEventListener('click', () => {
                const src = item.dataset.src;
                createModal(src);
            });
        });

        function createModal(src) {
            const modal = document.createElement('div');
            modal.classList.add('modal');
            modal.innerHTML = `
                <img src="${src}" alt="Image">
                <i class="fas fa-times closeBtn"></i>
                <button class="copyBtn">Copier l'adresse</button>
            `;
            document.body.appendChild(modal);

            modal.querySelector('.closeBtn').addEventListener('click', () => {
                modal.remove();
            });

            modal.querySelector('.copyBtn').addEventListener('click', () => {
                navigator.clipboard.writeText(src).then(() => {
                    alert("Adresse de l'image copiée !");
                }).catch(err => {
                    console.error('Impossible de copier l\'adresse : ', err);
                    alert("Impossible de copier l'adresse.");
                });
            });
        }
        // Bouton retour en haut
        const backToTopBtn = document.getElementById("backToTopBtn");

        window.onscroll = function() {
            if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
                backToTopBtn.style.display = "block";
            } else {
                backToTopBtn.style.display = "none";
            }
        };

        backToTopBtn.addEventListener('click', () => {
            window.scrollTo({ top: 0, behavior: 'smooth' });
        });
    </script>
</body>
</html>