diff --git a/presupuestos.html b/presupuestos.html index 4ad86e9..afd5815 100644 --- a/presupuestos.html +++ b/presupuestos.html @@ -455,7 +455,7 @@ async function downloadPDF(id) { } } - // ------------------ RENDERIZAR LA LISTA EN PANTALLA ------------------ + // ------------------ RENDERIZAR LA LISTA EN PANTALLA ------------------ function renderBudgetsList(budgets) { const container = document.getElementById('budgetsList'); if(budgets.length === 0) { @@ -470,6 +470,7 @@ async function downloadPDF(id) { } container.innerHTML = budgets.map(b => { + // 🔴 LÓGICA DE ESTADOS Y CADUCIDAD (Igual que en el Portal) let bDate = new Date(b.created_at); let diffDays = Math.ceil(Math.abs(new Date() - bDate) / (1000 * 60 * 60 * 24)); let isExpired = diffDays > 30; @@ -478,33 +479,27 @@ async function downloadPDF(id) { let statusText = "Pte. Resolver"; let icon = "clock"; + // Prioridad de estados if (b.status === 'paid') { - statusColor = "bg-emerald-100 text-emerald-700 border border-emerald-200"; statusText = "Pagado Online"; icon = "badge-check"; + statusColor = "bg-emerald-100 text-emerald-700 border border-emerald-200"; + statusText = "Pagado Online"; + icon = "badge-check"; } else if (isExpired && b.status !== 'rejected') { - statusColor = "bg-slate-100 text-slate-500 border border-slate-200"; statusText = "Caducado"; icon = "clock-alert"; + statusColor = "bg-slate-100 text-slate-500 border border-slate-200"; + statusText = "Caducado"; + icon = "clock-alert"; } else if (b.status === 'accepted' || b.status === 'converted') { - statusColor = "bg-blue-100 text-blue-700 border border-blue-200"; statusText = "Aceptado (Pte. Pago)"; icon = "clock"; + statusColor = "bg-blue-100 text-blue-700 border border-blue-200"; + statusText = "Aceptado (Pte. Pago)"; + icon = "clock"; } else if (b.status === 'rejected') { - statusColor = "bg-rose-100 text-rose-700"; statusText = "Rechazado"; icon = "x-circle"; + statusColor = "bg-rose-100 text-rose-700"; + statusText = "Rechazado"; + icon = "x-circle"; } const d = bDate.toLocaleDateString('es-ES', { day: '2-digit', month: 'short', year: '2-digit' }); - // LÓGICA DE BOTONES PARA EL TÉCNICO - let actionBtns = ''; - if (b.status === 'pending' && !isExpired) { - actionBtns = ` - - - `; - } else if ((b.status === 'accepted' || b.status === 'paid') && !isExpired) { - actionBtns = ``; - } else if (b.status === 'converted') { - actionBtns = `Agendado`; - } else { - actionBtns = `Cerrado`; - } - return `
Libre: No tienes nada agendado este día.
'; - return; - } - - // Ordenar por hora - dayServices.sort((a, b) => (a.scheduled_time || "23:59").localeCompare(b.scheduled_time || "23:59")); - - list.innerHTML = dayServices.map(s => { - const raw = s.raw_data || {}; - const pob = raw['Población'] || raw['POBLACION-PROVINCIA'] || raw['Dirección'] || 'Sin ubicación'; - const time = s.scheduled_time ? s.scheduled_time.substring(0,5) : 'S/H'; - return ` -No se pudo cargar la agenda.
'; - } - } catch(e) { - list.innerHTML = 'Error de conexión.
'; - } - } - - async function confirmAppointment() { - const id = document.getElementById('apptBudgetId').value; - const guild_id = document.getElementById('apptGuild').value; - const date = document.getElementById('apptDate').value; - const time = document.getElementById('apptTime').value; - - if (!guild_id || !date || !time) return showToast("⚠️ Gremio, Fecha y Hora son obligatorios."); - - const btn = document.getElementById('btnConfirmAppt'); - btn.disabled = true; - btn.innerHTML = ' Procesando...'; - lucide.createIcons(); - - // El técnico se auto-asigna al enviar assigned_to: 'self' - const payload = { - guild_id: guild_id, - date: date, - time: time, - use_automation: false, - assigned_to: 'self' - }; - - try { - const res = await fetch(`${API_URL}/budgets/${id}/convert`, { - method: 'POST', - headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, - body: JSON.stringify(payload) - }); - const data = await res.json(); - - if(data.ok) { - showToast("✅ ¡Cita agendada con éxito!"); - closeAppointmentModal(); - fetchBudgets(); - } else { - showToast("❌ " + (data.error || "Error al agendar cita.")); - } - } catch(e) { - showToast("❌ Error de conexión al convertir."); - } finally { - btn.disabled = false; - btn.innerHTML = ' Confirmar Cita'; - lucide.createIcons(); - } - }