No sections created yet.
';
return;
}
checklistData.sections.forEach(section => {
const sectionTitleEl = document.createElement('h3');
sectionTitleEl.textContent = section.title;
reviewContent.appendChild(sectionTitleEl);
if (section.items.length === 0) {
reviewContent.innerHTML += '
No items in this section.
';
return;
}
const tableWrapper = document.createElement('div');
tableWrapper.className = 'review-table-wrapper';
const table = document.createElement('table');
table.className = 'review-table';
let tableHTML = `
| Item Description |
Status |
Notes |
`;
section.items.forEach(item => {
tableHTML += `
| ${item.description} |
${item.status} |
${item.notes} |
`;
});
tableHTML += ``;
table.innerHTML = tableHTML;
tableWrapper.appendChild(table);
reviewContent.appendChild(tableWrapper);
});
pdfDownloadBtn.disabled = false;
switchTab(1); // Switch to review tab
}
/**
* V.A.1, IX: PDF download button *must* work and be *formatted*.
* This function uses jsPDF and autoTable to build a clean report.
*/
function downloadPDF() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'pt', 'a4');
let currentY = 40;
const margin = 40;
const pageWidth = doc.internal.pageSize.width;
// 1. Title
doc.setFontSize(22);
doc.setFont(undefined, 'bold');
doc.text(checklistData.title, pageWidth / 2, currentY, { align: 'center' });
currentY += 30;
// 2. Loop through sections and create tables
checklistData.sections.forEach(section => {
// Section Title
doc.setFontSize(16);
doc.setFont(undefined, 'bold');
doc.text(section.title, margin, currentY);
currentY += 20;
if (section.items.length > 0) {
const tableHead = [['Item Description', 'Status', 'Notes']];
const tableBody = section.items.map(item => [
item.description,
item.status,
item.notes
]);
// Check for page break *before* adding table
const tableHeight = (section.items.length + 1) * 20; // Rough estimate
if (currentY + tableHeight > doc.internal.pageSize.height - margin) {
doc.addPage();
currentY = margin;
}
doc.autoTable({
startY: currentY,
head: tableHead,
body: tableBody,
theme: 'grid',
headStyles: {
fillColor: [0, 123, 255], // --primary-color
fontSize: 10
},
styles: {
fontSize: 9,
cellPadding: 4,
overflow: 'linebreak'
},
columnStyles: {
0: { cellWidth: 'auto' },
1: { cellWidth: 80 },
2: { cellWidth: 'auto' }
}
});
currentY = doc.autoTable.previous.finalY + 25; // Space for next section
} else {
doc.setFontSize(10);
doc.setFont(undefined, 'italic');
doc.text("No items in this section.", margin, currentY);
currentY += 25;
}
});
doc.save(`${checklistData.title.replace(/\s/g, '_') || 'Compliance_Checklist'}.pdf`);
}
pdfDownloadBtn.addEventListener('click', downloadPDF);
// --- Tab Navigation ---
function switchTab(tabIndex) {
tabs.forEach((tab, index) => {
tab.classList.toggle('active', index === tabIndex);
contents[index].classList.toggle('active', index === tabIndex);
});
currentTab = tabIndex;
updateNavButtons();
}
function updateNavButtons() {
prevBtn.disabled = currentTab === 0;
nextBtn.disabled = currentTab === tabs.length - 1;
}
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => switchTab(index));
});
nextBtn.addEventListener('click', () => { if (currentTab < tabs.length - 1) switchTab(currentTab + 1); });
prevBtn.addEventListener('click', () => { if (currentTab > 0) switchTab(currentTab - 1); });
// --- Initial Setup ---
renderBuilder();
renderLibrary();
updateNavButtons();
});