<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Skyhook File Server</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            overflow: hidden;
        }

        header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
        }

        h1 {
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .breadcrumb {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 14px;
            opacity: 0.9;
            flex-wrap: wrap;
        }

        .breadcrumb a {
            color: white;
            text-decoration: none;
            transition: opacity 0.2s;
        }

        .breadcrumb a:hover {
            opacity: 0.8;
            text-decoration: underline;
        }

        .search-upload-container {
            padding: 20px 30px;
            background: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            align-items: center;
        }

        .search-box {
            flex: 1;
            min-width: 200px;
            position: relative;
        }

        .search-box input {
            width: 100%;
            padding: 12px 40px 12px 16px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.2s;
        }

        .search-box input:focus {
            outline: none;
            border-color: #667eea;
        }

        .search-icon {
            position: absolute;
            right: 12px;
            top: 50%;
            transform: translateY(-50%);
            color: #6c757d;
        }

        .upload-button {
            background: #667eea;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            white-space: nowrap;
        }

        .upload-button:hover {
            background: #5568d3;
            transform: translateY(-1px);
        }

        .drop-zone {
            margin: 30px;
            border: 3px dashed #dee2e6;
            border-radius: 12px;
            padding: 60px 20px;
            text-align: center;
            background: #f8f9fa;
            transition: all 0.3s;
            cursor: pointer;
        }

        .drop-zone.drag-over {
            border-color: #667eea;
            background: #e7f0ff;
        }

        .drop-zone-icon {
            font-size: 48px;
            margin-bottom: 15px;
        }

        .drop-zone-text {
            font-size: 16px;
            color: #6c757d;
            margin-bottom: 8px;
        }

        .drop-zone-hint {
            font-size: 13px;
            color: #adb5bd;
        }

        .file-table-container {
            padding: 30px;
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        thead {
            background: #f8f9fa;
        }

        th {
            text-align: left;
            padding: 12px 16px;
            font-weight: 600;
            font-size: 13px;
            color: #495057;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        tbody tr {
            border-bottom: 1px solid #f1f3f5;
            transition: background 0.2s;
        }

        tbody tr:hover {
            background: #f8f9fa;
        }

        td {
            padding: 16px;
            font-size: 14px;
            color: #495057;
        }

        .file-name {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .file-icon {
            font-size: 20px;
        }

        .file-link {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.2s;
        }

        .file-link:hover {
            color: #5568d3;
            text-decoration: underline;
        }

        .download-button {
            background: #28a745;
            color: white;
            border: none;
            padding: 6px 12px;
            border-radius: 6px;
            font-size: 12px;
            cursor: pointer;
            transition: all 0.2s;
        }

        .download-button:hover {
            background: #218838;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #6c757d;
        }

        .empty-state-icon {
            font-size: 64px;
            margin-bottom: 20px;
            opacity: 0.3;
        }

        .upload-progress {
            margin: 20px 30px;
            padding: 20px;
            background: #e7f0ff;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            display: none;
        }

        .upload-progress.active {
            display: block;
        }

        .progress-bar {
            width: 100%;
            height: 8px;
            background: #dee2e6;
            border-radius: 4px;
            overflow: hidden;
            margin-top: 10px;
        }

        .progress-fill {
            height: 100%;
            background: #667eea;
            transition: width 0.3s;
        }

        @media (max-width: 768px) {
            .container {
                border-radius: 0;
            }

            header {
                padding: 20px;
            }

            h1 {
                font-size: 22px;
            }

            .file-table-container {
                padding: 15px;
            }

            th,
            td {
                padding: 10px;
                font-size: 13px;
            }

            .drop-zone {
                margin: 15px;
                padding: 40px 15px;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <header>
            <h1>🚀 Skyhook File Server</h1>
            <div class="breadcrumb">
                <a href="/">Home</a>
                {% for crumb in breadcrumbs %}
                <span>›</span>
                <a href="/browse/{{ crumb.path }}">{{ crumb.name }}</a>
                {% endfor %}
            </div>
        </header>

        <div class="search-upload-container">
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="Search files and folders...">
                <span class="search-icon">🔍</span>
            </div>
            <button class="upload-button" onclick="document.getElementById('fileInput').click()">
                📤 Upload Files
            </button>
            <input type="file" id="fileInput" multiple style="display: none;">
        </div>

        <div class="drop-zone" id="dropZone">
            <div class="drop-zone-icon">📁</div>
            <div class="drop-zone-text">Drag and drop files here</div>
            <div class="drop-zone-hint">or click to browse</div>
        </div>

        <div class="upload-progress" id="uploadProgress">
            <div id="uploadStatus">Uploading files...</div>
            <div class="progress-bar">
                <div class="progress-fill" id="progressFill" style="width: 0%"></div>
            </div>
        </div>

        <div class="file-table-container">
            {% if items %}
            <table id="fileTable">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Size</th>
                        <th>Modified</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody id="fileTableBody">
                    {% for item in items %}
                    <tr data-name="{{ item.name|lower }}">
                        <td>
                            <div class="file-name">
                                <span class="file-icon">{{ '📁' if item.is_dir else '📄' }}</span>
                                {% if item.is_dir %}
                                <a href="/browse/{{ item.path }}" class="file-link">{{ item.name }}</a>
                                {% else %}
                                <span>{{ item.name }}</span>
                                {% endif %}
                            </div>
                        </td>
                        <td>{{ format_size(item.size) if not item.is_dir else '-' }}</td>
                        <td>{{ item.modified.strftime('%Y-%m-%d %H:%M') }}</td>
                        <td>
                            {% if not item.is_dir %}
                            <a href="/download/{{ item.path }}">
                                <button class="download-button">Download</button>
                            </a>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% else %}
            <div class="empty-state">
                <div class="empty-state-icon">📭</div>
                <div>This directory is empty</div>
            </div>
            {% endif %}
        </div>
    </div>

    <script>
        const currentPath = "{{ current_path }}";
        const dropZone = document.getElementById('dropZone');
        const fileInput = document.getElementById('fileInput');
        const uploadProgress = document.getElementById('uploadProgress');
        const progressFill = document.getElementById('progressFill');
        const uploadStatus = document.getElementById('uploadStatus');
        const searchInput = document.getElementById('searchInput');

        // Drag and drop handlers
        dropZone.addEventListener('click', () => fileInput.click());

        dropZone.addEventListener('dragover', (e) => {
            e.preventDefault();
            dropZone.classList.add('drag-over');
        });

        dropZone.addEventListener('dragleave', () => {
            dropZone.classList.remove('drag-over');
        });

        dropZone.addEventListener('drop', (e) => {
            e.preventDefault();
            dropZone.classList.remove('drag-over');
            handleFiles(e.dataTransfer.files);
        });

        fileInput.addEventListener('change', (e) => {
            handleFiles(e.target.files);
        });

        // Search functionality
        searchInput.addEventListener('input', (e) => {
            const searchTerm = e.target.value.toLowerCase();
            const rows = document.querySelectorAll('#fileTableBody tr');

            rows.forEach(row => {
                const name = row.getAttribute('data-name');
                if (name.includes(searchTerm)) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        });

        async function handleFiles(files) {
            if (files.length === 0) return;

            const formData = new FormData();
            Array.from(files).forEach(file => {
                formData.append('files', file);
            });
            formData.append('path', currentPath);

            uploadProgress.classList.add('active');
            progressFill.style.width = '0%';
            uploadStatus.textContent = `Uploading ${files.length} file(s)...`;

            try {
                // Simulate progress
                let progress = 0;
                const progressInterval = setInterval(() => {
                    progress += 5;
                    if (progress <= 90) {
                        progressFill.style.width = progress + '%';
                    }
                }, 100);

                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData,
                });

                clearInterval(progressInterval);
                progressFill.style.width = '100%';

                const result = await response.json();

                if (result.success > 0) {
                    uploadStatus.textContent = `✅ Successfully uploaded ${result.success} file(s)`;
                    setTimeout(() => {
                        location.reload();
                    }, 1000);
                } else {
                    uploadStatus.textContent = `❌ Upload failed`;
                }
            } catch (error) {
                uploadStatus.textContent = `❌ Upload error: ${error.message}`;
            } finally {
                setTimeout(() => {
                    uploadProgress.classList.remove('active');
                    fileInput.value = '';
                }, 2000);
            }
        }
    </script>
</body>

</html>