const POLL_INTERVAL = 5000; // 5 seconds document.addEventListener('DOMContentLoaded', function() { loadJobHistory(); document.getElementById('uploadForm').addEventListener('submit', handleFormSubmit); document.getElementById('bulkDownload').addEventListener('click', bulkDownload); // Add event listener for file input change document.querySelector('input[type="file"]').addEventListener('change', updateSelectedFiles); addSelectAllButton(); }); function handleFormSubmit(e) { e.preventDefault(); const formData = new FormData(this); const files = formData.getAll('images[]'); files.forEach(file => { const individualFormData = new FormData(); individualFormData.append('image', file); individualFormData.append('output_height', formData.get('output_height')); individualFormData.append('model', formData.get('model')); individualFormData.append('face_enhancement', formData.get('face_enhancement')); uploadFile(individualFormData); }); } function uploadFile(formData) { fetch('process.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.process_id) { addJobToList(data.process_id, formData.get('image').name); pollJobStatus(data.process_id); addToJobHistory(data.process_id, formData.get('image').name); } else if (data.error) { console.error('Error:', data.error); } }) .catch(error => { console.error('Error:', error); }); } function addJobToList(processId, fileName) { const jobList = document.getElementById('jobList'); const jobItem = document.createElement('div'); jobItem.id = `job-${processId}`; jobItem.innerHTML = ` `; jobList.appendChild(jobItem); if (!document.getElementById('selectAll')) { addSelectAllButton(); } document.getElementById('bulkDownload').style.display = 'block'; } function pollJobStatus(processId) { const pollInterval = setInterval(() => { fetch(`status.php?process_id=${processId}`) .then(response => response.json()) .then(data => { updateJobStatus(processId, data.status, data.progress); if (data.status === 'Completed') { clearInterval(pollInterval); updateJobHistory(processId, 'Completed'); } }) .catch(error => { console.error('Error:', error); }); }, POLL_INTERVAL); } function updateJobStatus(processId, status, progress) { const jobItem = document.getElementById(`job-${processId}`); if (jobItem) { const label = jobItem.querySelector('label'); label.textContent = `${label.textContent.split(' - ')[0]} - Status: ${status} - Progress: ${progress}%`; if (status === 'Completed') { jobItem.querySelector('button').style.display = 'inline-block'; } } } function downloadJob(processId) { let history = JSON.parse(localStorage.getItem('jobHistory')) || []; const job = history.find(job => job.processId === processId); if (job && (Date.now() - job.timestamp) <= (2 * 60 * 60 * 1000)) { window.location.href = `download.php?process_id=${processId}`; } else { alert('This job is no longer available for download.'); // Remove the job from history if it's too old history = history.filter(j => j.processId !== processId); localStorage.setItem('jobHistory', JSON.stringify(history)); updateJobHistoryDisplay(); } } function addToJobHistory(processId, fileName) { let history = JSON.parse(localStorage.getItem('jobHistory')) || []; history.push({ processId, fileName, status: 'Processing', timestamp: Date.now() }); localStorage.setItem('jobHistory', JSON.stringify(history)); updateJobHistoryDisplay(); } function updateJobHistory(processId, status) { let history = JSON.parse(localStorage.getItem('jobHistory')) || []; const index = history.findIndex(job => job.processId === processId); if (index !== -1) { history[index].status = status; localStorage.setItem('jobHistory', JSON.stringify(history)); updateJobHistoryDisplay(); } } function loadJobHistory() { updateJobHistoryDisplay(); } function updateJobHistoryDisplay() { cleanupOldJobs(); const historyContainer = document.getElementById('jobHistory'); const history = JSON.parse(localStorage.getItem('jobHistory')) || []; let historyHTML = '