let incomes = [];
let expenses = [];

function addIncomeField() {
  const incomeFields = document.getElementById('incomeFields');
  const incomeDiv = document.createElement('div');

  const incomeInput = document.createElement('input');
  incomeInput.type = 'text';
  incomeInput.placeholder = 'Income Source';
  incomeDiv.appendChild(incomeInput);

  const amountInput = document.createElement('input');
  amountInput.type = 'number';
  amountInput.placeholder = 'Amount';
  incomeDiv.appendChild(amountInput);

  incomeFields.appendChild(incomeDiv);
}

function addExpenseField() {
  const expenseFields = document.getElementById('expenseFields');
  const expenseDiv = document.createElement('div');

  const expenseInput = document.createElement('input');
  expenseInput.type = 'text';
  expenseInput.placeholder = 'Expense';
  expenseDiv.appendChild(expenseInput);

  const amountInput = document.createElement('input');
  amountInput.type = 'number';
  amountInput.placeholder = 'Amount';
  expenseDiv.appendChild(amountInput);

  expenseFields.appendChild(expenseDiv);
}

function calculateTotalIncome() {
  let totalIncome = 0;
  for (let income of incomes) {
    totalIncome += parseFloat(income.amount || 0);
  }
  return totalIncome.toFixed(2);
}

function calculateTotalExpenses() {
  let totalExpenses = 0;
  for (let expense of expenses) {
    totalExpenses += parseFloat(expense.amount || 0);
  }
  return totalExpenses.toFixed(2);
}

function calculateSavings() {
  const totalIncome = calculateTotalIncome();
  const totalExpenses = calculateTotalExpenses();
  const savings = (totalIncome - totalExpenses).toFixed(2);
  return savings;
}

function updateSummary() {
  document.getElementById('totalIncome').textContent = calculateTotalIncome();
  document.getElementById('totalExpenses').textContent = calculateTotalExpenses();
  document.getElementById('savings').textContent = calculateSavings();
}

function generatePDF() {
  // Functionality to generate PDF with receipt-like preview
  // Include the necessary code to create a PDF preview resembling a receipt
}

updateSummary(); // Initial summary update
