Summary of Inputs
${[...data.inputs, ...data.metrics].map(item => `
${item.label}:
${item.value}
`).join('')}
`;
};
const generatePDF = () => {
if (!analysisData) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
const pageWidth = doc.internal.pageSize.width;
const margin = 15;
let y = margin + 5;
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.text('Loan Default Risk Analysis Report', pageWidth / 2, y, { align: 'center' });
y += 10;
doc.setFontSize(11);
doc.setFont('helvetica', 'normal');
doc.setTextColor(100);
doc.text(`Report Generated: ${new Date('2025-09-19T14:49:00Z').toLocaleDateString('en-US')}`, pageWidth / 2, y, { align: 'center' });
y += 15;
// Result Section
doc.setFontSize(12);
doc.text('Estimated Default Probability:', margin, y);
doc.setFontSize(36);
doc.setFont('helvetica', 'bold');
doc.text(`${analysisData.result.probability.toFixed(2)}%`, pageWidth - margin, y + 8, { align: 'right' });
y += 10;
doc.setFontSize(14);
doc.text(`Assessment: ${analysisData.result.level}`, pageWidth - margin, y + 5, { align: 'right' });
y += 15;
doc.setDrawColor(220); // Light gray line
doc.line(margin, y, pageWidth - margin, y);
y += 10;
// Details Section
doc.setFontSize(16);
doc.setFont('helvetica', 'bold');
doc.setTextColor(0);
doc.text('Borrower Profile & Loan Metrics', margin, y);
y += 8;
doc.setFontSize(12);
doc.setFont('helvetica', 'normal');
const allData = [...analysisData.inputs, ...analysisData.metrics];
allData.forEach(item => {
if (y > doc.internal.pageSize.height - margin) {
doc.addPage();
y = margin;
}
doc.setFont('helvetica', 'bold');
doc.text(item.label + ':', margin, y);
doc.setFont('helvetica', 'normal');
doc.text(String(item.value), pageWidth / 2, y);
y += 8;
});
doc.save('Loan_Default_Risk_Report.pdf');
};
// APP NAVIGATION
const switchTab = (tabName) => {
if (!tabs.includes(tabName)) return;
currentTab = tabName;
Object.values(tabContent).forEach(content => content.classList.add('hidden'));
tabContent[tabName].classList.remove('hidden');
Object.values(tabButtons).forEach(button => button.classList.remove('active'));
tabButtons[tabName].classList.add('active');
updateNavButtons();
};
const updateNavButtons = () => {
const currentIndex = tabs.indexOf(currentTab);
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === tabs.length - 1;
};
const navigateTabs = (direction) => {
const currentIndex = tabs.indexOf(currentTab);
let newIndex = direction === 'next'
? Math.min(currentIndex + 1, tabs.length - 1)
: Math.max(currentIndex - 1, 0);
switchTab(tabs[newIndex]);
};
// INITIALIZATION
document.getElementById('calculate-risk-btn').addEventListener('click', calculateRisk);
document.getElementById('download-pdf-btn').addEventListener('click', generatePDF);
window.app = {
switchTab,
navigateTabs,
};
updateNavButtons();
});