`;
goalList.appendChild(goalEl);
});
};
addGoalBtn.addEventListener('click', () => {
const title = goalTitleInput.value.trim();
const desc = goalDescInput.value.trim();
const date = goalDateInput.value;
if (!title || !desc || !date) return;
goals.push({ title, desc, date });
goalTitleInput.value = '';
goalDescInput.value = '';
goalDateInput.value = '';
renderGoals();
});
goalList.addEventListener('click', (e) => {
if (e.target.classList.contains('delete-goal-btn')) {
goals.splice(e.target.dataset.index, 1);
renderGoals();
}
});
// --- PDF DOWNLOAD LOGIC ---
downloadPdfBtn.addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'l' }); // landscape
// Title
doc.setFontSize(22);
doc.text('My Study Plan', 14, 20);
// 1. Schedule
doc.setFontSize(16);
doc.text('Weekly Schedule', 14, 35);
doc.autoTable({
html: '#schedule-table',
startY: 40,
theme: 'grid',
styles: { fontSize: 7, cellPadding: 1 },
headStyles: { fillColor: [45, 55, 72] }
});
let finalY = doc.lastAutoTable ? doc.lastAutoTable.finalY + 15 : 40;
doc.addPage();
// 2. To-Do List
doc.setFontSize(16);
doc.text('To-Do List', 14, 20);
const taskData = tasks.map(t => [t.text, t.priority, t.completed ? 'Yes' : 'No']);
if (taskData.length > 0) {
doc.autoTable({
head: [['Task', 'Priority', 'Completed']],
body: taskData,
startY: 25,
theme: 'striped'
});
finalY = doc.lastAutoTable.finalY + 15;
} else {
doc.setFontSize(10);
doc.text('No tasks added.', 14, 25);
finalY = 35;
}
// 3. Study Goals
doc.setFontSize(16);
doc.text('Study Goals', 14, finalY);
const goalData = goals.map(g => [g.title, g.desc, g.date]);
if (goalData.length > 0) {
doc.autoTable({
head: [['Goal', 'Description', 'Target Date']],
body: goalData,
startY: finalY + 5,
theme: 'striped'
});
} else {
doc.setFontSize(10);
doc.text('No goals added.', 14, finalY + 5);
}
doc.save('study-plan.pdf');
});
// --- INITIALIZATION ---
generateSchedule();
renderTasks();
renderGoals();
});