<section class="section">
    <div class="band">
        <div class="card">
            <div class="entry-section">
                <?php
                $serverURL = rtrim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), '/') ?: '/';
                $parentURL = $serverURL === '/' ? '/' : dirname($serverURL);
                $parentURL = $parentURL === '\\' || $parentURL === '.' ? '/' : $parentURL;
                $folderSlug = basename($serverURL);
                $folderName = ucwords(str_replace(['-', '_'], ' ', $folderSlug));
                $directory = __DIR__;
                $items = scandir($directory) ?: [];

                $folders = [];
                $files = [];

                foreach ($items as $item) {
                    if ($item === '.' || $item === '..' || str_starts_with($item, '.')) {
                        continue;
                    }

                    $path = $directory . DIRECTORY_SEPARATOR . $item;

                    if (is_dir($path)) {
                        $folders[] = $item;
                        continue;
                    }

                    if (is_file($path) && basename($item) !== 'index.php' && in_array(pathinfo($item, PATHINFO_EXTENSION), ['php', 'md'], true)) {
                        $files[] = pathinfo($item, PATHINFO_FILENAME);
                    }
                }

                natcasesort($folders);
                natcasesort($files);
                ?>
                <div class="flex justify-between items-center border-b border-slate-200 dark:border-slate-700 pb-5">
                    <div class="text-4xl font-bold"><?= e($folderName) ?></div>
                    <a class="btn btn-primary" href="<?= e($parentURL) ?>">
                        Back
                    </a>
                </div>
                <ul class="m-4">
                    <?php
                    foreach ($folders as $folder) :
                        $itemName = ucwords(str_replace(['-', '_'], ' ', $folder)); ?>
                        <li class="mt-2">
                            <div class="flex items-center">
                                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.75V12A2.25 2.25 0 0 1 4.5 9.75h15A2.25 2.25 0 0 1 21.75 12v.75m-8.69-6.44-2.12-2.12a1.5 1.5 0 0 0-1.061-.44H4.5A2.25 2.25 0 0 0 2.25 6v12a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9a2.25 2.25 0 0 0-2.25-2.25h-5.379a1.5 1.5 0 0 1-1.06-.44Z" />
                                </svg>

                                <a class=' ms-2 hover:text-blue-600 font-bold' href='<?= e(rtrim($serverURL, '/') . '/' . $folder) ?>'>
                                    <?= e($itemName) ?></a>
                            </div>

                        </li>
                    <?php endforeach ?>
                </ul>
                <ul class="m-4">
                    <?php foreach ($files as $fileName) :
                        $title = ucwords(str_replace(['-', '_'], ' ', $fileName)); ?>
                        <li class="mt-2">
                            <div class="flex items-center">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="size-5">
                                    <path fill-rule="evenodd" d="M8.22 5.22a.75.75 0 0 1 1.06 0l4.25 4.25a.75.75 0 0 1 0 1.06l-4.25 4.25a.75.75 0 0 1-1.06-1.06L11.94 10 8.22 6.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd" />
                                </svg>

                                <a class='ms-1 hover:text-blue-600' href='<?= e(rtrim($serverURL, '/') . '/' . $fileName) ?>'><?= e($title) ?></a>
                            </div>
                        </li>
                    <?php endforeach ?>
                </ul>
            </div>
        </div>
    </div>
</section>