<?php
session_start();
error_reporting(0);

$PASS_GATE = "BosLevy";
$file = 'users.json';

// Inisialisasi file jika belum ada
if (!file_exists($file)) {
    file_put_contents($file, json_encode([]));
}

if (isset($_POST['gate_pass'])) {
    if ($_POST['gate_pass'] === $PASS_GATE) {
        $_SESSION['admin_auth'] = true;
    } else {
        $error_gate = "❌ Password Salah!";
    }
}

$is_authenticated = isset($_SESSION['admin_auth']) && $_SESSION['admin_auth'] === true;
$users = json_decode(file_get_contents($file), true) ?: [];

// Fungsi Hapus Folder Secara Rekursif
function deleteDir($dirPath) {
    if (!is_dir($dirPath)) return;
    $files = array_diff(scandir($dirPath), array('.', '..'));
    foreach ($files as $file) {
        (is_dir("$dirPath/$file")) ? deleteDir("$dirPath/$file") : unlink("$dirPath/$file");
    }
    return rmdir($dirPath);
}

// Logic: Hapus Panel (Folder & Data)
if (isset($_GET['hapus_panel']) && $is_authenticated) {
    $target_user = $_GET['user'];
    $folder = $_GET['hapus_panel'];
    foreach ($users as $key => $u) {
        if ($u['username'] === $target_user) {
            deleteDir(__DIR__ . "/" . $folder);
            $users[$key]['panels'] = array_values(array_diff($u['panels'], [$folder]));
            file_put_contents($file, json_encode(array_values($users), JSON_PRETTY_PRINT));
            header("Location: " . $_SERVER['PHP_SELF']); exit;
        }
    }
}

// Logic: Hapus User Secara Total
if (isset($_GET['hapus_user']) && $is_authenticated) {
    $target = $_GET['hapus_user'];
    foreach ($users as $key => $u) {
        if ($u['username'] === $target) {
            foreach ($u['panels'] as $f) { deleteDir(__DIR__ . "/" . $f); }
            unset($users[$key]);
            file_put_contents($file, json_encode(array_values($users), JSON_PRETTY_PRINT));
            header("Location: " . $_SERVER['PHP_SELF']); exit;
        }
    }
}

// Logic: Update Max Limit Panel
if (isset($_POST['update_limit']) && $is_authenticated) {
    $target_user = $_POST['user_limit'];
    $new_limit = (int)$_POST['new_limit'];
    foreach ($users as $key => $u) {
        if ($u['username'] === $target_user) {
            $users[$key]['limit'] = $new_limit;
            file_put_contents($file, json_encode(array_values($users), JSON_PRETTY_PRINT));
            header("Location: " . $_SERVER['PHP_SELF']); exit;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elite Admin | Levy Hosting</title>
    <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@500;700&family=Inter:wght@300;400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #00f2ff;
            --secondary: #7000ff;
            --danger: #ff2e63;
            --bg: #050507;
            --card-bg: rgba(20, 20, 25, 0.8);
        }

        body {
            background-color: var(--bg);
            background-image: 
                radial-gradient(circle at 10% 20%, rgba(112, 0, 255, 0.1) 0%, transparent 40%),
                radial-gradient(circle at 90% 80%, rgba(0, 242, 255, 0.1) 0%, transparent 40%);
            color: #e0e0e0;
            font-family: 'Inter', sans-serif;
            margin: 0;
            padding: 20px;
            min-height: 100vh;
        }

        .container { max-width: 700px; margin: auto; }

        /* Auth Screen */
        .gate-box {
            margin-top: 15vh;
            background: var(--card-bg);
            padding: 40px;
            border-radius: 24px;
            border: 1px solid rgba(0, 242, 255, 0.2);
            text-align: center;
            backdrop-filter: blur(20px);
            box-shadow: 0 20px 50px rgba(0,0,0,0.5);
        }

        .gate-box input {
            width: 100%;
            padding: 14px;
            background: rgba(0,0,0,0.3);
            border: 1px solid #333;
            color: #fff;
            border-radius: 12px;
            margin: 20px 0;
            box-sizing: border-box;
            outline: none;
            transition: 0.3s;
        }

        .gate-box input:focus { border-color: var(--primary); box-shadow: 0 0 15px rgba(0, 242, 255, 0.2); }

        .btn-gate {
            width: 100%;
            background: linear-gradient(45deg, var(--primary), var(--secondary));
            color: #fff;
            padding: 14px;
            border: none;
            border-radius: 12px;
            font-weight: bold;
            cursor: pointer;
            font-family: 'Rajdhani';
            text-transform: uppercase;
            letter-spacing: 2px;
        }

        /* Header & Search */
        h2 { font-family: 'Rajdhani'; text-transform: uppercase; letter-spacing: 3px; color: var(--primary); text-align: center; margin-bottom: 30px; }

        .search-container { margin-bottom: 25px; position: relative; }
        .search-input {
            width: 100%;
            padding: 15px 15px 15px 45px;
            background: var(--card-bg);
            border: 1px solid rgba(255,255,255,0.05);
            border-radius: 15px;
            color: #fff;
            box-sizing: border-box;
            outline: none;
            transition: 0.3s;
        }
        .search-input:focus { border-color: var(--primary); }
        .search-icon { position: absolute; left: 18px; top: 50%; transform: translateY(-50%); color: var(--primary); opacity: 0.7; }

        /* User Cards */
        .user-card {
            background: var(--card-bg);
            border-radius: 20px;
            border: 1px solid rgba(255,255,255,0.05);
            padding: 20px;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .user-card:hover { border-color: rgba(0, 242, 255, 0.3); transform: translateY(-3px); }

        .user-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(255,255,255,0.05);
        }

        .username { color: var(--primary); font-family: 'Rajdhani'; font-size: 1.2rem; font-weight: 700; }

        .limit-box { display: flex; align-items: center; gap: 8px; }
        .limit-input {
            width: 50px;
            background: rgba(0,0,0,0.4);
            border: 1px solid #444;
            color: var(--primary);
            border-radius: 8px;
            padding: 5px;
            text-align: center;
            font-weight: bold;
        }

        .panel-tag {
            background: rgba(255,255,255,0.03);
            padding: 6px 12px;
            border-radius: 10px;
            font-size: 12px;
            display: inline-flex;
            align-items: center;
            margin: 4px;
            border: 1px solid rgba(255,255,255,0.08);
        }

        .btn-del-p { color: var(--danger); margin-left: 8px; text-decoration: none; opacity: 0.6; transition: 0.3s; }
        .btn-del-p:hover { opacity: 1; transform: scale(1.2); }

        .btn-user-del {
            margin-top: 15px;
            width: 100%;
            background: rgba(255, 46, 99, 0.1);
            color: var(--danger);
            padding: 10px;
            border: 1px solid var(--danger);
            border-radius: 12px;
            text-decoration: none;
            display: block;
            text-align: center;
            font-size: 12px;
            font-weight: 600;
            transition: 0.3s;
        }

        .btn-user-del:hover { background: var(--danger); color: #fff; }

        .footer { text-align: center; margin-top: 40px; padding-bottom: 30px; }
        .btn-footer { color: #888; text-decoration: none; font-size: 14px; margin: 0 10px; transition: 0.3s; }
        .btn-footer:hover { color: var(--primary); }

        #noResult { text-align: center; padding: 40px; color: #555; display: none; }

        /* Chrome, Safari, Edge, Opera - Remove arrows from number input */
        input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
    </style>
</head>
<body>

<div class="container">
    <?php if (!$is_authenticated): ?>
        <div class="gate-box">
            <div style="font-size: 40px; margin-bottom: 10px;">🛡️</div>
            <h2>CORE ACCESS</h2>
            <?php if(isset($error_gate)) echo "<p style='color:var(--danger); font-size:13px;'>$error_gate</p>"; ?>
            <form method="POST">
                <input type="password" name="gate_pass" placeholder="Enter Security Key..." required autofocus>
                <button type="submit" class="btn-gate">INITIALIZE AUTH</button>
            </form>
        </div>
    <?php else: ?>
        <h2><i class="fas fa-terminal"></i> SYSTEM ADMINISTRATOR</h2>
        
        <div class="search-container">
            <i class="fas fa-search search-icon"></i>
            <input type="text" id="searchInput" class="search-input" placeholder="Search by username..." onkeyup="filterUsers()">
        </div>

        <div id="userList">
            <?php foreach ($users as $u): ?>
            <div class="user-card" data-username="<?= strtolower($u['username']) ?>">
                <div class="user-header">
                    <div class="username"><i class="fas fa-id-badge"></i> <?= htmlspecialchars($u['username']) ?></div>
                    <form method="POST" class="limit-box">
                        <input type="hidden" name="user_limit" value="<?= $u['username'] ?>">
                        <span style="font-size: 10px; color: #777;">MAX PANEL:</span>
                        <input type="number" name="new_limit" value="<?= $u['limit'] ?>" class="limit-input">
                        <button type="submit" name="update_limit" style="background:none; border:none; color:var(--primary); cursor:pointer;">
                            <i class="fas fa-save"></i>
                        </button>
                    </form>
                </div>
                
                <div style="font-size: 11px; color: #666; margin-bottom: 10px; text-transform: uppercase; letter-spacing: 1px;">Active Directories:</div>
                <div class="panel-list">
                    <?php if(empty($u['panels'])): ?>
                        <span style="color:#444; font-size:12px; font-style: italic;">No active panels found.</span>
                    <?php else: ?>
                        <?php foreach($u['panels'] as $p): ?>
                            <div class="panel-tag">
                                <i class="fas fa-folder" style="margin-right: 8px; color: #ffca28; font-size: 10px;"></i>
                                <?= htmlspecialchars($p) ?> 
                                <a href="?user=<?= urlencode($u['username']) ?>&hapus_panel=<?= urlencode($p) ?>" class="btn-del-p" onclick="return confirm('Wipe directory <?= $p ?>?')">
                                    <i class="fas fa-times-circle"></i>
                                </a>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
                
                <a href="?hapus_user=<?= urlencode($u['username']) ?>" class="btn-user-del" onclick="return confirm('ERASE USER PERMANENTLY?')">
                    <i class="fas fa-user-slash"></i> TERMINATE ACCOUNT
                </a>
            </div>
            <?php endforeach; ?>
        </div>

        <div id="noResult">
            <i class="fas fa-ghost" style="font-size: 30px; margin-bottom: 10px;"></i>
            <p>No matches found in database.</p>
        </div>

        <div class="footer">
            <a href="kezz.php" class="btn-footer"><i class="fas fa-home"></i> Dashboard</a>
            <a href="logout.php" class="btn-footer" style="color: var(--danger);"><i class="fas fa-power-off"></i> Logout System</a>
        </div>
    <?php endif; ?>
</div>

<script>
function filterUsers() {
    let input = document.getElementById('searchInput').value.toLowerCase();
    let cards = document.getElementsByClassName('user-card');
    let noResult = document.getElementById('noResult');
    let visibleCount = 0;

    for (let i = 0; i < cards.length; i++) {
        let username = cards[i].getAttribute('data-username');
        if (username.includes(input)) {
            cards[i].style.display = "block";
            visibleCount++;
        } else {
            cards[i].style.display = "none";
        }
    }
    noResult.style.display = (visibleCount === 0) ? "block" : "none";
}
</script>

</body>
</html>