High-Risk Transactions
${currentData.highRiskCount}
Potential Savings
$${currentData.potentialSaved.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})}
Flagged for Manual Review (High Risk)
| Customer | Amount | Location | Reasons |
${highRiskRows || `| No high-risk transactions found. |
`}
`;
const ctx = getElem('riskChart').getContext('2d');
if (riskChart) riskChart.destroy();
riskChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Low Risk', 'Medium Risk', 'High Risk'],
datasets: [{
data: [currentData.riskCounts.Low, currentData.riskCounts.Medium, currentData.riskCounts.High],
backgroundColor: ['#22C55E', '#F59E0B', '#EF4444'],
}]
},
options: { responsive: true, maintainAspectRatio: false, cutout: '50%' }
});
downloadSection.classList.remove('hidden');
showTab(1);
};
analyzeBtn.addEventListener('click', analyzeTransactions);
// --- PDF DOWNLOAD ---
const downloadPDF = () => {
if (!currentData) return;
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const btnText = getElem('pdf-btn-text');
const btnSpinner = getElem('pdf-btn-spinner');
btnText.classList.add('hidden');
btnSpinner.classList.remove('hidden');
downloadPdfBtn.disabled = true;
try {
const pageWidth = pdf.internal.pageSize.getWidth();
const margin = 40;
let y = margin;
pdf.setFont('helvetica', 'bold');
pdf.setFontSize(22);
pdf.text("Chargeback Analysis Report", pageWidth / 2, y, { align: 'center' });
y += 15;
pdf.setFont('helvetica', 'normal');
pdf.setFontSize(10);
pdf.text(`Report Date: ${today.toLocaleDateString('en-US')}`, pageWidth / 2, y, { align: 'center' });
y += 35;
pdf.autoTable({
startY: y,
theme: 'grid',
head: [['Metric', 'Value']],
body: [
['Total Transactions Analyzed', `${currentData.results.length}`],
['High-Risk Transactions Flagged', { content: `${currentData.highRiskCount}`, styles: {textColor: '#DC2626', fontStyle: 'bold'} }],
['Potential Savings from Blocked Transactions', `$${currentData.potentialSaved.toLocaleString('en-US')}`],
],
didDrawPage: (data) => { y = data.cursor.y; }
});
y += 20;
pdf.setFont('helvetica', 'bold');
pdf.text("High-Risk Transactions Flagged for Review", margin, y);
y += 15;
const tableData = currentData.results.filter(r => r.riskLevel === 'High').map(r => [r.name, `$${r.amount.toFixed(2)}`, r.location, r.reasons]);
if (tableData.length > 0) {
pdf.autoTable({
startY: y,
head: [['Customer', 'Amount', 'Location', 'Risk Factors']],
body: tableData,
theme: 'striped',
headStyles: { fillColor: [220, 38, 38] }, // red-600
});
} else {
pdf.setFont('helvetica', 'italic');
pdf.text("No high-risk transactions were identified in this batch.", margin, y);
}
pdf.save(`Chargeback-Analysis-Report.pdf`);
} catch(e) { console.error("PDF Generation failed:", e); }
finally {
btnText.classList.remove('hidden');
btnSpinner.classList.add('hidden');
downloadPdfBtn.disabled = false;
}
};
downloadPdfBtn.addEventListener('click', downloadPDF);
initialize();
});