Original Total
${formatCurrency(originalTotal)}
Rounded Total
${formatCurrency(roundedTotal)}
Total Difference
${formatCurrency(totalDifference)}
Transactions
${processedTransactions.length}
Original vs. Rounded Total
Transaction Breakdown
| Description |
Original |
Rounded |
Difference |
${transactionsHtml}
`;
// Render Chart
const chartCtx = document.getElementById('totalsChart').getContext('2d');
if (charts.totals) charts.totals.destroy();
charts.totals = new Chart(chartCtx, {
type: 'bar',
data: {
labels: ['Amount'],
datasets: [
{ label: 'Original Total', data: [originalTotal], backgroundColor: '#3b82f6' },
{ label: 'Rounded Total', data: [roundedTotal], backgroundColor: '#f43f5e' }
]
},
options: { responsive: true, maintainAspectRatio: false, scales: { y: { beginAtZero: true, ticks: { callback: value => formatCurrency(value) } } }, plugins: { tooltip: { callbacks: { label: c => `${c.dataset.label}: ${formatCurrency(c.raw)}` } } } }
});
document.getElementById('download-pdf-btn').addEventListener('click', generatePdf);
};
const renderDataConfig = () => {
const configContent = document.getElementById('content-data-config');
if (!configContent) return;
const transactionsHtml = appData.transactions.map((tx, index) => `
`).join('');
configContent.innerHTML = `
`;
attachConfigListeners();
};
const attachConfigListeners = () => {
document.getElementById('add-tx-btn').addEventListener('click', () => {
appData.transactions.push({ description: 'New Item', amount: 0 });
renderDataConfig();
lucide.createIcons();
});
document.querySelectorAll('.remove-tx-btn').forEach(btn => btn.addEventListener('click', e => {
appData.transactions.splice(parseInt(e.currentTarget.dataset.index), 1);
renderDataConfig();
lucide.createIcons();
}));
document.getElementById('update-data-btn').addEventListener('click', handleConfigUpdate);
};
const handleConfigUpdate = () => {
appData.roundingRule = document.querySelector('input[name="rounding-rule"]:checked').value;
const newTransactions = [];
document.querySelectorAll('.transaction-row').forEach(row => {
newTransactions.push({
description: row.querySelector('.tx-description').value,
amount: parseFloat(row.querySelector('.tx-amount').value) || 0
});
});
appData.transactions = newTransactions;
calculationResult = null; // Force recalculation
renderDashboard();
alert('Configuration updated and totals recalculated!');
switchTab(0);
};
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: 'landscape', 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('Payment-Rounding-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: 'Rounding Dashboard', id: 'dashboard' },
{ name: 'Data Configuration', id: 'data-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));
});
renderDashboard();
renderDataConfig();
switchTab(0);
};
initializeUI();
lucide.createIcons();
prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); });
});