Suggested Payment Plan
${paymentPlan.installments} payments of ${paymentPlan.amount}
Paid every 2 weeks, interest-free.
`;
lucide.createIcons();
renderScoreGaugeChart(totalScore);
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderScoreGaugeChart = (score) => {
const ctx = document.getElementById('scoreGaugeChart').getContext('2d');
if (charts.scoreGauge) charts.scoreGauge.destroy();
let backgroundColor = '#14b8a6'; // green-500
if (score < appData.config.thresholds.moderate) backgroundColor = '#f59e0b'; // amber-500
if (score < appData.config.thresholds.low) backgroundColor = '#ef4444'; // red-500
charts.scoreGauge = new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [{
data: [score, 100 - score],
backgroundColor: [backgroundColor, '#e5e7eb'],
borderColor: ['#fff'],
borderWidth: 2,
circumference: 180,
rotation: 270
}]
},
options: {
responsive: true, maintainAspectRatio: true,
plugins: {
tooltip: { enabled: false },
datalabels: {
formatter: (value, context) => {
if (context.dataIndex === 0) return `${value}\nScore`;
return '';
},
color: '#1f2937', font: { size: 24, weight: 'bold', family: 'Inter' },
textAlign: 'center'
}
},
cutout: '60%'
},
plugins: [ChartDataLabels]
});
};
const renderConfig = () => {
const configContent = document.getElementById('content-configuration');
if (!configContent) return;
configContent.innerHTML = `
Configure Eligibility Model
Factor Weights
Adjust the importance of each factor. Total must be 100%.
`;
attachConfigListeners();
};
// --- EVENT HANDLERS ---
const attachFormListeners = () => {
document.getElementById('credit-score').addEventListener('input', e => {
document.getElementById('credit-score-label').textContent = e.target.value;
});
document.getElementById('check-eligibility-btn').addEventListener('click', () => {
appData.inputs.annualIncome = parseFloat(document.getElementById('annual-income').value);
appData.inputs.monthlyHousing = parseFloat(document.getElementById('monthly-housing').value);
appData.inputs.otherMonthlyDebt = parseFloat(document.getElementById('other-debt').value);
appData.inputs.creditScore = parseInt(document.getElementById('credit-score').value);
appData.inputs.purchaseAmount = parseFloat(document.getElementById('purchase-amount').value);
appData.result = null; // force recalculation
renderResults();
switchTab(1); // Switch to results tab
});
};
const attachConfigListeners = () => {
const updateTotal = () => {
const totalEl = document.getElementById('weight-total');
let total = 0;
document.querySelectorAll('.config-weight-input').forEach(input => {
total += parseFloat(input.value) || 0;
});
totalEl.textContent = `Total: ${total}%`;
totalEl.className = total === 100 ? 'text-right font-bold text-green-600' : 'text-right font-bold text-red-600';
};
document.querySelectorAll('.config-weight-input').forEach(input => {
input.addEventListener('input', (e) => {
appData.config.weights[e.target.dataset.weight] = parseFloat(e.target.value) || 0;
updateTotal();
appData.result = null; // force recalculation on next view
});
});
updateTotal();
};
const generatePdf = () => {
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content-area');
const pdfHeader = document.getElementById('pdf-header');
document.getElementById('pdf-generated-date').textContent = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
pdfHeader.classList.remove('hidden');
html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false })
.then(canvas => {
pdfHeader.classList.add('hidden');
const imgData = canvas.toDataURL('image/jpeg', 0.95);
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight);
pdf.save('BNPL-Eligibility-Report.pdf');
loadingOverlay.style.display = 'none';
}).catch(err => {
console.error("PDF generation failed:", err);
pdfHeader.classList.add('hidden');
loadingOverlay.style.display = 'none';
alert('An error occurred generating the PDF.');
});
};
// --- TAB NAVIGATION & INITIALIZATION ---
const switchTab = (tabIndex) => {
activeTabIndex = tabIndex;
document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex));
document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex));
updateNavButtons();
};
const updateNavButtons = () => {
prevTabBtn.disabled = activeTabIndex === 0;
nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1;
};
const initializeUI = () => {
const tabs = [
{ name: 'Eligibility Form', id: 'eligibility-form' },
{ name: 'Eligibility Results', id: 'eligibility-results' },
{ name: 'Configuration', id: 'configuration' }
];
tabIdentifiers = tabs.map(t => t.id);
tabsContainer.innerHTML = tabs.map(tab => `
`).join('');
mainContent.innerHTML = tabs.map(tab => `
`).join('');
tabs.forEach((tab, index) => {
document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index));
});
renderForm();
renderResults();
renderConfig();
switchTab(0);
};
initializeUI();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});