
new Chart(ctx2, {
    type: 'pie',
    data: {
      labels: data.transactions
        .filter(txn => txn.transactionType === 'selling')
        .map(txn => txn.items[0].name),
      datasets: [{
        label: 'Profit Margins',
        data: data.transactions
          .filter(txn => txn.transactionType === 'selling')
          .map(txn => txn.items.reduce((acc, item) => acc + item.profit, 0)),
        backgroundColor: ['#4caf50', '#f44336', '#2196f3', '#ff9800', '#9c27b0']
      }]
    }
  });


// Event listener for filter button
document.getElementById('filter-btn').addEventListener('click', fetchReportData);

// Initial fetch of report data on page load
fetchReportData();

// Export to PDF using jsPDF
document.getElementById('download-pdf-btn').addEventListener('click', function () {
  const { jsPDF } = window.jspdf;
  const doc = new jsPDF();
  doc.text('Transaction Report', 10, 10);

  // Add summary section
  doc.text(`Total Sales: $${totalSalesElem.textContent}`, 10, 20);
  doc.text(`Total Purchases: $${totalPurchasesElem.textContent}`, 10, 30);
  doc.text(`Total Profit: $${totalProfitElem.textContent}`, 10, 40);

  // Add transaction table data
  doc.autoTable({
    html: '#transaction-table',
    startY: 50
  });

  doc.save('transaction_report.pdf');
});

// Export to CSV using PapaParse
document.getElementById('download-csv-btn').addEventListener('click', function () {
  const rows = Array.from(transactionTableBody.querySelectorAll('tr')).map(row => {
    const cells = row.querySelectorAll('td');
    return Array.from(cells).map(cell => cell.textContent);
  });

  const csv = Papa.unparse({
    fields: ['Transaction ID', 'Date', 'Company/Branch', 'Transaction Type', 'Total Value', 'Profit'],
    data: rows
  });

  const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
  const link = document.createElement('a');
  const url = URL.createObjectURL(blob);
  link.setAttribute('href', url);
  link.setAttribute('download', 'transaction_report.csv');
  link.style.visibility = 'hidden';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
});
});

new Chart(ctx2, {
    type: 'pie',
    data: {
      labels: data.transactions
        .filter(txn => txn.transactionType === 'selling')
        .map(txn => txn.items[0].name),
      datasets: [{
        label: 'Profit Margins',
        data: data.transactions
          .filter(txn => txn.transactionType === 'selling')
          .map(txn => txn.items.reduce((acc, item) => acc + item.profit, 0)),
        backgroundColor: ['#4caf50', '#f44336', '#2196f3', '#ff9800', '#9c27b0']
      }]
    }
  });
}

// Event listener for filter button
document.getElementById('filter-btn').addEventListener('click', fetchReportData);

// Initial fetch of report data on page load
fetchReportData();

// Export to PDF using jsPDF
document.getElementById('download-pdf-btn').addEventListener('click', function () {
  const { jsPDF } = window.jspdf;
  const doc = new jsPDF();
  doc.text('Transaction Report', 10, 10);

  // Add summary section
  doc.text(`Total Sales: $${totalSalesElem.textContent}`, 10, 20);
  doc.text(`Total Purchases: $${totalPurchasesElem.textContent}`, 10, 30);
  doc.text(`Total Profit: $${totalProfitElem.textContent}`, 10, 40);

  // Add transaction table data
  doc.autoTable({
    html: '#transaction-table',
    startY: 50
  });

  doc.save('transaction_report.pdf');
});

// Export to CSV using PapaParse
document.getElementById('download-csv-btn').addEventListener('click', function () {
  const rows = Array.from(transactionTableBody.querySelectorAll('tr')).map(row => {
    const cells = row.querySelectorAll('td');
    return Array.from(cells).map(cell => cell.textContent);
  });

  const csv = Papa.unparse({
    fields: ['Transaction ID', 'Date', 'Company/Branch', 'Transaction Type', 'Total Value', 'Profit'],
    data: rows
  });

  const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
  const link = document.createElement('a');
  const url = URL.createObjectURL(blob);
  link.setAttribute('href', url);
  link.setAttribute('download', 'transaction_report.csv');
  link.style.visibility = 'hidden';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
});

