document.addEventListener('DOMContentLoaded', function() {
const categorySelect = document.getElementById('category_id');
const headSelect = document.getElementById('head_id');
categorySelect.addEventListener('change', function() {
const categoryId = this.value;
headSelect.innerHTML = '';
fetch(`get_expense_heads.php?category_id=${categoryId}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
headSelect.innerHTML = '';
if (data.error) {
throw new Error(data.error);
}
if (data.length === 0) {
headSelect.innerHTML = '';
} else {
data.forEach(head => {
const option = document.createElement('option');
option.value = head.id;
option.textContent = head.name;
headSelect.appendChild(option);
});
}
})
.catch(error => {
console.error('Error:', error);
headSelect.innerHTML = ``;
});
});
const editLinks = document.querySelectorAll('.edit-expense');
editLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const expenseId = this.getAttribute('href').split('=')[1];
window.location.href = `edit_expense.php?id=${expenseId}`;
});
});
const deleteLinks = document.querySelectorAll('.delete-confirm');
deleteLinks.forEach(link => {
link.addEventListener('click', function(e) {
if (!confirm('Are you sure you want to delete this expense?')) {
e.preventDefault();
}
});
});
});