`;
} else if (data.loanAmountNeeded <= 0) {
summaryHtml += `
Congratulations! No loan is needed based on your budget and savings.
`;
}
fullSummaryContainerEl.innerHTML = summaryHtml;
if(downloadPdfButton) downloadPdfButton.style.display = 'block';
}
if(downloadPdfButton) {
downloadPdfButton.onclick = function() {
if (!wlbc_budgetDataForPdf) { alert("Please calculate the summary first."); return; }
console.log("WLBC Tool: Generating PDF...");
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
alert('Error: jsPDF library not loaded.'); console.error("WLBC Tool: jsPDF not loaded."); return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'pt', 'letter');
if (typeof doc.autoTable !== 'function') {
alert('Error: jsPDF-AutoTable plugin not loaded.'); console.error("WLBC Tool: jsPDF-AutoTable not loaded."); return;
}
const data = wlbc_budgetDataForPdf;
const pageMargin = 40;
const cellPadding = 5;
const lineHeight = 12;
let yPos = pageMargin;
doc.setFontSize(18);
doc.setTextColor(0, 123, 255); // Hex: #007BFF
doc.text("Wedding Budget & Loan Estimate", pageMargin, yPos); yPos += 30;
doc.setTextColor(0, 0, 0);
doc.setFontSize(14);
doc.text("Wedding Expense Budget", pageMargin, yPos); yPos += (lineHeight * 1.5);
const expenseBody = data.expenses.map(exp => [exp.category, `$${exp.amount.toFixed(2)}`]);
doc.autoTable({
head: [['Category', 'Estimated Cost ($)']],
body: expenseBody,
startY: yPos,
theme: 'striped',
headStyles: { fillColor: [0, 123, 255], textColor: [255, 255, 255], fontSize: 10 },
styles: { fontSize: 9, cellPadding: cellPadding },
columnStyles: { 1: { halign: 'right' } },
didDrawPage: function (hookData) { yPos = hookData.cursor.y > yPos ? hookData.cursor.y : yPos ; }
});
yPos = doc.previousAutoTable.finalY + cellPadding;
doc.setFontSize(10);
doc.setFont(undefined, 'bold');
doc.text(`Total Estimated Expenses: $${data.totalExpenses.toFixed(2)}`, pageMargin, yPos); yPos += (lineHeight * 2);
doc.setFont(undefined, 'normal');
doc.setFontSize(14);
doc.text("Funding Plan", pageMargin, yPos); yPos += (lineHeight * 1.5);
doc.setFontSize(10);
doc.text(`Your Available Savings/Contributions: $${data.savings.toFixed(2)}`, pageMargin + cellPadding, yPos); yPos += lineHeight;
doc.setFont(undefined, 'bold');
const loanNeededColorArray = data.loanAmountNeeded > 0 ? [232, 62, 140] : [40, 167, 69]; // Pinkish or Green
doc.setTextColor(loanNeededColorArray[0], loanNeededColorArray[1], loanNeededColorArray[2]);
doc.text(`Estimated Loan Amount Needed: $${data.loanAmountNeeded.toFixed(2)}`, pageMargin + cellPadding, yPos); yPos += (lineHeight * 2);
doc.setFont(undefined, 'normal');
doc.setTextColor(0, 0, 0);
if (data.loanAmountNeeded > 0 && data.loanRate !== null) {
doc.setFontSize(14);
doc.text("Estimated Loan Details", pageMargin, yPos); yPos += (lineHeight * 1.5);
doc.setFontSize(10);
doc.text(` Loan Amount: $${data.loanAmountNeeded.toFixed(2)}`, pageMargin + cellPadding, yPos); yPos += lineHeight;
doc.text(` Estimated Annual Interest Rate: ${data.loanRate.toFixed(2)}%`, pageMargin + cellPadding, yPos); yPos += lineHeight;
doc.text(` Loan Term: ${data.loanTerm} years`, pageMargin + cellPadding, yPos); yPos += lineHeight;
doc.text(` Estimated Monthly Payment: $${data.loanMonthlyPayment.toFixed(2)}`, pageMargin + cellPadding, yPos); yPos += lineHeight;
doc.text(` Estimated Total Interest Paid: $${data.loanTotalInterest.toFixed(2)}`, pageMargin + cellPadding, yPos); yPos += lineHeight;
doc.text(` Estimated Total Loan Repayment: $${data.loanTotalRepayment.toFixed(2)}`, pageMargin + cellPadding, yPos); yPos += lineHeight;
} else if (data.loanAmountNeeded <= 0) {
doc.setFontSize(10);
doc.setTextColor(40, 167, 69); // Green
doc.text("Congratulations! No loan is needed based on your budget and savings.", pageMargin, yPos); yPos +=(lineHeight*2);
doc.setTextColor(0,0,0);
}
const pageHeight = doc.internal.pageSize.height;
if (yPos < pageHeight - (lineHeight * 5) - pageMargin) {
yPos = pageHeight - (lineHeight * 5) - pageMargin;
} else {
yPos += (lineHeight * 0.5);
}
doc.setFontSize(8);
doc.setTextColor(100, 100, 100); // Gray
const disclaimerLines = doc.splitTextToSize("Note: This is an estimate based on the figures you provided. Actual loan terms, rates, and qualification depend on lender review, your credit profile, and other factors. This is not a loan offer or financial advice.", doc.internal.pageSize.width - (pageMargin*2));
doc.text(disclaimerLines, pageMargin, yPos);
const toolName = "WeddingLoanBudgetEstimate";
const date = new Date();
const filename = `${toolName}_${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, '0')}${String(date.getDate()).padStart(2, '0')}.pdf`;
doc.save(filename);
console.log("WLBC Tool: PDF generated: " + filename);
};
}
if(expenseItemsContainer) {
populateExpenseItems();
wlbc_openTab(null, 'wlbcTab1Expenses');
console.log("WLBC Tool: Initialized. Tab 1 (Expenses) displayed.");
} else {
console.error("WLBC Tool: CRITICAL - expenseItemsContainer not found. Cannot populate expenses.");
}
const allElements = { expenseItemsContainer, totalEstimatedExpensesEl, totalExpensesDisplayEl,
availableSavingsEl, loanAmountNeededDisplayEl, loanEstIntroEl, loanAmountForEstEl,
estimatedLoanRateEl, estimatedLoanTermEl, fullSummaryContainerEl, downloadPdfButton,
calculateSummaryButton, ...tabLinks, ...tabContents, ...errorDivs };
let allCriticalFound = true;
for (const key in allElements) {
if (!allElements[key]) {
console.error(`WLBC Tool: CRITICAL - Element for '${key}' was NOT FOUND in the DOM.`);
allCriticalFound = false;
}
}
if (!allCriticalFound) {
const toolContainer = document.getElementById("wlbcBudgetCalculatorTool");
if (toolContainer) toolContainer.innerHTML = "";
return;
}
console.log("WLBC Tool: All critical DOM elements located.");
});
