<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>RxAero Portal - .htaccess Manager</h1>";

$directories = ['.', 'admin', 'employee'];
$action = isset($_GET['action']) ? $_GET['action'] : 'disable';

if ($action == 'disable') {
    echo "<h2>Disabling .htaccess files</h2>";
    $renamed = 0;
    foreach ($directories as $dir) {
        $path = $dir . '/.htaccess';
        if (file_exists($path)) {
            $newPath = $dir . '/.htaccess_disabled';
            if (rename($path, $newPath)) {
                echo "✅ Renamed: $path → $newPath<br>";
                $renamed++;
            } else {
                echo "❌ Failed to rename: $path<br>";
            }
        } else {
            echo "ℹ️ No .htaccess found in $dir<br>";
        }
    }
    if ($renamed > 0) {
        echo "<p><strong>All .htaccess files have been disabled. <a href='?action=restore'>Click here to restore them</a>.</strong></p>";
    } else {
        echo "<p>No .htaccess files were found to disable.</p>";
    }
} elseif ($action == 'restore') {
    echo "<h2>Restoring .htaccess files</h2>";
    $restored = 0;
    foreach ($directories as $dir) {
        $disabledPath = $dir . '/.htaccess_disabled';
        if (file_exists($disabledPath)) {
            $originalPath = $dir . '/.htaccess';
            if (rename($disabledPath, $originalPath)) {
                echo "✅ Restored: $disabledPath → $originalPath<br>";
                $restored++;
            } else {
                echo "❌ Failed to restore: $disabledPath<br>";
            }
        } else {
            echo "ℹ️ No disabled .htaccess found in $dir<br>";
        }
    }
    if ($restored > 0) {
        echo "<p><strong>.htaccess files restored.</strong></p>";
    }
}

echo "<hr>";
echo "<p><a href='admin/chat.php' target='_blank'>Test Admin Chat</a> | <a href='employee/chat.php' target='_blank'>Test Employee Chat</a></p>";
?>