document.addEventListener('DOMContentLoaded', function() { const uploadArea = document.getElementById('uploadArea'); const fileInput = document.getElementById('fileInput'); const filePreview = document.getElementById('filePreview'); const fileList = document.getElementById('fileList'); const generateBtn = document.getElementById('generateBtn'); const slideCountSelect = document.getElementById('slideCount'); const customSlideCount = document.getElementById('customSlideCount'); const form = document.getElementById('slideGeneratorForm'); const progressContainer = document.getElementById('progressContainer'); const chooseBtn = uploadArea.querySelector('button.btn'); const initialProgressHTML = progressContainer ? progressContainer.innerHTML : ''; const bgInput = document.getElementById('bgImage'); const bgPreviewWrap = document.getElementById('bgPreviewWrap'); const bgPreview = document.getElementById('bgPreview'); // Content source elements const contentSourceOptions = document.querySelectorAll('input[name="content_source"]'); const documentSelection = document.getElementById('documentSelection'); const subjectSelection = document.getElementById('subjectSelection'); const documentCheckboxes = document.querySelectorAll('input[name="selected_documents"]'); const subjectRadios = document.querySelectorAll('input[name="selected_subject"]'); let selectedFiles = []; // Handle content source selection contentSourceOptions.forEach(option => { option.addEventListener('change', function() { const selectedSource = this.value; // Hide all content sections uploadArea.style.display = 'none'; if (documentSelection) documentSelection.style.display = 'none'; if (subjectSelection) subjectSelection.style.display = 'none'; // Show the relevant section if (selectedSource === 'upload') { uploadArea.style.display = 'block'; // Don't hide file preview or clear files if they exist // Let the user keep their uploaded files when switching modes } else if (selectedSource === 'existing_documents' && documentSelection) { documentSelection.style.display = 'block'; } else if (selectedSource === 'subject' && subjectSelection) { subjectSelection.style.display = 'block'; } // Always update the button state after changing source setTimeout(updateGenerateButton, 100); }); }); // Handle document selection if (documentCheckboxes) { documentCheckboxes.forEach(checkbox => { checkbox.addEventListener('change', updateGenerateButton); }); } // Handle subject selection if (subjectRadios) { subjectRadios.forEach(radio => { radio.addEventListener('change', function() { updateGenerateButton(); // Auto-update title when subject is selected const titleInput = document.getElementById('presentationTitle'); if (titleInput && !titleInput.value.trim()) { const selectedLabel = document.querySelector(`label[for="${this.id}"]`); if (selectedLabel) { const subjectTitle = selectedLabel.querySelector('.subject-title'); if (subjectTitle) { titleInput.value = subjectTitle.textContent.trim() + ' - Presentation'; } } } }); }); } // Handle file input change fileInput.addEventListener('change', function(e) { console.log('File input changed', e.target.files); handleFiles(e.target.files); }); // Handle drag and drop uploadArea.addEventListener('dragover', function(e) { e.preventDefault(); uploadArea.classList.add('dragover'); }); uploadArea.addEventListener('dragleave', function(e) { e.preventDefault(); uploadArea.classList.remove('dragover'); }); uploadArea.addEventListener('drop', function(e) { e.preventDefault(); uploadArea.classList.remove('dragover'); handleFiles(e.dataTransfer.files); }); // Improve UX: clicking the area or button opens the file dialog uploadArea.addEventListener('click', function(e) { console.log('Upload area clicked', e.target); // Avoid double-trigger when the hidden input is clicked if (e.target !== fileInput) { console.log('Triggering file input click'); fileInput.click(); } }); if (chooseBtn) { chooseBtn.addEventListener('click', function(e) { console.log('Choose button clicked'); e.preventDefault(); e.stopPropagation(); fileInput.click(); }); } // Handle slide count selection slideCountSelect.addEventListener('change', function() { if (this.value === 'custom') { customSlideCount.style.display = 'block'; } else { customSlideCount.style.display = 'none'; } }); // Handle files function handleFiles(files) { console.log('HandleFiles called with:', files); const allowedTypes = ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation']; Array.from(files).forEach(file => { console.log('Processing file:', file.name, file.type); if (allowedTypes.includes(file.type) || file.name.toLowerCase().match(/\.(pdf|doc|docx|txt|ppt|pptx)$/)) { if (!selectedFiles.find(f => f.name === file.name && f.size === file.size)) { selectedFiles.push(file); console.log('File added:', file.name); } else { console.log('File already exists:', file.name); } } else { console.log('File type not supported:', file.name, file.type); alert(`File type not supported: ${file.name}`); } }); console.log('Selected files:', selectedFiles); updateFilePreview(); updateGenerateButton(); } // Background image preview if (bgInput) { bgInput.addEventListener('change', function(e) { const file = e.target.files && e.target.files[0]; if (!file) { if (bgPreviewWrap) bgPreviewWrap.style.display = 'none'; return; } const reader = new FileReader(); reader.onload = function(evt) { if (bgPreview) bgPreview.src = evt.target.result; if (bgPreviewWrap) bgPreviewWrap.style.display = 'block'; }; reader.readAsDataURL(file); }); } // Update file preview function updateFilePreview() { console.log('updateFilePreview called', { filePreview: filePreview, fileList: fileList, selectedFilesLength: selectedFiles.length }); if (selectedFiles.length === 0) { if (filePreview) filePreview.classList.remove('show'); return; } if (!fileList) { console.error('fileList element not found'); return; } fileList.innerHTML = ''; selectedFiles.forEach((file, index) => { console.log('Adding file to preview:', file.name); const fileItem = document.createElement('div'); fileItem.className = 'file-item'; const fileExtension = file.name.split('.').pop().toLowerCase(); let iconClass = 'fas fa-file'; switch(fileExtension) { case 'pdf': iconClass = 'fas fa-file-pdf'; break; case 'doc': case 'docx': iconClass = 'fas fa-file-word'; break; case 'ppt': case 'pptx': iconClass = 'fas fa-file-powerpoint'; break; case 'txt': iconClass = 'fas fa-file-alt'; break; } fileItem.innerHTML = `
Your PowerPoint presentation has been generated successfully.
`; // Create download button const downloadBtn = document.createElement('a'); downloadBtn.href = data.download_url; downloadBtn.download = data.file_name; downloadBtn.className = 'btn btn-success btn-lg px-5 py-3'; downloadBtn.innerHTML = 'Download PowerPoint'; downloadBtn.style.cssText = 'text-decoration: none; font-weight: 600; box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);'; // Add hover effect downloadBtn.addEventListener('mouseenter', function() { this.style.transform = 'translateY(-2px)'; this.style.boxShadow = '0 6px 20px rgba(40, 167, 69, 0.4)'; }); downloadBtn.addEventListener('mouseleave', function() { this.style.transform = 'translateY(0)'; this.style.boxShadow = '0 4px 15px rgba(40, 167, 69, 0.3)'; }); downloadSection.appendChild(downloadBtn); // Create new session button const newSessionBtn = document.createElement('button'); newSessionBtn.className = 'btn btn-outline-primary btn-lg ms-3 px-4 py-3'; newSessionBtn.innerHTML = 'Generate Another'; newSessionBtn.style.cssText = 'font-weight: 600;'; newSessionBtn.addEventListener('click', function() { // Reset UI without full page reload selectedFiles = []; if (fileList) fileList.innerHTML = ''; if (filePreview) filePreview.classList.remove('show'); if (progressContainer) { progressContainer.classList.remove('show'); progressContainer.innerHTML = initialProgressHTML; } if (form) form.reset(); updateGenerateButton(); // Re-bind references after restoring HTML const newProgressBar = document.getElementById('progressBar'); const newProgressText = document.getElementById('progressText'); if (newProgressBar) newProgressBar.style.width = '0%'; if (newProgressText) newProgressText.textContent = 'Initializing...'; }); downloadSection.appendChild(newSessionBtn); // Replace progress container content progressContainer.innerHTML = ''; progressContainer.appendChild(downloadSection); generateBtn.disabled = false; }, 1000); } else { progressBar.style.width = '0%'; progressText.textContent = 'Error occurred'; generateBtn.disabled = false; // Show error message const errorMessage = document.createElement('div'); errorMessage.className = 'alert alert-danger mt-3'; errorMessage.innerHTML = `Error: ${data.error}`; progressContainer.appendChild(errorMessage); } }) .catch(error => { console.error('Error:', error); progressBar.style.width = '0%'; progressText.textContent = 'Error occurred'; generateBtn.disabled = false; // Show error message const errorMessage = document.createElement('div'); errorMessage.className = 'alert alert-danger mt-3'; errorMessage.innerHTML = 'An error occurred while generating slides.'; progressContainer.appendChild(errorMessage); }); } // Initialize the page state function initializePage() { console.log('Initializing page...', { uploadArea: uploadArea, filePreview: filePreview, fileList: fileList, generateBtn: generateBtn }); // Set initial visibility based on default selection const selectedSource = document.querySelector('input[name="content_source"]:checked'); if (selectedSource) { const sourceValue = selectedSource.value; console.log('Default content source:', sourceValue); // Hide all sections first if (documentSelection) documentSelection.style.display = 'none'; if (subjectSelection) subjectSelection.style.display = 'none'; uploadArea.style.display = 'none'; // Show the correct section if (sourceValue === 'upload') { uploadArea.style.display = 'block'; console.log('Upload area should be visible'); } else if (sourceValue === 'existing_documents' && documentSelection) { documentSelection.style.display = 'block'; } else if (sourceValue === 'subject' && subjectSelection) { subjectSelection.style.display = 'block'; } } // Initialize button state updateGenerateButton(); } // Initialize the page state on load initializePage(); // Test function for debugging window.testFilePreview = function() { console.log('Testing file preview...'); if (selectedFiles.length === 0) { // Add a dummy file for testing selectedFiles.push({ name: 'test.pdf', size: 1234567, type: 'application/pdf' }); } updateFilePreview(); }; // Removed simulated progress to avoid conflicting UI updates during real generation });