<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>All Jobs - Podcast Pile</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f5f5f5;
            color: #333;
            line-height: 1.6;
        }

        .container {
            max-width: 1600px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            background: #2c3e50;
            color: white;
            padding: 20px 0;
            margin-bottom: 30px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        header h1 {
            font-size: 28px;
            font-weight: 600;
        }

        .nav-link {
            color: #3498db;
            text-decoration: none;
            display: inline-block;
            margin-bottom: 20px;
        }

        .nav-link:hover {
            text-decoration: underline;
        }

        .controls {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            display: flex;
            gap: 15px;
            align-items: center;
        }

        .controls select {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        .jobs-container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

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

        .jobs-table th {
            background: #ecf0f1;
            padding: 12px;
            text-align: left;
            font-weight: 600;
            color: #2c3e50;
            border-bottom: 2px solid #bdc3c7;
            position: sticky;
            top: 0;
        }

        .jobs-table td {
            padding: 12px;
            border-bottom: 1px solid #ecf0f1;
        }

        .jobs-table tr:hover {
            background: #f8f9fa;
        }

        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
        }

        .status-pending { background: #fff3cd; color: #856404; }
        .status-assigned { background: #d1ecf1; color: #0c5460; }
        .status-processing { background: #cce5ff; color: #004085; }
        .status-completed { background: #d4edda; color: #155724; }
        .status-failed { background: #f8d7da; color: #721c24; }
        .status-expired { background: #e2e3e5; color: #383d41; }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
            margin-top: 20px;
            padding: 20px;
        }

        .pagination button {
            padding: 8px 16px;
            border: 1px solid #ddd;
            background: white;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .pagination button:hover:not(:disabled) {
            background: #f0f0f0;
        }

        .pagination button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        .pagination .page-info {
            font-size: 14px;
            color: #666;
        }

        .job-link {
            color: #3498db;
            text-decoration: none;
        }

        .job-link:hover {
            text-decoration: underline;
        }

        .url-cell {
            max-width: 500px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .json-indicator {
            background: #3498db;
            color: white;
            padding: 2px 8px;
            border-radius: 8px;
            font-size: 11px;
            margin-left: 5px;
        }

        .action-button {
            background: #f39c12;
            color: white;
            border: none;
            padding: 4px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            font-weight: 600;
        }

        .action-button:hover {
            background: #e67e22;
        }

        .action-button:disabled {
            background: #95a5a6;
            cursor: not-allowed;
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <h1>All Jobs</h1>
        </div>
    </header>

    <div class="container">
        <a href="/" class="nav-link">&larr; Back to Dashboard</a>

        <div class="controls">
            <label for="status-filter">Filter by status:</label>
            <select id="status-filter">
                <option value="">All Statuses</option>
                <option value="pending">Pending</option>
                <option value="assigned">Assigned</option>
                <option value="processing">Processing</option>
                <option value="completed">Completed</option>
                <option value="failed">Failed</option>
                <option value="expired">Expired</option>
            </select>
        </div>

        <div class="jobs-container">
            <table class="jobs-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Episode URL</th>
                        <th>Status</th>
                        <th>Lang</th>
                        <th>Worker</th>
                        <th>IP</th>
                        <th>Created</th>
                        <th>Completed</th>
                    </tr>
                </thead>
                <tbody id="jobs-tbody">
                    <tr>
                        <td colspan="8" class="loading">Loading jobs...</td>
                    </tr>
                </tbody>
            </table>

            <div class="pagination">
                <button id="first-page">&laquo; First</button>
                <button id="prev-page">&lsaquo; Prev</button>
                <span class="page-info" id="page-info">Page 1 of 1</span>
                <button id="next-page">Next &rsaquo;</button>
                <button id="last-page">Last &raquo;</button>
            </div>
        </div>
    </div>

    <script>
        let currentPage = {{ page }};
        let currentFilter = "{{ status_filter or '' }}";
        let totalPages = 1;

        async function loadJobs() {
            const params = new URLSearchParams({
                page: currentPage,
                per_page: 50
            });

            if (currentFilter) {
                params.append('status_filter', currentFilter);
            }

            try {
                const response = await fetch(`/api/jobs/paginated?${params}`);
                const data = await response.json();

                renderJobs(data.jobs);
                updatePagination(data.pagination);
            } catch (error) {
                document.getElementById('jobs-tbody').innerHTML =
                    '<tr><td colspan="7" class="loading">Error loading jobs</td></tr>';
            }
        }

        function renderJobs(jobs) {
            const tbody = document.getElementById('jobs-tbody');

            if (jobs.length === 0) {
                tbody.innerHTML = '<tr><td colspan="7" class="loading">No jobs found</td></tr>';
                return;
            }

            tbody.innerHTML = jobs.map(job => `
                <tr>
                    <td>
                        <a href="/jobs/${job.id}" class="job-link">#${job.id}</a>
                        ${job.has_json ? '<span class="json-indicator">JSON</span>' : ''}
                    </td>
                    <td class="url-cell" title="${job.episode_url}">${job.episode_url}</td>
                    <td><span class="status-badge status-${job.status}">${job.status}</span></td>
                    <td>${job.language || '-'}</td>
                    <td>${job.worker_id || '-'}</td>
                    <td>${job.worker_ip || '-'}</td>
                    <td>${job.created_at ? new Date(job.created_at).toLocaleString() : '-'}</td>
                    <td>${job.completed_at ? new Date(job.completed_at).toLocaleString() : '-'}</td>
                </tr>
            `).join('');
        }

        function updatePagination(pagination) {
            totalPages = pagination.total_pages;
            document.getElementById('page-info').textContent =
                `Page ${pagination.page} of ${totalPages} (${pagination.total} total jobs)`;

            document.getElementById('first-page').disabled = !pagination.has_prev;
            document.getElementById('prev-page').disabled = !pagination.has_prev;
            document.getElementById('next-page').disabled = !pagination.has_next;
            document.getElementById('last-page').disabled = !pagination.has_next;
        }

        document.getElementById('status-filter').addEventListener('change', (e) => {
            currentFilter = e.target.value;
            currentPage = 1;
            loadJobs();
        });

        document.getElementById('first-page').addEventListener('click', () => {
            currentPage = 1;
            loadJobs();
        });

        document.getElementById('prev-page').addEventListener('click', () => {
            if (currentPage > 1) {
                currentPage--;
                loadJobs();
            }
        });

        document.getElementById('next-page').addEventListener('click', () => {
            if (currentPage < totalPages) {
                currentPage++;
                loadJobs();
            }
        });

        document.getElementById('last-page').addEventListener('click', () => {
            currentPage = totalPages;
            loadJobs();
        });

        // Load initial data
        loadJobs();
    </script>
</body>
</html>
