document.addEventListener( 'DOMContentLoaded', function () { const addBillForm = document.getElementById('addBillForm'); if (addBillForm) { addBillForm.addEventListener( 'submit', function (e) { e.preventDefault(); const formData = new FormData(this); axios.post('/api/add-bill', formData) .then( function (response) { console.log('Bill added successfully'); loadBills(); } ) .catch( function (error) { console.error('Error adding bill:', error); } ); } ); } // Load bills if on the dashboard page if (document.getElementById('billsTable')) { loadBills(); } // Event delegation for dynamically added "Mark as Paid" buttons document.addEventListener( 'click', function (e) { if (e.target && e.target.matches('.mark-bill-paid-btn')) { const billId = e.target.getAttribute('data-bill-id'); axios.post('/api/mark-bill-paid', { id: billId }) .then( function (response) { console.log('Bill marked as paid successfully'); loadBills(); } ) .catch( function (error) { console.error('Error marking bill as paid:', error); } ); } } ); } ); function loadBills() { const billsTableBody = document.querySelector('#billsTable tbody'); billsTableBody.innerHTML = 'Loading bills...'; axios.get('/api/bills') .then( function (response) { const bills = response.data; billsTableBody.innerHTML = ''; bills.forEach( function (bill) { const row = ` ${bill.description} $${parseFloat(bill.amount).toFixed(2)} ${bill.due_date} ${bill.is_paid ? '' : ''} `; billsTableBody.innerHTML += row; } ); } ) .catch( function (error) { console.error('Error loading bills:', error); billsTableBody.innerHTML = 'Error loading bills.'; } ); }