`;
contentList.innerHTML += categoryHtml;
}
}
// --- Tab Switching ---
function faqo_showTab(targetId, element) {
tabButtons.forEach(btn => btn.classList.remove('faqo-active'));
document.querySelectorAll('.faqo-tab-content').forEach(content => content.classList.remove('active'));
if (element) {
element.classList.add('faqo-active');
}
document.getElementById(targetId).classList.add('active');
if (targetId === 'review') {
// Render review using current filter setting
const currentFilter = document.getElementById('category-filter') ? document.getElementById('category-filter').value : 'all';
faqo_renderReview(currentFilter);
}
}
window.faqo_showTab = faqo_showTab;
// --- PDF Export ---
function faqo_downloadPDF() {
faqo_collectData();
// CRITICAL FIX: Check for jsPDF.autoTable existence
if (typeof jspdf === 'undefined' || (typeof jspdf.plugin === 'undefined' || typeof jspdf.plugin.autotable === 'undefined')) {
alert('Error: PDF generation libraries failed to load completely. Please try again.');
return;
}
const filterCategory = document.getElementById('category-filter').value;
const filteredItems = FAQO_STATE.items.filter(c => filterCategory === 'all' || c.category === filterCategory);
if (filteredItems.length === 0) {
alert('No Q/A pairs match the current filter selection for export.');
return;
}
const { jsPDF } = jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
doc.setFont('sans-serif', 'normal');
const margin = 40;
const pageWidth = doc.internal.pageSize.getWidth();
let yPos = margin;
// --- Header ---
doc.setFontSize(18);
doc.setFont('sans-serif', 'bold');
doc.setTextColor('#1e40af'); // Blue-800
doc.text(`FAQ Content Export: ${filterCategory === 'all' ? 'All Categories' : filterCategory}`, pageWidth / 2, yPos, { align: 'center' });
yPos += 30;
// --- Grouped Content ---
const groupedItems = filteredItems.reduce((acc, item) => {
acc[item.category] = acc[item.category] || [];
acc[item.category].push(item);
return acc;
}, {});
for (const category in groupedItems) {
if (yPos > doc.internal.pageSize.getHeight() - 80) { doc.addPage(); yPos = margin; }
// Category Header
doc.setFontSize(14);
doc.setFont('sans-serif', 'bold');
doc.setTextColor('#2563eb'); // Blue-600
doc.text(category.toUpperCase(), margin, yPos);
yPos += 15;
groupedItems[category].forEach((item, index) => {
if (yPos > doc.internal.pageSize.getHeight() - 80) { doc.addPage(); yPos = margin; }
// Question
doc.setFontSize(10);
doc.setFont('sans-serif', 'bold');
doc.setTextColor('#1f2937');
const qText = `${index + 1}. Q: ${item.question}`;
const qLines = doc.splitTextToSize(qText.toString(), pageWidth - margin * 2);
doc.text(qLines, margin + 10, yPos);
yPos += (qLines.length * 10) + 2;
// Answer
doc.setFontSize(9);
doc.setFont('sans-serif', 'normal');
doc.setTextColor('#374151');
const aText = `A: ${item.answer}`;
const aLines = doc.splitTextToSize(aText.toString(), pageWidth - margin * 2);
doc.text(aLines, margin + 10, yPos);
yPos += (aLines.length * 9) + 8; // Space between items
});
yPos += 10; // Space between categories
}
doc.save(`FAQ_Content_Export.pdf`);
}
// --- Initialization ---
document.addEventListener('DOMContentLoaded', () => {
// 1. Assign DOM elements
dom.categoryFilter = document.getElementById('category-filter');
// 2. Attach listeners
document.getElementById('faqo-add-qa-btn').addEventListener('click', faqo_addItem);
document.getElementById('matrix-next-btn').addEventListener('click', () => faqo_showTab('review', document.querySelector('.faqo-tab-btn[data-tab="review"]')));
document.getElementById('faqo-download-pdf').addEventListener('click', faqo_downloadPDF);
tabButtons.forEach(btn => {
btn.addEventListener('click', (e) => faqo_showTab(e.target.dataset.tab, e.target));
});
// Filter listener
dom.categoryFilter.addEventListener('change', () => faqo_renderReview(dom.categoryFilter.value));
// 3. Initial population
faqo_renderMatrixTable();
});