`;
this.elements.analysisOutput.innerHTML = outputHTML;
if(this.elements.pdfDownloadContainer) this.elements.pdfDownloadContainer.style.display = 'block';
this.navigateToTab('FreStressAnalysisTab', 'stressAnalysisTabButton');
},
downloadPdf: function() {
try {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
alert('PDF generation library (jsPDF core) is not loaded.'); return;
}
const { jsPDF: JSPDF_CONSTRUCTOR } = window.jspdf;
const doc = new JSPDF_CONSTRUCTOR();
if (typeof doc.autoTable !== 'function' && this.calculatedData.considerations && this.calculatedData.considerations.length > 0) { // Only check autotable if we might use it
// For this PDF, autoTable might be overkill, simple text may suffice.
// If we use it for considerations:
// alert('PDF generation plugin (jsPDF-Autotable) is not loaded.'); return;
}
if (!this.calculatedData || Object.keys(this.calculatedData).length === 0) {
alert('Error: Calculation data missing for PDF.'); return;
}
const data = this.calculatedData;
if (typeof data.frontEndDTI === 'undefined') {
alert('Error: Essential calculation data missing for PDF.'); return;
}
const primaryColor = '#5c4b99'; const textColor = '#414042';
const fNum = (num, dec=1) => (typeof num !== 'number' || isNaN(num) || !isFinite(num)) ? 'N/A' : num.toLocaleString(undefined, {minimumFractionDigits:dec, maximumFractionDigits:dec});
doc.setFontSize(16); doc.setTextColor(primaryColor);
doc.text("Financial Stress Indication Summary", 14, 22);
doc.setFontSize(10); doc.setTextColor(textColor);
doc.text(`Report Generated: ${new Date().toLocaleDateString('en-US', {year:'numeric', month:'long', day:'numeric'})}`, 14, 30);
let currentY = 45;
const addLine = (label, value, color = textColor) => {
doc.setFont(undefined, 'bold'); doc.setTextColor(textColor); doc.text(label, 14, currentY);
doc.setFont(undefined, 'normal'); doc.setTextColor(color); doc.text(value, 85, currentY); currentY += 7;
};
const addSubHeader = (text) => {
doc.setFontSize(12); doc.setFont(undefined, 'bold'); doc.setTextColor(primaryColor);
doc.text(text, 14, currentY); currentY += 8;
doc.setFontSize(10); doc.setFont(undefined, 'normal'); doc.setTextColor(textColor);
};
addSubHeader("Input Financial Snapshot:");
addLine("Gross Monthly Income:", `$${fNum(data.grossIncome,0)}`);
addLine("Total Monthly Housing Payment:", `$${fNum(data.housingPayment,0)}`);
addLine("Other Monthly Debts:", `$${fNum(data.otherDebts,0)}`);
addLine("Liquid Savings:", `$${fNum(data.savings,0)}`);
currentY +=3;
addSubHeader("Calculated Indicators:");
addLine("Front-End DTI:", `${fNum(data.frontEndDTI)}% (${data.frontEndDTIText})`);
addLine("Back-End DTI:", `${fNum(data.backEndDTI)}% (${data.backEndDTIText})`);
addLine("Savings Buffer:", `${data.savingsBufferMonths === Infinity ? 'N/A' : fNum(data.savingsBufferMonths) + ' months'} (${data.savingsBufferText})`);
if (data.paymentsBehindStatus !== 'no') addLine("Payment Status:", data.paymentsBehindStatus.replace('yes_','').replace('_plus','+') + " month(s) behind", '#c0392b');
if (data.incomeLossStatus !== 'no') addLine("Recent Income Loss:", data.incomeLossStatus.replace('yes_',''), '#dd6b20');
currentY += 5;
doc.setFontSize(12); doc.setFont(undefined, 'bold');
let stressColor = textColor;
if (data.stressClass === "fre-stress-low") stressColor = '#2e856e';
else if (data.stressClass === "fre-stress-moderate") stressColor = '#d68910';
else if (data.stressClass === "fre-stress-high") stressColor = '#c0392b';
doc.setTextColor(stressColor);
doc.text(`Overall Indication: ${data.overallStressLevel}`, 14, currentY); currentY += 10;
doc.setTextColor(textColor);
addSubHeader("General Considerations:");
doc.setFontSize(9);
data.considerations.forEach(item => {
const splitItem = doc.splitTextToSize(`• ${item}`, doc.internal.pageSize.width - 28 - 14);
if (currentY + splitItem.length * 5 > doc.internal.pageSize.height - 30) { doc.addPage(); currentY = 20; }
doc.text(splitItem, 14, currentY);
currentY += splitItem.length * 5;
});
currentY += 5;
doc.setFontSize(8); doc.setTextColor('#6d6875');
const finalNote = "IMPORTANT: This tool provides an estimation of financial stress indicators based on your inputs. It is not financial advice and does not predict foreclosure, which is a complex legal process. Actual risk depends on many individual factors and local regulations. If you have concerns about your mortgage or financial situation, please contact your lender, a HUD-approved housing counselor (in the US), or a qualified financial advisor promptly for personalized assistance.";
const splitFinalNote = doc.splitTextToSize(finalNote, doc.internal.pageSize.width - 28);
if (currentY + splitFinalNote.length * 4 > doc.internal.pageSize.height - 15) { doc.addPage(); currentY = 20; }
doc.text(splitFinalNote, 14, currentY);
doc.save("Financial_Stress_Indicator_Summary.pdf");
} catch (error) {
console.error("PDF Generation Error:", error);
alert("An error occurred while generating the PDF: " + error.message + "\nPlease check the console (F12).");
}
}
};
document.addEventListener('DOMContentLoaded', function() {
freApp.init();
});
