`;
resultsOutput.innerHTML = html;
}
// --- Event Listeners ---
messageBoxCloseButton.addEventListener('click', hideMessageBox);
// Tab navigation
tabInputButton.addEventListener('click', () => switchTab('input'));
tabResultsButton.addEventListener('click', () => switchTab('results'));
tabAboutButton.addEventListener('click', () => switchTab('about'));
// Next/Previous buttons
nextTabButton.addEventListener('click', function() {
if (activeTab === 'input') {
switchTab('results');
} else if (activeTab === 'results') {
switchTab('about');
}
});
prevTabButton.addEventListener('click', function() {
if (activeTab === 'results') {
switchTab('input');
} else if (activeTab === 'about') {
switchTab('results');
}
});
// Collapsible sections
currentYearHeader.addEventListener('click', () => toggleCollapsible(currentYearHeader, currentYearContent));
previousYearHeader.addEventListener('click', () => toggleCollapsible(previousYearHeader, previousYearContent));
// PDF Download functionality
downloadPdfButton.addEventListener('click', async function() {
const pdfContentDiv = document.createElement('div');
pdfContentDiv.style.position = 'absolute';
pdfContentDiv.style.left = '-9999px';
pdfContentDiv.style.width = '800px';
pdfContentDiv.style.backgroundColor = '#ffffff';
pdfContentDiv.style.padding = '20px'; // Add padding for better look in PDF
const pdfTitle = document.createElement('h2');
pdfTitle.textContent = 'Financial Statement Fraud Detection Report';
pdfTitle.className = 'text-2xl font-bold text-gray-800 mb-6 text-center py-4';
pdfContentDiv.appendChild(pdfTitle);
// Add content from resultsOutput, ensuring tables are cloned correctly
const resultsContent = resultsOutput.cloneNode(true);
resultsContent.removeAttribute('id'); // Remove ID to avoid duplicates
resultsContent.style.overflowX = 'visible'; // Ensure content is not cut off in PDF
// Remove the "No results message" if it's there
const msg = resultsContent.querySelector('#noResultsMessage');
if (msg) msg.remove();
// Remove the download button from the cloned content
const btn = resultsContent.querySelector('#downloadPdfButton');
if (btn) btn.remove();
pdfContentDiv.appendChild(resultsContent);
// Apply consistent styling to cloned tables for PDF
pdfContentDiv.querySelectorAll('table').forEach(table => {
table.style.width = '100%';
table.style.borderCollapse = 'collapse';
table.style.marginBottom = '20px'; /* Add some space between tables */
table.style.fontFamily = 'sans-serif'; /* Ensure font for PDF */
});
pdfContentDiv.querySelectorAll('th, td').forEach(cell => {
cell.style.border = '1px solid #e5e7eb';
cell.style.padding = '12px';
cell.style.textAlign = 'left';
cell.style.fontSize = '12px'; /* Smaller font for PDF */
});
pdfContentDiv.querySelectorAll('th').forEach(th => {
th.style.backgroundColor = '#f9fafb';
th.style.fontWeight = '600';
th.style.color = '#374151';
});
pdfContentDiv.querySelectorAll('tbody tr:nth-child(odd)').forEach(tr => {
tr.style.backgroundColor = '#f9fafb';
});
pdfContentDiv.querySelectorAll('tbody tr:hover').forEach(tr => {
tr.style.backgroundColor = '#ffffff'; /* Remove hover effect for PDF */
});
pdfContentDiv.querySelectorAll('h4').forEach(h4 => {
h4.style.fontSize = '1.125rem'; /* Tailwind text-lg */
h4.style.fontWeight = 'bold';
h4.style.color = '#374151'; /* Gray-700 */
h4.style.marginBottom = '0.5rem';
});
pdfContentDiv.querySelectorAll('.red-flag').forEach(span => {
span.style.color = '#dc2626'; /* Ensure red color in PDF */
span.style.fontWeight = 'bold';
});
pdfContentDiv.querySelectorAll('.yellow-flag').forEach(span => {
span.style.color = '#d97706'; /* Ensure yellow color in PDF */
span.style.fontWeight = 'bold';
});
pdfContentDiv.querySelectorAll('.green-flag').forEach(span => {
span.style.color = '#16a34a'; /* Ensure green color in PDF */
span.style.fontWeight = 'bold';
});
document.body.appendChild(pdfContentDiv);
try {
const canvas = await html2canvas(pdfContentDiv, {
scale: 2, // Increase scale for better resolution in PDF
useCORS: true, // Enable cross-origin resource sharing for images (if any)
logging: false // Disable logging for cleaner console
});
const imgData = canvas.toDataURL('image/png');
// Initialize jsPDF
const { jsPDF } = window.jspdf;
const pdf = new jsPDF('p', 'mm', 'a4'); // Portrait, millimeters, A4 size
const imgWidth = 190; // A4 width in mm (210mm) minus margins (10mm on each side)
const pageHeight = pdf.internal.pageSize.height;
const imgHeight = canvas.height * imgWidth / canvas.width;
let heightLeft = imgHeight;
let position = 10; // Initial Y position for content
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
// Add more pages if content overflows
while (heightLeft >= -50) { // Adjusted condition to ensure all content is captured
position = heightLeft - imgHeight + 10; // Adjust position for new page
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
pdf.save('Financial_Fraud_Detection_Report.pdf');
} catch (error) {
console.error('Error generating PDF:', error);
showMessageBox('PDF Error', 'Failed to generate PDF. Please try again. Details: ' + error.message);
} finally {
// Clean up the temporary div
document.body.removeChild(pdfContentDiv);
}
});
});
} catch (e) {
// Catch any errors that occur even before DOMContentLoaded fires fully
// or unhandled errors within the DOMContentLoaded function
// Use a fallback to alert if messageBox isn't ready
if (document.getElementById('messageBoxTitle') && document.getElementById('messageBoxText')) {
document.getElementById('messageBoxTitle').textContent = 'Initialization Error';
document.getElementById('messageBoxText').textContent = 'An unexpected error occurred during tool initialization: ' + e.message + '. Please refresh the page.';
document.getElementById('messageBox').style.display = 'flex';
} else {
console.error("Critical Error during tool initialization:", e);
alert("An critical error occurred during tool initialization: " + e.message + ". Please refresh the page. Check console for details.");
}
}