${tab.name}
`; tab.items.forEach((item, itemIndex) => { const itemId = `${tabId}-item-${itemIndex}`; const itemDiv = document.createElement('div'); itemDiv.className = 'checklist-item'; itemDiv.innerHTML = ` `; content.appendChild(itemDiv); }); } tabContentsWrapper.appendChild(content); }); // Attach event listener for PDF button after it's created const downloadPdfButton = document.getElementById('download-pdf-button'); if (downloadPdfButton) { downloadPdfButton.addEventListener('click', downloadPDF); } } /** * Shows the specified tab and hides others. Updates button states. * @param {number} index - The index of the tab to show. */ function showTab(index) { if (index < 0 || index >= tabIds.length) { console.warn(`Attempted to show invalid tab index: ${index}`); return; } currentTabIndex = index; // Update content visibility document.querySelectorAll('.tab-content').forEach((content, i) => { if (i === index) { content.classList.add('active'); } else { content.classList.remove('active'); } }); // Update button active state document.querySelectorAll('.tab-button').forEach((button, i) => { if (i === index) { button.classList.add('active'); } else { button.classList.remove('active'); } }); // Update navigation button visibility and text if (nextButton && prevButton) { prevButton.style.display = currentTabIndex === 0 ? 'none' : 'inline-flex'; if (currentTabIndex === tabIds.length - 1) { // If on the last tab (Results) nextButton.style.display = 'none'; // Hide next button } else if (currentTabIndex === tabIds.length - 2) { // If on the second to last tab (e.g., Trading & Portfolio) nextButton.textContent = 'Generate Report'; nextButton.style.display = 'inline-flex'; nextButton.removeEventListener('click', handleNextButtonClick); // Remove generic next listener nextButton.addEventListener('click', generateComplianceReport); // Add report generation listener } else { nextButton.textContent = 'Next'; nextButton.style.display = 'inline-flex'; nextButton.removeEventListener('click', generateComplianceReport); // Remove report generation listener nextButton.addEventListener('click', handleNextButtonClick); // Add generic next listener } } } /** * Handles click on tab buttons. * @param {Event} event - The click event. */ function handleTabButtonClick(event) { const targetTabId = event.target.dataset.tab; const newIndex = tabIds.indexOf(targetTabId); if (newIndex !== -1) { showTab(newIndex); } } /** * Handles click on the "Next" button. */ function handleNextButtonClick() { if (currentTabIndex < tabIds.length - 1) { showTab(currentTabIndex + 1); } } /** * Handles click on the "Previous" button. */ function handlePrevButtonClick() { if (currentTabIndex > 0) { showTab(currentTabIndex - 1); } } /** * Generates the compliance report based on checked items. */ function generateComplianceReport() { const allCheckboxes = document.querySelectorAll('#tab-contents-wrapper input[type="checkbox"]'); const results = {}; let completedItemsCount = 0; let totalItemsCount = 0; // Initialize results structure tabIds.forEach(tabId => { if (tabId !== 'results') { results[tabId] = { name: complianceData[tabId].name, items: [] }; totalItemsCount += complianceData[tabId].items.length; } }); // Populate results based on checkbox status allCheckboxes.forEach(checkbox => { const tabId = checkbox.dataset.tabId; const itemIndex = parseInt(checkbox.dataset.itemIndex); const itemName = complianceData[tabId].items[itemIndex]; const isChecked = checkbox.checked; if (isChecked) { completedItemsCount++; } if (results[tabId]) { // Ensure the tabId exists in results (i.e., not 'results' tab itself) results[tabId].items.push({ name: itemName, completed: isChecked }); } }); const completionPercentage = totalItemsCount > 0 ? (completedItemsCount / totalItemsCount) * 100 : 0; let outputHtml = `Overall Compliance Progress: ${completionPercentage.toFixed(2)}% Complete
(${completedItemsCount} of ${totalItemsCount} items completed)
Detailed Breakdown:
| Category | Compliance Item | Status |
|---|---|---|
| ${category.name} | ` : ''}${item.name} | ${item.completed ? 'Complete' : 'Incomplete'} |
Hedge Fund Compliance Checklist Report
Generated on: ${new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}
${resultsOutputDiv.innerHTML}
`;
// Temporarily hide the main tool container to avoid layout issues during PDF generation
complianceChecklistContainer.style.display = 'none';
html2pdf().from(pdfContent).set({
margin: [20, 20, 20, 20], // Top, Left, Bottom, Right
filename: 'Hedge_Fund_Compliance_Checklist.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, logging: true, dpi: 192, letterRendering: true },
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }
}).save().then(() => {
// Restore visibility of the main tool container after PDF generation is complete
complianceChecklistContainer.style.display = 'flex';
pdfContent.remove(); // Clean up temporary element
}).catch(error => {
console.error("Error generating PDF:", error);
showCustomModal('PDF Generation Error', 'There was an issue generating the PDF. Please try again.');
complianceChecklistContainer.style.display = 'flex';
pdfContent.remove();
});
}
// --- Event Listeners ---
if (nextButton) {
nextButton.addEventListener('click', handleNextButtonClick);
}
if (prevButton) {
prevButton.addEventListener('click', handlePrevButtonClick);
}
// Custom modal close button
if (modalCloseButton) {
modalCloseButton.addEventListener('click', hideCustomModal);
}
// Close modal if clicking outside (on overlay)
if (customModalOverlay) {
customModalOverlay.addEventListener('click', (e) => {
if (e.target === customModalOverlay) {
hideCustomModal();
}
});
}
// --- Initial Setup ---
initializeTabs(); // Populate tabs and content on load
showTab(0); // Show the first tab on load
});