`;
}
function addTableRow(tableId) {
const table = document.getElementById(tableId);
const newRow = table.tBodies[0].insertRow();
let cellsHtml = '';
if (tableId.includes('timeSeries')) {
cellsHtml = ` `;
} else {
cellsHtml = ` `;
}
newRow.innerHTML = `${cellsHtml} `;
newRow.querySelector('.delete-row-btn').addEventListener('click', (e) => e.target.closest('tr').remove());
}
function renderChartsForTab(tabId) {
Object.values(charts).forEach(chart => { if (chart) chart.destroy(); });
if (tabId === 'dashboard-overview') {
const lineCtx = document.getElementById('revenueVsExpensesChart')?.getContext('2d');
if(lineCtx) charts.line = new Chart(lineCtx, { type: 'line', data: { labels: dashboardData.timeSeries.map(d => d.period), datasets: [{ label: 'Revenue', data: dashboardData.timeSeries.map(d => d.revenue), borderColor: '#2563eb', backgroundColor: 'rgba(37, 99, 235, 0.1)', fill: true, tension: 0.3 }, { label: 'Expenses', data: dashboardData.timeSeries.map(d => d.expenses), borderColor: '#ef4444', backgroundColor: 'rgba(239, 68, 68, 0.1)', fill: true, tension: 0.3 }] }, options: { responsive: true, maintainAspectRatio: false, plugins:{legend:{position:'top'}}, scales:{y:{ticks:{callback:v=>'$'+v/1000+'k'}}}}});
const doughnutCtx = document.getElementById('expenseBreakdownChart')?.getContext('2d');
if(doughnutCtx) charts.doughnut = new Chart(doughnutCtx, { type: 'doughnut', data: { labels: Object.keys(dashboardData.incomeStatement.opEx), datasets: [{ data: Object.values(dashboardData.incomeStatement.opEx), backgroundColor: ['#3b82f6', '#16a34a', '#f97316', '#c026d3', '#db2777'], borderWidth: 3, borderColor: '#f9fafb' }] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'bottom' }, title: { display: true, text: 'Operating Expense Breakdown' } } } });
}
}
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;
}
async function generatePDF() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'pt', 'a4');
const activeTab = tabs[currentTabIndex];
const addHeader = (title) => {
doc.setFontSize(18); doc.text('Automated Financial Report', 40, 60);
doc.setFontSize(12); doc.setTextColor(100); doc.text(`Section: ${title}`, 40, 80);
doc.setLineWidth(0.5); doc.line(40, 90, 555, 90);
};
addHeader(activeTab.name);
const tabElement = document.getElementById(activeTab.id);
if (activeTab.id === 'dashboard-overview') {
const canvas = await html2canvas(tabElement, { backgroundColor: '#ffffff', scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdfWidth = doc.internal.pageSize.getWidth() - 80;
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
doc.addImage(imgData, 'PNG', 40, 110, pdfWidth, pdfHeight);
} else {
const table = tabElement.querySelector('table');
if (table) doc.autoTable({ html: table, startY: 110, theme: 'grid' });
}
doc.save(`Financial_Report_${activeTab.name.replace(/\s/g, '_')}.pdf`);
}
init();
});
