`;
return content;
}
function downloadPdf() {
// Need to ensure calculations have been run
if (Object.keys(calculatedMetrics).length === 0 || !calculatedMetrics.peRatio) {
errorMessageDiv.textContent = "Please calculate P/E ratios first before downloading the PDF.";
errorMessageDiv.style.display = 'block';
return;
}
const printWindow = window.open('', '_blank');
if (!printWindow) {
alert("Please allow pop-ups for PDF download.");
return;
}
printWindow.document.write('P/E Ratio Report ');
printWindow.document.write('');
printWindow.document.write('');
printWindow.document.write(generatePdfContent());
printWindow.document.write('');
printWindow.document.close();
printWindow.print();
printWindow.close();
}
// --- Event Listeners ---
tab1Button.addEventListener('click', () => activateTab('tab1Content'));
tab2Button.addEventListener('click', () => activateTab('tab2Content'));
calculateButton.addEventListener('click', calculatePERatio);
resetButton.addEventListener('click', resetCalculator);
pdfDownloadButton.addEventListener('click', downloadPdf);
prevTabButton.addEventListener('click', function() {
const currentTab = document.querySelector('.pe-tab-content.active');
const tabs = [tab1Content, tab2Content];
const currentIndex = tabs.indexOf(currentTab);
if (currentIndex > 0) {
activateTab(tabs[currentIndex - 1].id);
}
});
nextTabButton.addEventListener('click', function() {
const currentTab = document.querySelector('.pe-tab-content.active');
const tabs = [tab1Content, tab2Content];
const currentIndex = tabs.indexOf(currentTab);
if (currentIndex < tabs.length - 1) {
// If on input tab and trying to go to results, calculate first
if (currentTab.id === 'tab1Content') {
calculatePERatio(); // Attempt calculation
// If calculation fails, do not proceed to tab 2
if (errorMessageDiv.style.display === 'block') {
return;
}
}
activateTab(tabs[currentIndex + 1].id);
}
});
// Initial setup
resetCalculator(); // Call reset to set up default inputs and initial state
updateNavigationButtons();
});