<html lang=en><meta charset=viewport content="width=device-width, initial-scale=1.0"><title>Dashboard</title><script src=https://cdn.tailwindcss.com></script><link href=https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css rel=stylesheet><style>.hidden{display:none}.no-select{user-select:none}.no-scroll{overflow:hidden}.line-numbers{counter-reset:line}.line-numbers textarea{padding-left:2em}.line-numbers textarea::before{counter-increment:line;content:counter(line);display:inline-block;width:2em;margin-left:-2em;text-align:right;color:#888}.alert{position:fixed;bottom:20px;left:20px;background-color:#323232;color:#fff;padding:10px 20px;border-radius:5px;display:flex;align-items:center;opacity:0;transition:opacity .5s,transform .5s;transform:translateY(20px)}.alert.show{opacity:1;transform:translateY(0)}.alert.success{background-color:#4caf50}.alert.error{background-color:#f44336}.alert i{margin-right:10px}</style><body class="bg-gray-900 h-screen no-scroll no-select text-white"><div class="flex h-full"><div class="p-4 bg-gray-800 hidden md:block w-1/5"id=sidebar><div class=mb-6><ul class=space-y-4><li class="flex items-center space-x-2"><i class="fas fa-tachometer-alt"></i> <button class=hover:text-gray-400 id=dashboardBtn>Dashboard</button><li class="flex items-center space-x-2"><i class="fas fa-folder"></i> <a class=hover:text-gray-400 href=files.html>Files</a><li class="flex items-center space-x-2"><i class="fas fa-cog"></i> <a class=hover:text-gray-400 href=settings.html>Settings</a><li class="flex items-center space-x-2"><i class="fas fa-user"></i> <button class=hover:text-gray-400 id=profileBtn>Profile</button></ul></div><div class="hidden mt-4"id=profileOptions><div class="rounded bg-gray-700 bg-opacity-50 p-2"><p class=text-sm>Email: townzone@host.panel.com</p><button class="rounded text-white bg-red-600 hover:bg-red-700 mt-2 px-2 py-1"id=logoutBtn>Logout</button></div></div></div><div class="p-4 flex-1 overflow-hidden"><div class="flex items-center justify-between mb-6"><div class="flex items-center space-x-2"><span class="text-xl font-bold">TownZone Hosting</span> <i class="fas fa-server text-xl"></i></div><div class="rounded text-white py-2 bg-gray-800 flex items-center px-4"id=serverStatus><div class="bg-orange-500 h-3 mr-2 rounded-full w-3"></div><span>WAITING</span></div></div><div id=dashboardContent><div class="mb-6 gap-4 grid grid-cols-1 md:grid-cols-3"><div class="rounded bg-gray-800 p-4"><h3 class="font-semibold text-lg">CPU Usage</h3><p class=text-sm id=cpuUsage>0% / ∞<div class="rounded h-2 bg-gray-700 mt-2"><div class="rounded h-2 bg-blue-600"id=cpuUsageBar style=width:0%></div></div></div><div class="rounded bg-gray-800 p-4"><h3 class="font-semibold text-lg">Memory Usage</h3><p class=text-sm id=memoryUsage>0 MB / ∞<div class="rounded h-2 bg-gray-700 mt-2"><div class="rounded h-2 bg-blue-600"id=memoryUsageBar style=width:0%></div></div></div><div class="rounded bg-gray-800 p-4"><h3 class="font-semibold text-lg">Disk Usage</h3><p class=text-sm id=diskUsage>0 MB / ∞<div class="rounded h-2 bg-gray-700 mt-2"><div class="rounded h-2 bg-blue-600"id=diskUsageBar style=width:0%></div></div></div></div><div class="mb-6 gap-4 grid grid-cols-2 md:grid-cols-4"><button class="rounded text-white py-2 bg-blue-600 hover:bg-blue-700"id=startBtn>Start</button> <button class="rounded text-white py-2 bg-green-600 hover:bg-green-700"id=restartBtn>Restart</button> <button class="rounded text-white py-2 bg-yellow-600 hover:bg-yellow-700"id=stopBtn>Stop</button> <button class="rounded text-white py-2 bg-red-600 hover:bg-red-700"id=killBtn>Kill</button></div><div class="rounded bg-gray-800 p-4 h-96 no-select overflow-y-auto"id=console><pre class=text-sm id=consoleOutput>
[INFO]: Generating keypair
[INFO]: Starting server on 0.0.0.0:54828
[INFO]: Using epoll channel type
[INFO]: Time elapsed: 189 ms
[INFO]: Time elapsed: 65 ms
[INFO]: Time elapsed: 45 ms
[INFO]: Starting remote control listener
[INFO]: Thread RCON Listener started
[INFO]: RCON running on 0.0.0.0:64683
[INFO]: Running delayed init tasks
                    </pre></div></div></div></div><div class=alert id=alert><i class=fas></i> <span id=alertMessage></span></div><script>function showAlert(message, type) {
            const alertBox = document.getElementById('alert');
            const alertMessage = document.getElementById('alertMessage');
            const alertIcon = alertBox.querySelector('i');

            alertMessage.textContent = message;
            alertBox.className = `alert show ${type}`;
            alertIcon.className = type === 'success' ? 'fas fa-check-circle' : 'fas fa-times-circle';

            setTimeout(() => {
                alertBox.classList.remove('show');
            }, 3000);
        }

        document.getElementById('dashboardBtn').addEventListener('click', function() {
            document.getElementById('dashboardContent').classList.remove('hidden');
            document.getElementById('filesContent').classList.add('hidden');
            document.getElementById('fileEditorContent').classList.add('hidden');
            document.getElementById('settingsContent').classList.add('hidden');
        });

        document.getElementById('profileBtn').addEventListener('click', function() {
            const profileOptions = document.getElementById('profileOptions');
            profileOptions.classList.toggle('hidden');
        });

        document.getElementById('logoutBtn').addEventListener('click', function() {
            localStorage.removeItem('token');
            window.location.href = 'login.html';
        });

        async function fetchServerStatus() {
            try {
                const [statusResponse, diskResponse] = await Promise.all([
                    fetch('https://institution-hills-generation-smoking.trycloudflare.com/status/townzone'),
                    fetch('https://institution-hills-generation-smoking.trycloudflare.com/directory-size')
                ]);

                const data = await statusResponse.json();
                const diskData = await diskResponse.json();

                document.getElementById('cpuUsage').textContent = `${data.cpu}% / ∞`;
                document.getElementById('cpuUsageBar').style.width = `${Math.min(data.cpu, 100)}%`;

                document.getElementById('memoryUsage').textContent = `${data.memory} MB / ∞`;
                document.getElementById('memoryUsageBar').style.width = `${Math.min(data.memory / 500 * 100, 100)}%`;

                const diskSizeMB = diskData.size / (1024 * 1024);
                document.getElementById('diskUsage').textContent = `${diskSizeMB.toFixed(2)} MB / ∞`;
                document.getElementById('diskUsageBar').style.width = `${Math.min(diskSizeMB / 500 * 100, 100)}%`;

                const statusElement = document.getElementById('serverStatus');
                if (data.status === 'online') {
                    statusElement.innerHTML = '<div class="bg-green-500 h-3 w-3 rounded-full mr-2"></div><span>RUNNING</span>';
                } else {
                    statusElement.innerHTML = '<div class="bg-red-500 h-3 w-3 rounded-full mr-2"></div><span>OFFLINE</span>';
                }
            } catch (error) {
                console.error('Error fetching server status:', error);
            }
        }

        fetchServerStatus();

        document.getElementById('startBtn').addEventListener('click', async function() {
            try {
                const response = await fetch('https://institution-hills-generation-smoking.trycloudflare.com/start/townzone', { method: 'POST' });
                if (response.ok) {
                    showAlert('Server started successfully!', 'success');
                    fetchServerStatus();
                } else {
                    showAlert('Failed to start server.', 'error');
                }
            } catch (error) {
                console.error('Error starting server:', error);
                showAlert('Error starting server.', 'error');
            }
        });

        document.getElementById('restartBtn').addEventListener('click', async function() {
            try {
                const response = await fetch('https://institution-hills-generation-smoking.trycloudflare.com/restart/townzone', { method: 'POST' });
                if (response.ok) {
                    showAlert('Server restarted successfully!', 'success');
                    fetchServerStatus();
                } else {
                    showAlert('Failed to restart server.', 'error');
                }
            } catch (error) {
                console.error('Error restarting server:', error);
                showAlert('Error restarting server.', 'error');
            }
        });

        document.getElementById('stopBtn').addEventListener('click', async function() {
            try {
                const response = await fetch('https://institution-hills-generation-smoking.trycloudflare.com/stop/townzone', { method: 'POST' });
                if (response.ok) {
                    showAlert('Server stopped successfully!', 'success');
                    fetchServerStatus();
                } else {
                    showAlert('Failed to stop server.', 'error');
                }
            } catch (error) {
                console.error('Error stopping server:', error);
                showAlert('Error stopping server.', 'error');
            }
        });

        document.getElementById('killBtn').addEventListener('click', async function() {
            try {
                const response = await fetch('https://institution-hills-generation-smoking.trycloudflare.com/stop/townzone', { method: 'POST' });
                if (response.ok) {
                    showAlert('Server killed successfully!', 'success');
                    fetchServerStatus();
                } else {
                    showAlert('Failed to kill server.', 'error');
                }
            } catch (error) {
                console.error('Error killing server:', error);
                showAlert('Error killing server.', 'error');
            }
        });

        async function fetchPM2Logs() {
            try {
                const statusResponse = await fetch('https://institution-hills-generation-smoking.trycloudflare.com/status/townzone');
                const statusData = await statusResponse.json();

                if (statusData.status === 'online') {
                    const logsResponse = await fetch('https://institution-hills-generation-smoking.trycloudflare.com/logs/townzone');
                    const logsData = await logsResponse.json();
                    const consoleOutput = document.getElementById('consoleOutput');
                    consoleOutput.textContent = logsData.logs.join('\n');
                    consoleOutput.scrollTop = consoleOutput.scrollHeight; 
                } else {
                    const consoleOutput = document.getElementById('consoleOutput');
                    consoleOutput.textContent = 'Server is offline';
                }
            } catch (error) {
                console.error('Error fetching PM2 logs or server status:', error);
                const consoleOutput = document.getElementById('consoleOutput');
                consoleOutput.textContent = 'Server is offline';
            }
        }

        setInterval(fetchServerStatus, 1000);
        setInterval(fetchPM2Logs, 1000);

        async function loadBackgroundImage() {
            try {
                const response = await fetch('https://institution-hills-generation-smoking.trycloudflare.com/background');
                const data = await response.json();
                if (data.url) {
                    document.body.style.backgroundImage = `url(${data.url})`;
                    document.body.style.backgroundSize = 'cover';
                    document.body.style.backgroundRepeat = 'no-repeat';
                    document.getElementById('sidebar').style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
                    document.getElementById('console').style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
                }
            } catch (error) {
                console.error('Error loading background image:', error);
            }
        }

        loadBackgroundImage();

        document.addEventListener('DOMContentLoaded', function() {
            const token = localStorage.getItem('token');
            if (!token) {
                window.location.href = 'login.html';
            }
        });</script>