<?php
// Function to get the size of a folder
function getFolderSize($folderPath) {
    $totalSize = 0;
    $files = glob(rtrim($folderPath, '/') . '/*');

    foreach ($files as $file) {
        if (is_file($file)) {
            $totalSize += filesize($file);
        }
        if (is_dir($file)) {
            $totalSize += getFolderSize($file);
        }
    }

    return $totalSize;
}

// Get the current directory
$currentDir = __DIR__;

// Check if the form is submitted
if (isset($_POST['submit'])) {
    // Retrieve the selected folders
    $selectedFolders = $_POST['folders'];

    // Get the list of files in each folder
    $fileLists = [];
    foreach ($selectedFolders as $folder) {
        $folderPath = $currentDir . '/' . $folder;
        $files = glob($folderPath . '/*.txt');
        $fileNames = array_map('basename', $files);
        $fileLists[$folder] = $fileNames;
    }

    // Display the contents of the selected folders in a table grid
    echo '<table>';
    echo '<tr>';
    echo '<th>File Name</th>';

    // Display folder names as table headers
    foreach ($selectedFolders as $folder) {
        echo '<th>' . $folder . '</th>';
    }

    echo '</tr>';

    // Iterate through each file
    $allFiles = array_reduce($fileLists, 'array_merge', []);
    $uniqueFiles = array_unique($allFiles);

    foreach ($uniqueFiles as $fileName) {
        echo '<tr>';
        echo '<td>' . $fileName . '</td>';

        // Get the file size for the first folder
        $firstFolder = $selectedFolders[0];
        $firstFolderPath = $currentDir . '/' . $firstFolder;
        $firstFilePath = $firstFolderPath . '/' . $fileName;
        $firstFileSize = file_exists($firstFilePath) ? filesize($firstFilePath) : 0;

        // Display the file size for each folder and compare with the first folder
        foreach ($selectedFolders as $folder) {
            $folderPath = $currentDir . '/' . $folder;
            $filePath = $folderPath . '/' . $fileName;

            if (file_exists($filePath)) {
                $fileSize = filesize($filePath);

                echo '<td>';

                if ($fileSize === $firstFileSize) {
                    echo '<a target="_blank" href="" style=color:green>[OK]</a>';
                } elseif ($fileSize > $firstFileSize) {
                    echo '<a target="_blank" href="" style=color:darkgreen>[OK - GREATER]</a>';
                } else {
                    echo '<a target="_blank" href="" style=color:red>[NO - LESSER]</a>';
                }

                echo ' ' . $fileSize . ' bytes</td>';
            } else {
                echo '<td>-</td>'; // Display "-" for missing files
            }
        }

        echo '</tr>';
    }

    echo '</table>';
}

// Get the folders in the current directory
$folders = array_filter(glob('*'), 'is_dir');

// Display the form
echo '<form method="post">';
foreach ($folders as $index => $folder) {
    $checked = ($index === 0) ? 'checked' : '';
    echo '<input type="checkbox" name="folders[]" value="' . $folder . '" ' . $checked . '>' . $folder . '<br>';

    // Get the size of each folder
    $folderPath = $currentDir . '/' . $folder;
    $folderSize = getFolderSize($folderPath);

    // Store the folder size in a hidden input field
    echo '<input type="hidden" name="folder_sizes[' . $folder . ']" value="' . $folderSize . '">';
}
echo '<input type="submit" name="submit" value="Submit">';
echo '</form>';
?>