Effective Tax Rate
${results.effectiveTaxRate.toFixed(2)}%
Income Tax
${formatCurrency(results.incomeTax)}
Self-Employment Tax
${formatCurrency(results.seTax)}
`;
// Update charts
if (taxBreakdownChart) taxBreakdownChart.destroy();
taxBreakdownChart = new Chart(document.getElementById('tax-breakdown-chart').getContext('2d'), {
type: 'doughnut',
data: {
labels: ['Income Tax', 'Self-Employment Tax'],
datasets: [{ data: [results.incomeTax, results.seTax], backgroundColor: ['#10b981', '#f59e0b'] }]
},
options: { responsive: true, plugins: { legend: { position: 'top' } } }
});
if (incomeOverviewChart) incomeOverviewChart.destroy();
incomeOverviewChart = new Chart(document.getElementById('income-overview-chart').getContext('2d'), {
type: 'bar',
data: {
labels: ['Gross Income', 'Net Earnings', 'Taxable Income'],
datasets: [{ data: [results.grossIncome, results.netEarnings, results.taxableIncome], backgroundColor: ['#3b82f6', '#8b5cf6', '#ec4899'] }]
},
options: { responsive: true, plugins: { legend: { display: false } } }
});
showTab('dashboard');
};
// --- EVENT HANDLERS ---
const handleCalculate = () => {
const results = calculateTaxes();
updateDashboard(results);
};
const handleNavClick = (direction) => {
const currentIndex = TABS.indexOf(currentTab);
const newIndex = direction === 'next' ? currentIndex + 1 : currentIndex - 1;
if (newIndex >= 0 && newIndex < TABS.length) {
showTab(TABS[newIndex]);
}
};
const generatePDF = () => {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content');
const pdfBtnContainer = document.getElementById('pdf-button-container');
if (!pdfContent || !pdfBtnContainer) return;
pdfBtnContainer.style.display = 'none';
html2canvas(pdfContent, { scale: 2, useCORS: true }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgWidth = pdfWidth - 20;
const imgHeight = (canvas.height * imgWidth) / canvas.width;
pdf.setFontSize(22);
pdf.setFont('helvetica', 'bold');
pdf.text(`Affiliate Tax Estimate Report (${TAX_YEAR})`, pdfWidth / 2, 15, { align: 'center' });
pdf.addImage(imgData, 'PNG', 10, 25, imgWidth, imgHeight);
pdf.save(`affiliate-tax-estimate-${TAX_YEAR}.pdf`);
pdfBtnContainer.style.display = 'block';
}).catch(err => {
console.error("Error generating PDF:", err);
pdfBtnContainer.style.display = 'block';
});
};
// --- ATTACH LISTENERS ---
Object.keys(tabButtons).forEach(key => tabButtons[key].addEventListener('click', () => showTab(key)));
prevBtn.addEventListener('click', () => handleNavClick('prev'));
nextBtn.addEventListener('click', () => handleNavClick('next'));
calculateBtn.addEventListener('click', handleCalculate);
downloadPdfBtn.addEventListener('click', generatePDF);
// --- INITIAL SETUP ---
showTab('dashboard');
});