`;
faqOutputEl.appendChild(item);
});
resultContainer.classList.remove('hidden');
errorMessageEl.classList.add('hidden');
};
// Event listener for accordion functionality
faqOutputEl.addEventListener('click', (e) => {
const header = e.target.closest('.accordion-header');
if (header) {
const item = header.parentElement;
const content = header.nextElementSibling;
if (item.classList.contains('active')) {
item.classList.remove('active');
content.style.maxHeight = '0px';
} else {
// Close other items if you want only one open at a time
// faqOutputEl.querySelectorAll('.accordion-item.active').forEach(activeItem => {
// activeItem.classList.remove('active');
// activeItem.querySelector('.accordion-content').style.maxHeight = '0px';
// });
item.classList.add('active');
content.style.maxHeight = content.scrollHeight + 'px';
}
}
});
const displayError = (message) => {
errorMessageEl.textContent = message;
errorMessageEl.classList.remove('hidden');
resultContainer.classList.add('hidden');
loadingIndicator.classList.add('hidden');
generateButton.disabled = false;
generateButton.classList.remove('opacity-50', 'cursor-not-allowed');
};
const downloadPDF = () => {
if (typeof window.jspdf === 'undefined') {
displayError("Could not generate PDF. The 'jspdf' library is missing.");
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ unit: 'pt', format: 'a4' });
const topic = faqTopicEl.value;
const margin = 40;
let y = margin;
doc.setFont("helvetica", "bold");
doc.setFontSize(20);
doc.text("Frequently Asked Questions", doc.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 20;
doc.setFont("helvetica", "normal");
doc.setFontSize(12);
doc.setTextColor(100);
doc.text(`Topic: ${topic}`, doc.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 30;
doc.setDrawColor(220, 220, 220);
doc.line(margin, y, doc.internal.pageSize.getWidth() - margin, y);
y += 20;
generatedFaqs.forEach((faq, index) => {
if (y > doc.internal.pageSize.getHeight() - margin) {
doc.addPage();
y = margin;
}
doc.setFont("helvetica", "bold");
doc.setFontSize(11);
doc.setTextColor(40);
const qLines = doc.splitTextToSize(`Q: ${faq.question}`, doc.internal.pageSize.getWidth() - margin * 2);
doc.text(qLines, margin, y);
y += qLines.length * 12 + 5;
doc.setFont("helvetica", "normal");
doc.setFontSize(11);
doc.setTextColor(80);
const aLines = doc.splitTextToSize(faq.answer, doc.internal.pageSize.getWidth() - margin * 2);
doc.text(aLines, margin, y);
y += aLines.length * 12 + 25;
});
doc.save("generated-faqs.pdf");
};
generateButton.addEventListener('click', generateFaqs);
pdfDownloadButton.addEventListener('click', downloadPDF);
});
