`;
}
function addTableRow(tableId) {
const tableBody = document.querySelector(`#${tableId} tbody`);
const newRow = tableBody.insertRow();
let cellsHtml = '';
if (tableId.includes('sales')) cellsHtml = `
| | `;
else if (tableId.includes('products')) cellsHtml = `
| | | | | `;
newRow.innerHTML = `${cellsHtml}
| `;
newRow.querySelector('.delete-row-btn').addEventListener('click', (e) => e.target.closest('tr').remove());
}
function renderChartsForTab(tabId) {
const chartOptions = {
scales: { y: { beginAtZero: true, ticks: { color: '#94a3b8', callback: (v) => '$' + (v/1000) + 'k' }, grid: { color: '#475569' } }, x: { ticks: { color: '#94a3b8' }, grid: { display: false } } },
plugins: { legend: { display: false } }
};
if (tabId === 'sales-dashboard') {
if (window.salesChartInstance) window.salesChartInstance.destroy();
const salesCtx = document.getElementById('salesChart')?.getContext('2d');
if(salesCtx) window.salesChartInstance = new Chart(salesCtx, { type: 'line', data: { labels: dashboardData.salesOverTime.map(d=>d.label), datasets: [{ data: dashboardData.salesOverTime.map(d=>d.sales), backgroundColor: 'rgba(34, 197, 94, 0.2)', borderColor: 'rgba(34, 197, 94, 1)', borderWidth: 2, tension: 0.3, fill: true }] }, options: chartOptions });
}
if (tabId === 'product-performance') {
if (window.topProductsChartInstance) window.topProductsChartInstance.destroy();
const topProductsCtx = document.getElementById('topProductsChart')?.getContext('2d');
const sortedProducts = [...dashboardData.products].sort((a,b) => b.revenue - a.revenue).slice(0, 5);
if(topProductsCtx) window.topProductsChartInstance = new Chart(topProductsCtx, { type: 'bar', data: { labels: sortedProducts.map(p=>p.name), datasets: [{ data: sortedProducts.map(p=>p.revenue), backgroundColor: ['#2563eb', '#4f46e5', '#7c3aed', '#c026d3', '#db2777'] }] }, options: { ...chartOptions, indexAxis: 'y' } });
}
if (tabId === 'inventory-reviews') {
if (window.reviewsChartInstance) window.reviewsChartInstance.destroy();
const reviewsCtx = document.getElementById('reviewsChart')?.getContext('2d');
if(reviewsCtx) window.reviewsChartInstance = new Chart(reviewsCtx, { type: 'doughnut', data: { labels: ['5 Star', '4 Star', '3 Star', '2 Star', '1 Star'], datasets: [{ data: Object.values(dashboardData.kpis.reviews).reverse(), backgroundColor: ['#16a34a', '#22c55e', '#facc15', '#f97316', '#ef4444'], borderColor: '#1e293b', borderWidth: 3 }] }, options: { plugins: { legend: { position: 'bottom', labels: { color: '#cbd5e1' } } } } });
}
}
function navigateTabs(direction) {
const newIndex = currentTabIndex + direction;
if (newIndex >= 0 && newIndex < tabs.length) showTab(newIndex);
}
function updateNavButtons() {
prevTabBtn.disabled = currentTabIndex === 0;
nextTabBtn.disabled = currentTabIndex === tabs.length - 1;
}
function generatePDF() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
const activeTab = tabs[currentTabIndex];
doc.setFontSize(18); doc.text('Amazon Data Visualization Report', 14, 22);
doc.setFontSize(12); doc.text(activeTab.name, 14, 30);
const canvas = tabContentArea.querySelector(`#${activeTab.id} canvas`);
const table = tabContentArea.querySelector(`#${activeTab.id} table`);
if (canvas && !table) { // Chart-only tabs
const chartImg = canvas.toDataURL('image/png', 1.0);
doc.addImage(chartImg, 'PNG', 15, 40, 180, 90);
} else if (table) { // Tabs with tables
doc.autoTable({ html: table, startY: 40, theme: 'grid', headStyles: { fillColor: [51, 65, 85] } });
}
doc.save(`Amazon_Dashboard_${activeTab.name.replace(/\s/g, '_')}.pdf`);
}
init();
});