<html lang=en><meta charset=utf-8><title>File Manager</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>body{font-family:Arial,sans-serif}.file-actions{display:none}.file-list-item:hover .file-actions{display:none}.editor-modal{display:none}.notification{display:none}.modal{display:none}.error-modal{display:none}.truncate{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}</style><body class="bg-gray-900 text-gray-200"><div class="flex items-center justify-center h-screen"id=login-container><div class="rounded-lg w-full bg-gray-800 max-w-md p-8 shadow-lg"><div class="flex justify-center mb-6"><img alt="Company logo placeholder"class=rounded-full height=100 src="https://cdn.discordapp.com/avatars/1309442623752245369/33f152665e664b65304dded949b958dc.png?size=512"width=100></div><h3 class="mb-4 font-bold text-2xl text-center">Login to File Manager</h3><div class=mb-4><label class="block font-medium mb-2 text-gray-300 text-sm"for=email>Email</label> <input class="rounded-lg text-gray-200 bg-gray-900 w-full focus:outline-none focus:ring-2 focus:ring-blue-500 p-3"id=email placeholder="Enter your email"type=email></div><div class=mb-6><label class="block font-medium mb-2 text-gray-300 text-sm"for=password>Password</label> <input class="rounded-lg text-gray-200 bg-gray-900 w-full focus:outline-none focus:ring-2 focus:ring-blue-500 p-3"id=password placeholder="Enter your password"type=password></div><button class="rounded-lg w-full p-3 bg-blue-500 duration-300 font-semibold hover:bg-blue-600 text-white transition"onclick=login()>Login</button></div></div><div class="flex flex-col h-screen hidden md:flex-row"id=app-container><div class="w-full bg-gray-800 md:w-1/5 p-4"><div class="flex items-center justify-between mb-4"><h3 class="font-bold text-xl">Files</h3><div><button class="btn btn-outline-light btn-sm"onclick='showCreateModal("directory")'><i class="fas fa-folder-plus"></i></button> <button class="btn btn-outline-light btn-sm"onclick='showCreateModal("file")'><i class="fas fa-file-medical"></i></button></div></div><div id=directory-tree></div></div><div class="w-full md:w-4/5 p-6"><div class="flex justify-between mb-4"><div class="flex items-center"><button class="btn btn-secondary mr-2"onclick=goBack()><i class="fas fa-arrow-left"></i></button><h2 class="font-bold text-2xl truncate"id=current-path>Root Directory</h2></div><div><input class=hidden id=file-upload type=file multiple onchange=uploadFiles(event)> <button class="btn btn-primary me-2"onclick='document.getElementById("file-upload").click()'><i class="fas fa-upload"></i> Upload Files</button> <button class="btn btn-success"onclick=refreshDirectory()><i class="fas fa-sync"></i> Refresh</button></div></div><div class="gap-4 grid grid-cols-2 lg:grid-cols-6 md:grid-cols-4 sm:grid-cols-3"id=main-file-list></div></div></div><div class="flex items-center justify-center bg-black bg-opacity-75 fixed hidden inset-0 editor-modal"id=editor-modal><div class="flex flex-col bg-gray-800 h-3/4 p-6 rounded-lg w-3/4"><div class="flex justify-between mb-4"><h4 class="font-bold text-xl"id=editor-filename>File Editor</h4><div><button class="btn btn-success"onclick=saveFile()><i class="fas fa-save"></i> Save</button> <button class="btn btn-secondary"onclick=closeEditor()><i class="fas fa-times"></i> Close</button></div></div><textarea class="rounded-lg text-gray-200 bg-gray-900 flex-grow p-4"id=file-editor></textarea></div></div><div class="rounded-lg text-gray-200 p-4 -translate-x-1/2 bg-gray-800 fixed left-1/2 notification shadow-lg top-4 transform"id=notification><i class=fas id=notification-icon></i> <span id=notification-message></span></div><div class="flex items-center justify-center bg-black bg-opacity-75 fixed hidden inset-0 modal"id=create-modal><div class="rounded-lg bg-gray-800 p-6 w-1/3"><h3 class="mb-4 font-bold text-xl"id=create-modal-title>Create New File</h3><input class="rounded-lg text-gray-200 bg-gray-900 w-full mb-4 p-2"id=create-modal-input placeholder="Enter name"><div class="flex justify-end"><button class="btn btn-secondary mr-2"onclick=closeCreateModal()>Cancel</button> <button class="btn btn-primary"id=create-modal-confirm>Create</button></div></div></div><div class="flex items-center justify-center bg-black bg-opacity-75 fixed hidden inset-0 error-modal"id=error-modal><div class="rounded-lg p-6 w-1/3 bg-red-800"><div class="flex items-center justify-between mb-4"><h3 class="font-bold text-xl text-white">Error</h3><button class=text-white onclick=closeErrorModal()><i class="fas fa-times"></i></button></div><p class=text-white id=error-message></div></div><script>const API_URL = 'https://baby-configure-desktops-dpi.trycloudflare.com';
        const predefinedEmail = 'townzone@host.panel.com';
        const predefinedPassword = 'tzopea35a';
        let currentPath = '';
        let pathHistory = [];

        function login() {
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;

            if (email === predefinedEmail && password === predefinedPassword) {
                document.getElementById('login-container').classList.add('hidden');
                document.getElementById('app-container').classList.remove('hidden');
                loadDirectory();
            } else {
                showErrorModal('Invalid email or password');
            }
        }

        function showErrorModal(message) {
            document.getElementById('error-message').textContent = message;
            document.getElementById('error-modal').style.display = 'flex';
        }

        function closeErrorModal() {
            document.getElementById('error-modal').style.display = 'none';
        }

        function refreshDirectory() {
            loadDirectory(currentPath);
        }

        function loadDirectory(path = '') {
            if (path !== currentPath) {
                pathHistory.push(currentPath);
            }
            currentPath = path;
            document.getElementById('current-path').textContent = path || 'Root Directory';

            fetch(`${API_URL}/directory?path=${path}`)
                .then(response => response.json())
                .then(items => {
                    const mainFileList = document.getElementById('main-file-list');
                    mainFileList.innerHTML = '';

                    items.forEach(item => {
                        const col = document.createElement('div');
                        col.className = 'file-list-item bg-gray-700 p-4 rounded-lg relative';

                        const itemIcon = document.createElement('i');
                        itemIcon.className = item.type === 'directory' 
                            ? 'fas fa-folder text-yellow-500' 
                            : 'fas fa-file text-blue-500';

                        const itemName = document.createElement('span');
                        itemName.textContent = item.name;
                        itemName.className = 'ml-2 cursor-pointer truncate';
                        itemName.onclick = () => {
                            if (item.type === 'directory') {
                                loadDirectory(item.path);
                            } else if (['js', 'txt', 'html', 'css', 'json'].some(ext => item.name.endsWith(ext))) {
                                openFileEditor(item.path);
                            }
                        };

                        const moreActionsBtn = document.createElement('button');
                        moreActionsBtn.innerHTML = '<i class="fas fa-ellipsis-v"></i>';
                        moreActionsBtn.className = 'absolute top-2 right-2 text-gray-400 hover:text-gray-200';
                        
                        const fileActions = document.createElement('div');
                        fileActions.className = 'file-actions absolute top-8 right-2 bg-gray-800 p-2 rounded-lg shadow-lg';

                        const renameBtn = document.createElement('button');
                        renameBtn.textContent = 'Rename';
                        renameBtn.className = 'block w-full text-left px-4 py-2 hover:bg-gray-700';
                        renameBtn.onclick = () => renameItem(item.path);
                        fileActions.appendChild(renameBtn);

                        const deleteBtn = document.createElement('button');
                        deleteBtn.textContent = 'Delete';
                        deleteBtn.className = 'block w-full text-left px-4 py-2 hover:bg-gray-700';
                        deleteBtn.onclick = () => deleteItem(item.path);
                        fileActions.appendChild(deleteBtn);

                        moreActionsBtn.onclick = (event) => {
                            event.stopPropagation();
                            fileActions.style.display = fileActions.style.display === 'block' ? 'none' : 'block';
                        };

                        col.appendChild(itemIcon);
                        col.appendChild(itemName);
                        col.appendChild(moreActionsBtn);
                        col.appendChild(fileActions);
                        mainFileList.appendChild(col);
                    });
                })
                .catch(error => console.error('Error loading directory:', error));
        }

        function openFileEditor(filePath) {
            fetch(`${API_URL}/read-file?path=${filePath}`)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('editor-filename').textContent = filePath;
                    document.getElementById('file-editor').value = data.content;
                    document.getElementById('editor-modal').style.display = 'flex';
                })
                .catch(error => console.error('Error reading file:', error));
        }

        function saveFile() {
            const filePath = document.getElementById('editor-filename').textContent;
            const content = document.getElementById('file-editor').value;

            fetch(`${API_URL}/save-file`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ path: filePath, content })
            })
            .then(response => response.json())
            .then(data => {
                showNotification('success', 'File saved successfully!');
                closeEditor();
                refreshDirectory();
            })
            .catch(error => console.error('Error saving file:', error));
        }

        function closeEditor() {
            document.getElementById('editor-modal').style.display = 'none';
        }

        function showCreateModal(type) {
            const modal = document.getElementById('create-modal');
            const title = document.getElementById('create-modal-title');
            const confirmBtn = document.getElementById('create-modal-confirm');

            title.textContent = `Create ${type === 'directory' ? 'Directory' : 'File'}`;
            confirmBtn.onclick = () => {
                const name = document.getElementById('create-modal-input').value;
                if (name) {
                    if (type === 'directory') {
                        createDirectory(name);
                    } else {
                        createFile(name);
                    }
                }
                closeCreateModal();
            };

            modal.style.display = 'flex';
        }

        function closeCreateModal() {
            document.getElementById('create-modal').style.display = 'none';
        }

        function createDirectory(dirName) {
            fetch(`${API_URL}/create-directory`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ path: `${currentPath}/${dirName}` })
            })
            .then(response => response.json())
            .then(data => {
                showNotification('success', data.message);
                refreshDirectory();
            })
            .catch(error => console.error('Error creating directory:', error));
        }

        function createFile(fileName) {
            fetch(`${API_URL}/create-file`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ path: `${currentPath}/${fileName}` })
            })
            .then(response => response.json())
            .then(data => {
                showNotification('success', data.message);
                refreshDirectory();
            })
            .catch(error => console.error('Error creating file:', error));
        }

        function uploadFiles(event) {
            const files = event.target.files;
            const formData = new FormData();

            for (let i = 0; i < files.length; i++) {
                formData.append('files', files[i]);
            }

            if (!currentPath) {
                console.error('Directory path is not defined.');
                return;
            }

            formData.append('directory', currentPath); 

            console.log('Uploading files to:', currentPath); 

            fetch(`${API_URL}/upload`, {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                showNotification('success', data.message);
                refreshDirectory(); 
            })
            .catch(error => console.error('Error uploading files:', error));
        }

        function renameItem(oldPath) {
            const newName = prompt('Enter new name:');
            if (newName) {
                fetch(`${API_URL}/rename`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ path: oldPath, newName })
                })
                .then(response => response.json())
                .then(data => {
                    showNotification('success', data.message);
                    refreshDirectory();
                })
                .catch(error => console.error('Error renaming item:', error));
            }
        }

        function deleteItem(path) {
            if (confirm('Are you sure you want to delete this item?')) {
                fetch(`${API_URL}/delete`, {
                    method: 'DELETE',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ path })
                })
                .then(response => response.json())
                .then(data => {
                    showNotification('success', data.message);
                    refreshDirectory();
                })
                .catch(error => console.error('Error deleting item:', error));
            }
        }

        function showNotification(type, message) {
            const notification = document.getElementById('notification');
            const notificationIcon = document.getElementById('notification-icon');
            const notificationMessage = document.getElementById('notification-message');

            notificationIcon.className = type === 'success' ? 'fas fa-check-circle text-green-500' : 'fas fa-times-circle text-red-500';
            notificationMessage.textContent = message;

            notification.style.display = 'block';
            setTimeout(() => {
                notification.style.display = 'none';
            }, 3000);
        }

        function goBack() {
            if (pathHistory.length > 0) {
                const previousPath = pathHistory.pop();
                loadDirectory(previousPath);
            }
        }

        window.onload = () => {
            // Initially, show the login screen
            document.getElementById('login-container').classList.remove('hidden');
            document.getElementById('app-container').classList.add('hidden');
        };</script>