document.addEventListener('DOMContentLoaded', function () { const overrideBtn = document.getElementById('override-btn'); if (overrideBtn) { overrideBtn.addEventListener('click', function () { const projectId = this.dataset.projectId; const month = this.dataset.month; if (this.textContent.trim() === 'Override') { this.textContent = 'Save'; this.classList.remove('btn-warning'); this.classList.add('btn-success'); makeEditable('WIP', month); makeEditable('Opening-Balance', month); makeEditable('Billings', month); makeEditable('Expenses', month); } else if (this.textContent.trim() === 'Save') { const wip = document.getElementById(`wip-${month}-input`).value; const openingBalance = document.getElementById(`opening-balance-${month}-input`).value; const billings = document.getElementById(`billings-${month}-input`).value; const expenses = document.getElementById(`expenses-${month}-input`).value; const formData = new FormData(); formData.append('projectId', projectId); formData.append('month', month); formData.append('wip', wip); formData.append('openingBalance', openingBalance); formData.append('billings', billings); formData.append('expenses', expenses); fetch('save_override.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { this.textContent = 'Overridden'; this.classList.remove('btn-success'); this.classList.add('btn-secondary'); this.disabled = true; updateCell('WIP', month, wip); updateCell('Opening-Balance', month, openingBalance); updateCell('Billings', month, billings); updateCell('Expenses', month, expenses); } else { alert('Error saving override: ' + data.error); } }) .catch(error => { console.error('Error:', error); alert('An unexpected error occurred.'); }); } }); } function makeEditable(metric, month) { const cell = document.getElementById(`${metric.toLowerCase().replace(/\s/g, '-')}-${month}`); if (cell) { const value = cell.textContent.replace(/€/g, '').replace(/,/g, ''); cell.innerHTML = ``; } } function updateCell(metric, month, value) { const cell = document.getElementById(`${metric.toLowerCase().replace(/\s/g, '-')}-${month}`); if (cell) { cell.innerHTML = `€${parseFloat(value).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } } });