Alerts Triggered
${currentData.totalAlerts}
Total Value Flagged
$${currentData.totalAlertValue.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})}
Flagged Transactions
| ID | Customer | Amount | Location | Reason |
${alertRows || `| No alerts triggered. |
`}
`;
const ctx = getElem('alertChart').getContext('2d');
if (alertChart) alertChart.destroy();
alertChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: Object.keys(currentData.alertCounts),
datasets: [{
data: Object.values(currentData.alertCounts),
backgroundColor: ['#EF4444', '#F59E0B', '#3B82F6'],
}]
},
options: { responsive: true, maintainAspectRatio: false }
});
downloadSection.classList.remove('hidden');
showTab(1);
};
runMonitoringBtn.addEventListener('click', runMonitoring);
// --- 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("Transaction Alert 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 Monitored', `${currentData.transactions.length}`],
['Total Alerts Triggered', { content: `${currentData.totalAlerts}`, styles: {textColor: '#DC2626', fontStyle: 'bold'} }],
['Total Value of Flagged Transactions', `$${currentData.totalAlertValue.toLocaleString('en-US')}`],
],
didDrawPage: (data) => { y = data.cursor.y; }
});
y += 20;
pdf.setFont('helvetica', 'bold');
pdf.text("Flagged Transaction Details", margin, y);
y += 15;
const tableData = currentData.alerts.map(a => [a.id, a.name, `$${a.amount.toFixed(2)}`, a.location, a.reasons.join(', ')]);
pdf.autoTable({
startY: y,
head: [['Transaction ID', 'Customer', 'Amount', 'Location', 'Alert Reason']],
body: tableData,
theme: 'striped',
headStyles: { fillColor: [220, 38, 38] }, // red-600
});
pdf.save(`Transaction-Alert-Report.pdf`);
} catch(e) { console.error("PDF Generation failed:", e); }
finally {
btnText.classList.remove('hidden');
btnSpinner.classList.remove('hidden');
downloadPdfBtn.disabled = false;
}
};
downloadPdfBtn.addEventListener('click', downloadPDF);
initialize();
});