`;
document.getElementById('analysis-form').addEventListener('submit', handleAnalysis);
};
const handleAnalysis = (e) => {
e.preventDefault();
// --- Gather Inputs ---
const amount = parseFloat(document.getElementById('transaction-amount').value);
const locationMatch = document.getElementById('location-match').value;
const merchantCategory = document.getElementById('merchant-category').value;
const transactionTime = document.getElementById('transaction-time').value;
const customerHistory = document.getElementById('customer-history').value;
transactionInputs = {
amount: new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(amount),
locationMatch, merchantCategory, transactionTime, customerHistory
};
// --- "AI" Scoring Logic ---
let riskScore = 0;
let maxScore = 0;
const riskFactors = [];
const { weights, thresholds } = appData;
// Amount
maxScore += weights.amount * 10;
if (amount > thresholds.amount) {
const factor = Math.min(amount / thresholds.amount, 3); // Cap contribution
riskScore += weights.amount * 10 * factor;
riskFactors.push({ name: 'High Transaction Amount', impact: 'High' });
}
// Location Mismatch
maxScore += weights.locationMismatch * 10;
if (locationMatch === 'mismatch') {
riskScore += weights.locationMismatch * 10;
riskFactors.push({ name: 'Location Mismatch', impact: 'High' });
}
// High-Risk Category
maxScore += weights.highRiskCategory * 10;
if (merchantCategory === 'high') {
riskScore += weights.highRiskCategory * 10;
riskFactors.push({ name: 'High-Risk Merchant', impact: 'Medium' });
} else if (merchantCategory === 'medium') {
riskScore += weights.highRiskCategory * 5;
}
// Late Night Transaction
maxScore += weights.lateNight * 10;
if (transactionTime === 'late-night') {
riskScore += weights.lateNight * 10;
riskFactors.push({ name: 'Late-Night Transaction', impact: 'Low' });
}
// New Customer
maxScore += weights.newCustomer * 10;
if (customerHistory === 'new') {
riskScore += weights.newCustomer * 10;
riskFactors.push({ name: 'New Customer', impact: 'Medium' });
}
// Normalize score to 0-100
const finalScore = Math.min(100, Math.round((riskScore / maxScore) * 100));
let recommendation, scoreColor, statusIcon;
if (finalScore >= 75) {
recommendation = 'High Risk - Decline';
scoreColor = 'border-red-500';
statusIcon = `
`;
} else if (finalScore >= 40) {
recommendation = 'Medium Risk - Manual Review';
scoreColor = 'border-yellow-500';
statusIcon = `
`;
} else {
recommendation = 'Low Risk - Approve';
scoreColor = 'border-green-500';
statusIcon = `
`;
}
analysisResult = { score: finalScore, recommendation, riskFactors, scoreColor, statusIcon };
renderResults();
};
const renderResults = () => {
const resultsContainer = document.getElementById('results-container');
if (!analysisResult) return;
const { score, recommendation, riskFactors, scoreColor, statusIcon } = analysisResult;
resultsContainer.innerHTML = `
Analysis Result
${statusIcon}
Recommendation
${recommendation}
Key Risk Factors Identified
${riskFactors.length > 0 ? riskFactors.map(f => `
-
${f.name}
${f.impact} Impact
`).join('') : `- No significant risk factors identified.
`}
`;
const gaugeFill = resultsContainer.querySelector('.risk-gauge-fill');
const rotation = (score / 100) * 180;
gaugeFill.style.transform = `rotate(${rotation}deg)`;
lucide.createIcons();
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderConfigTab = () => {
const configContent = document.getElementById('content-config');
if (!configContent) return;
const { weights, thresholds } = appData;
configContent.innerHTML = `
`;
document.getElementById('config-form').addEventListener('submit', handleConfigUpdate);
};
const handleConfigUpdate = (e) => {
e.preventDefault();
Object.keys(appData.weights).forEach(key => {
appData.weights[key] = parseFloat(document.getElementById(`weight-${key}`).value);
});
appData.thresholds.amount = parseFloat(document.getElementById('threshold-amount').value);
alert('Risk model configuration saved!');
};
const generatePdf = () => {
if (!analysisResult) return;
loadingOverlay.style.display = 'flex';
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
let y = 40;
pdf.setFontSize(18);
pdf.setFont('helvetica', 'bold');
pdf.text('Fraud Risk Analysis Report', pdf.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 30;
pdf.setFontSize(10);
pdf.setFont('helvetica', 'normal');
pdf.text(`Generated on: ${new Date().toLocaleDateString()}`, 40, y);
y += 30;
// Transaction Details Table
pdf.setFontSize(12);
pdf.setFont('helvetica', 'bold');
pdf.text('Transaction Details', 40, y);
y += 15;
const transactionData = Object.entries(transactionInputs).map(([key, value]) => [key.replace(/([A-Z])/g, ' $1').replace(/^./, str => str.toUpperCase()), value]);
pdf.autoTable({ startY: y, head: [['Parameter', 'Value']], body: transactionData, theme: 'striped' });
y = pdf.autoTable.previous.finalY + 30;
// Analysis Result
pdf.setFontSize(12);
pdf.setFont('helvetica', 'bold');
pdf.text('Analysis Result', 40, y);
y += 20;
pdf.setFontSize(10);
pdf.setFont('helvetica', 'normal');
pdf.text(`Fraud Risk Score:`, 50, y);
pdf.setFont('helvetica', 'bold');
pdf.text(`${analysisResult.score} / 100`, 150, y);
y += 20;
pdf.setFont('helvetica', 'normal');
pdf.text(`Recommendation:`, 50, y);
pdf.setFont('helvetica', 'bold');
pdf.text(analysisResult.recommendation, 150, y);
y += 30;
// Risk Factors Table
if (analysisResult.riskFactors.length > 0) {
pdf.setFontSize(12);
pdf.setFont('helvetica', 'bold');
pdf.text('Key Risk Factors Identified', 40, y);
y += 15;
pdf.autoTable({
startY: y,
head: [['Factor', 'Assessed Impact']],
body: analysisResult.riskFactors.map(f => [f.name, f.impact]),
theme: 'grid',
headStyles: { fillColor: [220, 53, 69] } // Red header
});
}
pdf.save(`Fraud-Analysis-Report.pdf`);
loadingOverlay.style.display = 'none';
};
// --- 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: 'Transaction Analysis', id: 'analysis' },
{ name: 'Risk Model Configuration', id: 'config' }
];
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));
});
renderAnalysisTab();
renderConfigTab();
switchTab(0);
lucide.createIcons();
};
initializeUI();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});