const API = '/api/pendientes'; let currentDate = new Date(); let todosPendientes = []; const monthNames = ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre']; async function cargarPendientes() { const res = await fetch(API); todosPendientes = await res.json(); renderCalendar(); renderLista(); renderCompletados(); } function formatearFecha(fechaStr) { const partes = fechaStr.split('-'); return partes[2] + '-' + partes[1] + '-' + partes[0]; } function hoyStr() { const hoy = new Date(); return hoy.getFullYear() + '-' + String(hoy.getMonth() + 1).padStart(2, '0') + '-' + String(hoy.getDate()).padStart(2, '0'); } function esVencido(p) { if (p.estado === 'completado') return false; return p.fecha < hoyStr(); } function renderCalendar() { const year = currentDate.getFullYear(); const month = currentDate.getMonth(); document.getElementById('monthYear').textContent = monthNames[month] + ' ' + year; const firstDay = new Date(year, month, 1).getDay(); const daysInMonth = new Date(year, month + 1, 0).getDate(); const grid = document.getElementById('calendarGrid'); grid.innerHTML = ''; for (let i = 0; i < firstDay; i++) { grid.appendChild(document.createElement('div')); } for (let d = 1; d <= daysInMonth; d++) { const dateStr = year + '-' + String(month + 1).padStart(2, '0') + '-' + String(d).padStart(2, '0'); const delDia = todosPendientes.filter(function(p) { return p.fecha === dateStr; }); const dayEl = document.createElement('div'); const tieneVencidos = delDia.some(function(p) { return esVencido(p); }); dayEl.className = 'day' + (tieneVencidos ? ' vencido-dia' : ''); let html = '' + d + ''; if (delDia.length) { const vencidosCount = delDia.filter(function(p) { return esVencido(p); }).length; const badgeClass = vencidosCount > 0 ? 'day-count vencidos' : 'day-count'; const badgeText = delDia.length > 9 ? '9+' : String(delDia.length); html += '' + badgeText + ''; const primero = delDia[0]; const texto = escaparHtml(primero.descripcion); html += '
No hay pendientes para este dia.
'; } else { if (noCompletados.length) { const tit = document.createElement('div'); tit.className = 'subsubtitulo'; tit.textContent = 'Pendientes (' + noCompletados.length + ')'; cont.appendChild(tit); noCompletados.forEach(function(p) { cont.appendChild(crearCardPendiente(p)); }); } if (completados.length) { const titC = document.createElement('div'); titC.className = 'subsubtitulo'; titC.textContent = 'Completados (' + completados.length + ')'; cont.appendChild(titC); completados.forEach(function(p) { cont.appendChild(crearCardPendiente(p)); }); } } document.getElementById('modalDia').classList.remove('hidden'); } function crearCardPendiente(p) { const vencido = esVencido(p); const div = document.createElement('div'); div.className = 'pendiente-card ' + (vencido ? 'vencido' : p.urgencia); let html = 'No hay pendientes.
'; return; } const titTotal = document.createElement('div'); titTotal.className = 'subtitulo'; titTotal.textContent = 'Pendientes (' + noCompletados.length + ')'; cont.appendChild(titTotal); if (vencidos.length) { const titVencidos = document.createElement('div'); titVencidos.className = 'subsubtitulo'; titVencidos.textContent = 'Vencidos / Prioritarios (' + vencidos.length + ')'; cont.appendChild(titVencidos); vencidos.forEach(function(p) { cont.appendChild(crearCardPendiente(p)); }); } if (normales.length) { const titNormales = document.createElement('div'); titNormales.className = 'subsubtitulo'; titNormales.textContent = 'Pendientes (' + normales.length + ')'; cont.appendChild(titNormales); normales.forEach(function(p) { cont.appendChild(crearCardPendiente(p)); }); } } function renderCompletados() { const cont = document.getElementById('listaCompletados'); if (!cont) return; cont.innerHTML = ''; const completados = todosPendientes.filter(function(p) { return p.estado === 'completado'; }); if (!completados.length) { cont.innerHTML = 'No hay completados aun.
'; return; } const tit = document.createElement('div'); tit.className = 'subtitulo'; tit.textContent = 'Completados (' + completados.length + ')'; cont.appendChild(tit); completados.forEach(function(p) { cont.appendChild(crearCardPendiente(p)); }); } async function editarPendiente(id) { const p = todosPendientes.find(function(x) { return x.id === id; }); if (!p) return; document.getElementById('pendienteId').value = p.id; document.getElementById('descripcion').value = p.descripcion; document.getElementById('fecha').value = p.fecha; document.getElementById('estado').value = p.estado; document.getElementById('urgencia').value = p.urgencia; document.getElementById('btnGuardar').textContent = 'Actualizar'; document.getElementById('btnCancelar').classList.remove('hidden'); document.querySelector('.tab[data-tab=pendientes]').click(); } async function eliminarPendiente(id) { if (!confirm('Eliminar este pendiente?')) return; await fetch(API + '/' + id, { method: 'DELETE' }); cargarPendientes(); } cargarPendientes();