Number of Vendors
${vendorCount}
Vendor Payout Details
| Vendor Name | Payout Amount |
${vendorRowsHtml}
`;
// Render Chart
const splitCtx = document.getElementById('revenueSplitChart').getContext('2d');
if (charts.split) charts.split.destroy();
charts.split = new Chart(splitCtx, {
type: 'doughnut',
data: {
labels: ['Platform Commission', 'Total Vendor Payouts'],
datasets: [{ data: [platformCommission, totalVendorPayout], backgroundColor: ['#0891b2', '#10b981'] }]
},
options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'bottom' }, tooltip: { callbacks: { label: c => `${c.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 vendorsHtml = appData.vendors.map((vendor, index) => `
`).join('');
configContent.innerHTML = `
`;
updateVendorShareTotal();
attachConfigListeners();
};
const attachConfigListeners = () => {
document.getElementById('add-vendor-btn').addEventListener('click', () => {
appData.vendors.push({ name: 'New Vendor', share: 0 });
renderDataConfig();
lucide.createIcons();
});
document.querySelectorAll('.remove-vendor-btn').forEach(btn => btn.addEventListener('click', e => {
appData.vendors.splice(parseInt(e.currentTarget.dataset.index), 1);
renderDataConfig();
lucide.createIcons();
}));
document.getElementById('vendors-container').addEventListener('input', updateVendorShareTotal);
document.getElementById('update-data-btn').addEventListener('click', handleConfigUpdate);
};
const updateVendorShareTotal = () => {
const totalEl = document.getElementById('vendor-share-total');
if (!totalEl) return;
const total = Array.from(document.querySelectorAll('.vendor-share')).reduce((sum, input) => sum + (parseFloat(input.value) || 0), 0);
totalEl.textContent = `${total}%`;
totalEl.className = total === 100 ? 'text-green-600' : 'text-red-600';
};
const handleConfigUpdate = () => {
appData.totalRevenue = parseFloat(document.getElementById('totalRevenue').value) || 0;
appData.platformCommissionRate = parseFloat(document.getElementById('platformCommissionRate').value) || 0;
const newVendors = [];
document.querySelectorAll('.vendor-row').forEach(row => {
newVendors.push({
name: row.querySelector('.vendor-name').value,
share: parseFloat(row.querySelector('.vendor-share').value) || 0,
});
});
appData.vendors = newVendors;
distributionResult = null; // Force recalculation
renderDashboard();
alert('Distribution model updated!');
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-Distribution-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: 'Distribution 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); });
});