<?php
session_start();
require_once 'php/adminconfig.php';

// Enhanced login and role check
if (!isset($_SESSION['admin_id']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: signin-admin.php');
    exit();
}

// Simple logout
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
    session_destroy();
    header('Location: signin-admin.php');
    exit();
}

// Function to get human-readable file size
function formatFileSize($bytes) {
    if ($bytes >= 1073741824) {
        return number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        return number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        return number_format($bytes / 1024, 2) . ' KB';
    } else {
        return $bytes . ' bytes';
    }
}

// Get user folder if specified
$current_path = isset($_GET['folder']) ? $_GET['folder'] : '';
$uploads_dir = 'uploads/' . $current_path;

// Validate path to prevent directory traversal
if (strpos(realpath($uploads_dir), realpath('uploads')) !== 0) {
    die('Invalid directory');
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta name="robots" content="noindex, nofollow">
    <link rel="shortcut icon" type="image/png" href="images/icon.jpg" />
    <link rel="stylesheet" type="text/css" href="css/admin.css">
    <title>File Manager - Uploads</title>
    <style>
        
        .contaiiner {
            max-width: 1200px;
            margin: 10px 20px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding: 20px;
            background-color: #f4f4f4;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            border-radius: 8px;
            color: #555;
        }

        .header a:hover {
            transform: scale(1.1);
        }

        .breadcrumb {
            margin-bottom: 20px;
            padding: 10px;
            background-color: #f4f4f4;
            border-radius: 4px;
        }

        .breadcrumb a {
            color: #64748b;
            text-decoration: none;
        }

        .breadcrumb a:hover {
            color: white;
        }

        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
        }

        .item {
            background-color: #f4f4f4;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            border-radius: 8px;
            padding: 15px;
            text-align: center;
            transition: transform 0.2s;
            color: #000;
        }

        .item:hover {
            transform: translateY(-5px);
        }

        .item img {
            max-width: 100%;
            height: auto;
            border-radius: 4px;
            margin-bottom: 10px;
        }

        .folder-icon {
            font-size: 48px;
            margin-bottom: 10px;
            color: #000;
        }

        .item-name {
            font-size: 14px;
            margin-bottom: 5px;
            word-break: break-all;
        }

        .item-info {
            font-size: 12px;
            color: #64748b;
        }

        
        .empty-message {
            text-align: center;
            padding: 40px;
            background-color: #1a2942;
            border-radius: 8px;
            grid-column: 1 / -1;
        }
    </style>
</head>
<body>

    <?php include "required/admin-nav.php" ?>

    <div class="pc">
    <div class="contaiiner">
        <div class="header">
            <h1>Upload Manager</h1>
            <a href="admin-payments.php" style="background: purple; padding: 10px 15px; border-radius: 5px; text-decoration: none; color: #fff; font-weight: 600; transition: 1s ease;">Return to payments detail</a>

            <a href="?" style="background: purple; padding: 10px 15px; border-radius: 5px; text-decoration: none; color: #fff; font-weight: 600; transition: 1s ease;"><i class="fas fa-user"></i> Image Gallary</a>
        </div>

        <div class="breadcrumb">
            <a href="?">uploads</a>
            <?php
            if ($current_path) {
                echo ' / ' . str_replace('/', ' / ', trim($current_path, '/'));
            }
            ?>
        </div>

        <div class="grid">
            <?php
            // Read directory contents
            $items = array_diff(scandir($uploads_dir), array('.', '..'));
            
            if (empty($items)) {
                echo '<div class="empty-message">No files or folders found</div>';
            } else {
                foreach ($items as $item) {
                    $item_path = $uploads_dir . '/' . $item;
                    $is_dir = is_dir($item_path);
                    $file_size = $is_dir ? '' : formatFileSize(filesize($item_path));
                    $modified_time = date('Y-m-d H:i:s', filemtime($item_path));
                    
                    echo '<div class="item">';
                    if ($is_dir) {
                        // Show folder
                        echo '<a href="?folder=' . ($current_path ? $current_path . '/' : '') . $item . '" style="text-decoration: none; color: white;">';
                        echo '<div class="folder-icon">📁</div>';
                        echo '<div class="item-name" style="color: black;">' . htmlspecialchars($item) . '</div>';
                        echo '</a>';
                    } else {
                        // Show file/image
                        $is_image = in_array(strtolower(pathinfo($item, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'png', 'gif']);
                        if ($is_image) {
                            echo '<img src="' . $item_path . '" alt="' . htmlspecialchars($item) . '">';
                        } else {
                            echo '<div class="folder-icon">📄</div>';
                        }
                        echo '<div class="item-name">' . htmlspecialchars($item) . '</div>';
                        echo '<div class="item-info">' . $file_size . '<br>' . $modified_time . '</div>';
                    }
                    echo '</div>';
                }
            }
            ?>
        </div>
    </div>
</div>
</body>
</html>