<?php
// ==========================================
// LOGIKA PHP & CEK SERVER
// ==========================================
session_start();

// Cek apakah user sudah login, jika belum lempar ke index.php
if (!isset($_SESSION['username'])) {
    header("Location: index.php");
    exit();
}

$username = htmlspecialchars($_SESSION['username']);
// Mengambil data platform dari session (default ke 'java' jika tidak diset)
$platform = isset($_SESSION['platform']) ? $_SESSION['platform'] : 'java';
$name_path = ($platform === 'bedrock') ? "bedrock-" . urlencode($username) : urlencode($username);
$store_base_url = "https://store.fanaesmc.me/" . $name_path;

// Logika URL Dinamis Web Store
if ($platform === 'bedrock') {
    $store_url = "https://store.fanaesmc.me/bedrock-" . urlencode($username) . "/";
} else {
    $store_url = "https://store.fanaesmc.me/" . urlencode($username) . "/";
}

// Cek Server Otomatis
$server_ip = 'play.anymhost.com';
$server_port = 19132;
$timeout = 2;

// fsockopen untuk mengecek ping ke server
$connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $timeout);

if ($connection) {
    $server_is_online = true;
    fclose($connection);
} else {
    $server_is_online = false;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - ANYMHOST</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <style>
        /* CSS Reset & Basic Setup */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #0f172a; /* Dark mode background */
            color: #f8fafc;
            line-height: 1.6;
            padding: 20px;
        }

        /* Kontainer Utama - Dibatasi lebarnya agar rapi di PC & HP */
        .container {
            max-width: 900px;
            margin: 0 auto;
            width: 100%;
        }

        /* Header */
        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 30px;
        }

        .logo {
            max-width: 120px;
            height: auto;
        }

        .btn-logout {
            background-color: #ef4444;
            color: white;
            text-decoration: none;
            padding: 8px 16px;
            border-radius: 6px;
            font-weight: 600;
            font-size: 0.9rem;
            transition: background-color 0.3s ease;
        }

        .btn-logout:hover {
            background-color: #dc2626;
        }

        /* Teks Sapaan */
        h1 {
            font-size: 1.8rem;
            margin-bottom: 25px;
            font-weight: 700;
            word-wrap: break-word; /* Mencegah teks panjang meluap di HP */
        }

        /* Menu Navigasi Grid */
        .menu-grid {
            display: grid;
            /* Grid dinamis: Akan berjejer 3 di PC, dan menyusun ke bawah di layar HP */
            grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .menu-card {
            background-color: #1e293b;
            border-radius: 12px;
            padding: 30px 20px;
            text-align: center;
            text-decoration: none;
            color: #f8fafc;
            display: flex;
            flex-direction: column;
            align-items: center;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
            transition: transform 0.3s ease, box-shadow 0.3s ease, background-color 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.05);
        }

        .menu-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.4);
            background-color: #26354c;
        }

        .menu-card i {
            font-size: 40px;
            margin-bottom: 15px;
            color: #3b82f6; /* Warna aksen ikon */
        }

        .menu-card h2 {
            font-size: 1.2rem;
            font-weight: 600;
        }

        /* Kotak Status Server */
        .status-box {
            background-color: #1e293b;
            border-radius: 12px;
            padding: 25px;
            border: 1px solid rgba(255, 255, 255, 0.05);
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .status-header h3 {
            font-size: 1.1rem;
            color: #94a3b8;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 5px;
        }

        .status-header p {
            font-family: 'Courier New', Courier, monospace;
            font-weight: bold;
            font-size: 1.1rem;
            color: #f8fafc;
            word-wrap: break-word;
        }

        .status-indicator {
            display: inline-flex;
            align-items: center;
            padding: 8px 16px;
            border-radius: 50px;
            font-weight: bold;
            font-size: 0.95rem;
            background-color: rgba(0, 0, 0, 0.2);
            width: fit-content;
        }

        .dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 10px;
        }

        /* Warna Dinamis */
        .online {
            color: #22c55e;
            border: 1px solid rgba(34, 197, 94, 0.3);
        }
        .online .dot {
            background-color: #22c55e;
            box-shadow: 0 0 8px #22c55e;
        }

        .offline {
            color: #ef4444;
            border: 1px solid rgba(239, 68, 68, 0.3);
        }
        .offline .dot {
            background-color: #ef4444;
            box-shadow: 0 0 8px #ef4444;
        }

        /* Media Query Ekstra untuk HP Layar Sangat Kecil */
        @media (max-width: 400px) {
            header {
                flex-direction: column;
                gap: 15px;
            }
            h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>

    <div class="container">
        <header>
            <img src="logo.png" alt="ANYMHOST Logo" class="logo">
            <a href="/" class="btn-logout"><i class="fa-solid fa-power-off"></i> Logout</a>
        </header>

        <h1>Selamat Datang, <?php echo $username; ?>!</h1>

        <div class="menu-grid">
            <a href="profile" class="menu-card">
                <i class="fa-solid fa-user"></i>
                <h2>Profile</h2>
            </a>
            <a href="rules.php" class="menu-card">
                <i class="fa-solid fa-book"></i>
                <h2>Rules</h2>
            </a>

            <a href="money.php" class="menu-card">
                <i class="fa-solid fa-coins"></i>
                <h2>Money</h2>
            </a>

            <a href="<?php echo $store_base_url; ?>/ranks" class="menu-card">
                <i class="fa-solid fa-medal"></i>
                <h2>Ranks</h2>
            </a>
        </div>

        <div class="status-box">
            <div class="status-header">
                <h3>Status Server</h3>
                <p><i class="fa-solid fa-server"></i> <?php echo $server_ip . ' : ' . $server_port; ?></p>
            </div>

            <?php if ($server_is_online): ?>
                <div class="status-indicator online">
                    <div class="dot"></div>
                    Online
                </div>
            <?php else: ?>
                <div class="status-indicator offline">
                    <div class="dot"></div>
                    Offline
                </div>
            <?php endif; ?>
        </div>
    </div>

</body>
</html>