<?php
require_once '../config/db.php';
require_once 'layout.php';

// OVERRIDE: Admin panel needs HTML
header('Content-Type: text/html; charset=utf-8');

// Define Upload Paths
$baseDir = '../uploads';
$dirs = ['avatars', 'services', 'chat'];

// Ensure Dirs Exist
foreach ($dirs as $d) {
    if (!is_dir("$baseDir/$d")) mkdir("$baseDir/$d", 0777, true);
}

// Get Files (Simple mock scanner)
$files = [];
foreach ($dirs as $d) {
    $path = "$baseDir/$d";
    $scan = scandir($path);
    foreach ($scan as $f) {
        if ($f !== '.' && $f !== '..') {
            $files[] = [
                'name' => $f,
                'path' => "$path/$f",
                'folder' => $d,
                'size' => filesize("$path/$f"),
                'time' => filemtime("$path/$f")
            ];
        }
    }
}

renderAdminSidebar('files');
?>

<div class="flex flex-col h-[calc(100vh-140px)]">
    <!-- Header -->
    <div class="flex justify-between items-center mb-6">
        <div>
            <h2 class="text-2xl font-bold text-white tracking-tight">Gestor de Archivos</h2>
            <p class="text-gray-400 text-sm">Explora los archivos subidos por los usuarios.</p>
        </div>
        <div class="flex gap-2 text-sm text-gray-400 bg-dark-800 px-4 py-2 rounded-lg border border-dark-700">
            <span><i class="fas fa-hdd mr-2"></i> Uso de Disco: <b><?php echo count($files) * 0.5; // Mock Calc ?> MB</b></span>
        </div>
    </div>

    <!-- File Browser -->
    <div class="flex-1 bg-dark-800 rounded-2xl border border-dark-700 shadow-xl overflow-hidden flex flex-col">
        <!-- Toolbar -->
        <div class="p-4 border-b border-dark-700 bg-dark-900/30 flex gap-4">
             <button class="text-gray-400 hover:text-white flex items-center gap-2"><i class="fas fa-level-up-alt"></i> Subir Nivel</button>
             <div class="h-6 w-px bg-dark-700"></div>
             <button class="text-brand-400 flex items-center gap-2 font-bold"><i class="fas fa-folder text-yellow-500"></i> uploads/</button>
        </div>

        <!-- Grid -->
        <div class="flex-1 overflow-y-auto p-6">
            <div class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 xl:grid-cols-8 gap-4">
                
                <!-- Folders -->
                <?php foreach ($dirs as $d): ?>
                <div class="group cursor-pointer">
                    <div class="aspect-square bg-dark-900 rounded-xl border border-dark-700 flex flex-col items-center justify-center hover:bg-dark-700 hover:border-brand-500 transition relative">
                        <i class="fas fa-folder text-4xl text-yellow-500 mb-2 group-hover:scale-110 transition"></i>
                        <span class="text-xs text-gray-400 font-bold"><?php echo $d; ?></span>
                         <span class="absolute top-2 right-2 text-[10px] bg-dark-800 px-1.5 rounded text-gray-500">DIR</span>
                    </div>
                </div>
                <?php endforeach; ?>

                <!-- Files (Mock Display since real dir is empty initially) -->
                 <?php foreach ($files as $f): ?>
                <div class="group cursor-pointer">
                    <div class="aspect-square bg-dark-900 rounded-xl border border-dark-700 flex flex-col items-center justify-center hover:bg-dark-700 hover:border-brand-500 transition relative overflow-hidden">
                        <?php if (in_array(pathinfo($f['name'], PATHINFO_EXTENSION), ['jpg', 'png', 'webp'])): ?>
                             <img src="<?php echo $f['path']; ?>" class="absolute inset-0 w-full h-full object-cover opacity-50 group-hover:opacity-100 transition">
                        <?php else: ?>
                            <i class="fas fa-file text-4xl text-gray-500 mb-2"></i>
                        <?php endif; ?>
                        
                        <div class="absolute bottom-0 inset-x-0 bg-black/70 p-2 truncate text-center">
                            <span class="text-xs text-gray-200"><?php echo $f['name']; ?></span>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>

                <?php if(empty($files)): ?>
                     <div class="col-span-full border-2 border-dashed border-dark-700 rounded-xl p-8 text-center opacity-50">
                        <p class="text-gray-500">No hay archivos en la raíz pública.</p>
                     </div>
                <?php endif; ?>

            </div>
        </div>
    </div>
</div>

<?php renderAdminFooter(); ?>
