`;
}
pdfHtml += `
`;
if(pdfOutputEl) pdfOutputEl.innerHTML = pdfHtml;
const opt = {
margin: [0.5, 0.5, 0.5, 0.5],
filename: `Future_Expense_Projections_${new Date().toISOString().slice(0,10)}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, useCORS: true, logging: false },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
if (typeof html2pdf !== 'undefined' && pdfOutputEl) {
html2pdf().from(pdfOutputEl).set(opt).save()
.catch(err => { console.error("PDF generation failed:", err); alert("PDF generation error."); });
} else {
alert("PDF library not loaded or output element not found.");
}
}
// --- Tab Navigation ---
window.fept_openTab = function(evt, tabName) {
tabs.forEach(tab => { if(tab) tab.style.display = 'none'; });
tabLinks.forEach(link => { if(link) link.classList.remove('active'); });
const activeTabContent = toolContainer.querySelector('#' + tabName);
if(activeTabContent) activeTabContent.style.display = 'block';
else { console.error(`Tab content for ${tabName} not found.`); return; }
let clickedIndex = -1;
if (evt && evt.currentTarget) {
evt.currentTarget.classList.add('active');
clickedIndex = tabLinks.indexOf(evt.currentTarget);
} else {
clickedIndex = tabLinks.findIndex(link => {
if (!link) return false;
const onclickAttr = link.getAttribute('onclick');
return onclickAttr && onclickAttr.includes(`'${tabName}'`);
});
if (clickedIndex !== -1 && tabLinks[clickedIndex]) {
tabLinks[clickedIndex].classList.add('active');
} else { // Fallback if findIndex fails
if (tabLinks.length > 0 && tabLinks[0]) tabLinks[0].classList.add('active');
clickedIndex = 0;
}
}
currentTabIndex = (clickedIndex !== -1 && clickedIndex < tabs.length) ? clickedIndex : 0;
fept_updateNavButtons();
// If opening tab 2, automatically try to calculate if data exists
if (tabName === 'fept-tab2' && expenses.length > 0 && getNum(projectionYearsEl) > 0 && getNum(mainInflationRateEl) >= 0) {
fept_calculateAndDisplayProjections(true); // Run with sensitivity to show all possible tables
}
}
window.fept_navigateTab = function(direction) {
let newIndex = currentTabIndex;
if (direction === 'next' && currentTabIndex < tabs.length - 1) newIndex++;
else if (direction === 'prev' && currentTabIndex > 0) newIndex--;
if (tabLinks[newIndex] && newIndex !== currentTabIndex) tabLinks[newIndex].click();
}
function fept_updateNavButtons() {
if(prevButton) prevButton.disabled = currentTabIndex === 0;
if(nextButton) nextButton.disabled = currentTabIndex >= tabs.length - 1;
}
// --- LocalStorage Load/Save ---
function saveToLocalStorage() {
try {
localStorage.setItem(`${toolId}_expenses`, JSON.stringify(expenses));
localStorage.setItem(`${toolId}_projectionYears`, projectionYearsEl.value);
localStorage.setItem(`${toolId}_mainInflationRate`, mainInflationRateEl.value);
} catch (e) { console.warn("Could not save to local storage."); }
}
function loadFromLocalStorage() {
try {
const storedExpenses = localStorage.getItem(`${toolId}_expenses`);
if (storedExpenses) expenses = JSON.parse(storedExpenses);
const storedYears = localStorage.getItem(`${toolId}_projectionYears`);
if (storedYears && projectionYearsEl) projectionYearsEl.value = storedYears;
const storedInflation = localStorage.getItem(`${toolId}_mainInflationRate`);
if (storedInflation && mainInflationRateEl) mainInflationRateEl.value = storedInflation;
} catch (e) {
console.warn("Could not load from local storage or data was corrupted.");
expenses = []; // Reset if error
}
}
// --- Initialization ---
function initializeTool() {
loadFromLocalStorage();
fept_renderExpensesTable();
fept_openTab(null, 'fept-tab1');
}
let fept_attempts = 0;
function checkLibrariesAndInit_fept() {
if (typeof html2pdf !== 'undefined') {
initializeTool();
} else if (fept_attempts < 20) {
fept_attempts++;
setTimeout(checkLibrariesAndInit_fept, 100);
} else {
alert("Error: PDF generation library (html2pdf.js) could not be loaded. PDF export will not work.");
initializeTool();
}
}
checkLibrariesAndInit_fept();
})();
