-

Generar Nuevo

+

Generar Nuevo

@@ -261,6 +261,7 @@ let catalog = []; let currentItems = []; let allBudgets = []; + let editingId = null; // null = modo crear, con valor = modo editar // ------------------ INICIALIZACIÓN Y TEMA CORPORATIVO ------------------ async function applyTheme() { @@ -295,6 +296,9 @@ // ------------------ NAVEGACIÓN Y TOASTS ------------------ function showCreateView() { + editingId = null; + document.getElementById('createViewTitle').textContent = 'Generar Nuevo'; + document.getElementById('btnSave').innerHTML = ' Generar Presupuesto'; document.getElementById('viewList').classList.add('hidden'); document.getElementById('viewCreate').classList.remove('hidden'); currentItems = []; @@ -307,6 +311,21 @@ document.getElementById('viewList').classList.remove('hidden'); } + function editBudget(id) { + const budget = allBudgets.find(b => b.id === id); + if (!budget) return showToast("Error: Presupuesto no encontrado"); + editingId = id; + document.getElementById('c_name').value = budget.client_name || ''; + document.getElementById('c_phone').value = budget.client_phone || ''; + document.getElementById('c_address').value = budget.client_address || ''; + currentItems = (budget.items || []).map(it => ({ concept: it.concept, qty: it.qty, price: it.price, total: (Number(it.qty) * Number(it.price)) })); + document.getElementById('createViewTitle').textContent = 'Editar Presupuesto #' + id; + document.getElementById('btnSave').innerHTML = ' Guardar Cambios'; + document.getElementById('viewList').classList.add('hidden'); + document.getElementById('viewCreate').classList.remove('hidden'); + renderItems(); + } + function showToast(msg) { const toast = document.getElementById('toast'); document.getElementById('toastMsg').innerText = msg; @@ -572,6 +591,7 @@ ${actionBtns}
+ ${(b.status !== 'converted' && b.status !== 'paid') ? `` : ''}
@@ -669,18 +689,20 @@ btn.innerHTML = ' Generando...'; lucide.createIcons(); + const isEdit = !!editingId; const totals = calcTotals(); const payload = { client_name: cName, client_phone: cPhone, client_address: cAddress, items: currentItems, subtotal: totals.sub, tax: totals.tax, total: totals.tot }; try { - const res = await fetch(`${API_URL}/budgets`, { - method: 'POST', + const res = await fetch(isEdit ? `${API_URL}/budgets/${editingId}` : `${API_URL}/budgets`, { + method: isEdit ? 'PUT' : 'POST', headers: { 'Content-Type': 'application/json', "Authorization": `Bearer ${localStorage.getItem("token")}` }, body: JSON.stringify(payload) }); const data = await res.json(); if(data.ok) { - showToast("¡Presupuesto Creado!"); + showToast(isEdit ? "¡Presupuesto Actualizado!" : "¡Presupuesto Creado!"); + editingId = null; hideCreateView(); fetchBudgets(); } else { showToast("Error al guardar."); }