document.addEventListener('DOMContentLoaded', () => { const btnProcessUpload = document.getElementById('btnProcessUpload'); const fileUpload = document.getElementById('fileUpload'); const extractionPreview = document.getElementById('extractionPreview'); const previewText = document.getElementById('previewText'); const configSection = document.getElementById('configSection'); const btnGenerate = document.getElementById('btnGenerate'); const genSpinner = document.getElementById('genSpinner'); const resultArea = document.getElementById('resultArea'); const paperPreview = document.getElementById('paperPreview'); const btnAddSection = document.getElementById('btnAddSection'); const patternLines = document.getElementById('patternLines'); const btnDownloadPdf = document.getElementById('btnDownloadPdf'); const step2Indicator = document.getElementById('step2Indicator'); const step3Indicator = document.getElementById('step3Indicator'); const fileNameDisplay = document.getElementById('fileNameDisplay'); const genIcon = document.getElementById('genIcon'); let extractedFullContent = ""; let lastGeneratedData = null; // File name display update fileUpload.addEventListener('change', (e) => { if (e.target.files.length > 0) { fileNameDisplay.textContent = e.target.files[0].name; fileNameDisplay.classList.add('text-primary', 'fw-bold'); } }); // 1. Handle File Upload and Extraction btnProcessUpload.addEventListener('click', async () => { if (!fileUpload.files[0]) { alert('Please select a file first.'); return; } const formData = new FormData(); formData.append('file', fileUpload.files[0]); btnProcessUpload.disabled = true; btnProcessUpload.innerHTML = 'Extracting...'; try { const response = await fetch('/upload', { method: 'POST', body: formData }); const data = await response.json(); if (data.error) { alert(data.error); } else { extractedFullContent = data.content_preview; previewText.textContent = data.content_preview + "..."; document.getElementById('extractedFileName').textContent = fileUpload.files[0].name; extractionPreview.classList.remove('d-none'); configSection.classList.remove('d-none'); step2Indicator.classList.add('step-active'); window.currentFileId = data.file_id; // Smooth scroll to config setTimeout(() => configSection.scrollIntoView({ behavior: 'smooth', block: 'start' }), 300); } } catch (err) { console.error(err); alert('Upload failed.'); } finally { btnProcessUpload.disabled = false; btnProcessUpload.innerHTML = 'Content Extracted '; btnProcessUpload.classList.replace('btn-primary-custom', 'btn-success'); } }); // 2. Add More Sections btnAddSection.addEventListener('click', () => { const newLine = document.createElement('div'); newLine.className = 'row g-2 mb-2 pattern-line'; newLine.innerHTML = `
`; patternLines.appendChild(newLine); }); // 3. Generate Questions btnGenerate.addEventListener('click', async () => { const blooms = Array.from(document.querySelectorAll('.form-check-input:checked')).map(c => c.value); const difficulty = document.getElementById('difficultyLevel').value; const sections = Array.from(document.querySelectorAll('.pattern-line')).map(line => { const inputs = line.querySelectorAll('input, select'); return { name: inputs[0].value, count: parseInt(inputs[1].value), marks: parseInt(inputs[2].value), type: inputs[3].value }; }); const payload = { content: extractedFullContent, file_id: window.currentFileId, blooms_levels: blooms, difficulty: difficulty, pattern: sections }; btnGenerate.disabled = true; genSpinner.classList.remove('d-none'); genIcon.classList.add('d-none'); try { const response = await fetch('/generate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); const data = await response.json(); if (data.error) { alert(data.error); } else { lastGeneratedData = data; renderPreview(data); resultArea.classList.remove('d-none'); step3Indicator.classList.add('step-active'); resultArea.scrollIntoView({ behavior: 'smooth' }); } } catch (err) { console.error(err); alert('Generation failed.'); } finally { btnGenerate.disabled = false; genSpinner.classList.add('d-none'); genIcon.classList.remove('d-none'); } }); // 4. Render Preview function renderPreview(data) { let html = `

${data.title || 'Examination Paper'}

`; data.sections.forEach(section => { html += `

${section.section_name}

`; if (section.instructions) html += `

${section.instructions}

`; section.questions.forEach((q, idx) => { html += `
Q${idx + 1}. ${q.question_text} [${q.marks} Marks]
Level: ${q.bloom_level}
`; if (q.type === 'MCQ' && q.options) { html += '
'; q.options.forEach((opt, i) => { html += `
${String.fromCharCode(65 + i)}. ${opt}
`; }); html += '
'; } html += '
'; }); }); paperPreview.innerHTML = html; } // 5. Download PDF btnDownloadPdf.addEventListener('click', async () => { if (!lastGeneratedData) return; // This is a simplified approach. In a real app, you would hit a /download endpoint. const response = await fetch('/download', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(lastGeneratedData) }); const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `ExamPaper_${Date.now()}.pdf`; document.body.appendChild(a); a.click(); a.remove(); }); });