async function renderGoals() { const content = document.getElementById('pageContent'); content.innerHTML = `
0
Total Goals
0
Active
0
Completed
0%
Avg Progress
`; try { const data = await api.getGoals(); const goals = data.goals || []; const list = document.getElementById('goalList'); const totalEl = document.getElementById('goalTotalCount'); const activeEl = document.getElementById('goalActiveCount'); const completeEl = document.getElementById('goalCompleteCount'); const avgEl = document.getElementById('goalAvgProgress'); if (!list) return; if (goals.length === 0) { list.innerHTML = `
🎯
No goals yet
Create your first goal to start tracking progress
`; if (totalEl) totalEl.textContent = '0'; if (activeEl) activeEl.textContent = '0'; if (completeEl) completeEl.textContent = '0'; if (avgEl) avgEl.textContent = '0%'; return; } const active = goals.filter(g => g.status === 'active').length; const done = goals.filter(g => g.status === 'completed').length; const avgProg = Math.round(goals.reduce((s, g) => s + (g.progress || 0), 0) / goals.length); if (totalEl) totalEl.textContent = goals.length; if (activeEl) activeEl.textContent = active; if (completeEl) completeEl.textContent = done; if (avgEl) avgEl.textContent = avgProg + '%'; list.innerHTML = goals.map(g => `
${g.status} ${g.category} ${g.target_date ? `🎯 ${g.target_date}` : ''}
${escapeHtml(g.title)}
${g.description ? `
${escapeHtml(g.description)}
` : ''}
${g.progress || 0}%
${g.status !== 'completed' ? `` : ''}
`).join(''); } catch (err) { showToast('Failed to load goals: ' + err.message, 'error'); } } function showCreateGoalModal() { const modal = document.getElementById('modalContainer'); modal.innerHTML = ` `; } async function createGoal() { const title = document.getElementById('goalTitle').value.trim(); if (!title) { showToast('Title is required', 'error'); return; } try { await api.createGoal({ title, description: document.getElementById('goalDesc').value.trim(), category: document.getElementById('goalCategory').value, target_date: document.getElementById('goalDate').value, }); showToast('Goal created!', 'success'); closeModal(); renderGoals(); } catch (err) { showToast('Failed to create goal: ' + err.message, 'error'); } } async function updateGoalProgress(id, progress) { try { const status = progress >= 100 ? 'completed' : 'active'; await api.updateGoal(id, { progress, status }); renderGoals(); } catch (err) { showToast('Failed to update goal: ' + err.message, 'error'); } } async function completeGoal(id) { try { await api.updateGoal(id, { progress: 100, status: 'completed' }); showToast('Goal completed! 🎉', 'success'); renderGoals(); } catch (err) { showToast('Failed to complete goal: ' + err.message, 'error'); } } async function deleteGoal(id) { if (!confirm('Delete this goal?')) return; try { await api.deleteGoal(id); showToast('Goal deleted', 'info'); renderGoals(); } catch (err) { showToast('Failed to delete goal: ' + err.message, 'error'); } }