<?php
$title = 'Файлы';
$description = 'Файловый менеджер';
$contentStyle = '/css/files.css';
?>

<div class="folder">
    <div class="path">
        <a href="/files">📂 ...</a>
        <?php if ($subPath):
            $parts = explode('/', $subPath);
            $cumulative = '';
            foreach ($parts as $part):
                $cumulative .= ($cumulative ? '/' : '') . $part;
        ?>
            / <a href="/files/<?php echo $cumulative; ?>"><?php echo htmlspecialchars($part); ?></a>
        <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <ul class="file-list">
        <?php if ($subPath): ?>
            <a class="btn" href="/files/<?php echo $parentDir; ?>">Наверх</a>
        <?php endif; ?>

        <?php if (!empty($folders)): ?>
                <?php foreach ($folders as $folder):
            $folderPath = $basePath . '/' . $subPath . '/' . $folder;
            $items = scandir($folderPath);
            $itemCount = 0;
            foreach ($items as $item) {
                if ($item !== '.' && $item !== '..') {
                    $itemCount++;
                }
            }
            $folderParam = $subPath ? $subPath . '/' . $folder : $folder;
        ?>
        <li class="dir">
            <div class="item-container">
                <a href="/files/<?php echo $folderParam; ?>">
                    📁 <?php echo htmlspecialchars($folder); ?>
                </a>
                <div class="item-meta">
                    <span class="file-size"><?php echo $itemCount; ?> объектов</span>
                    <div class="item-actions">
                        <a href="/files.download/<?php echo $folderParam; ?>" 
                           class="btn download-btn" 
                           title="Скачать папку целиком">
                            ⬇️
                        </a>
                        <a href="/files.delete/<?php echo $folderParam; ?>" 
                            class="btn delete-btn" 
                            title="Удалить папку"> 
                            🗑️
                        </a>
                    </div>
                </div>
            </div>
        </li>
        <?php endforeach; ?>
        <?php endif; ?>

        <?php if (!empty($files)): ?>
        <?php foreach ($files as $file):
            $filePath = $basePath . '/' . $subPath . '/' . $file;
            $size = filesize($filePath);
            $fileParam = $subPath ? $subPath . '/' . $file : $file;
        ?>
        <li class="file">
            <div class="item-container">
                <a href="/files.download/<?php echo $fileParam; ?>">
                    📄 <?php echo htmlspecialchars($file); ?>
                </a>
                <div class="item-meta">
                    <span class="file-size"><?php echo formatBytes($size); ?></span>
                    <div class="item-actions">
                        <a href="/files.delete/<?php echo $fileParam; ?>" 
                            class="btn delete-btn" 
                            title="Удалить файл">
                            🗑️
                        </a>
                    </div>
                </div>
            </div>
        </li>
        <?php endforeach; ?>
        <?php endif; ?>

        <?php if (empty($folders) && empty($files)): ?>
        <li class="empty">Папка пуста</li>
        <?php endif; ?>
    </ul>

    <div class="info">
        <?php 
        $totalSize = 0;
        foreach ($files as $file) {
            $totalSize += filesize($basePath . '/' . $subPath . '/' . $file);
        }
        ?>
        📊 Всего: <?php echo count($folders) + count($files); ?> объектов 
        (папок: <?php echo count($folders); ?>, файлов: <?php echo count($files); ?>)
        <?php if ($totalSize > 0): ?>
        • Общий размер: <?php echo formatBytes($totalSize); ?>
        <?php endif; ?>
    </div>
</div>
