${daysLeft !== null && daysLeft < 0 ? 'Overdue' : ''} ${formatDate(project.deadline)}
${daysLeft !== null && daysLeft >= 0 ? ` (${daysLeft} day${daysLeft === 1 ? '' : 's'} left)` : ''}
`
: 'No projects found matching your criteria.
';
} else {
filteredProjects.forEach(project => {
EL.grid.appendChild(createProjectCard(project));
});
}
};
// --- New Project Form ---
const handleNewProjectSubmit = (e) => {
e.preventDefault();
const newProject = {
id: generateId(),
name: EL.trackName.value.trim(),
genre: EL.genre.value.trim(),
status: EL.status.value,
deadline: EL.deadline.value,
tasks: []
};
if (newProject.name) {
projects.unshift(newProject); // Add to the beginning
saveProjects();
renderProjects();
EL.form.reset(); // Clear form
} else {
alert('Please enter a track name.');
}
};
// --- Modal Logic ---
const openProjectModal = (projectId) => {
currentProjectId = projectId;
const project = projects.find(p => p.id === projectId);
if (!project) {
console.error('Project not found:', projectId);
return;
}
EL.modalTitle.textContent = project.name;
EL.m_name.value = project.name;
EL.m_genre.value = project.genre;
EL.m_status.value = project.status;
EL.m_deadline.value = project.deadline;
renderTasks(project.tasks);
EL.modal.showModal();
};
const closeProjectModal = () => {
EL.modal.close();
currentProjectId = null;
EL.modalForm.reset();
EL.taskList.innerHTML = '';
};
const handleModalSave = (e) => {
e.preventDefault(); // Prevent default form submission inside dialog
const projectIndex = projects.findIndex(p => p.id === currentProjectId);
if (projectIndex === -1) return;
const project = projects[projectIndex];
project.name = EL.m_name.value.trim();
project.genre = EL.m_genre.value.trim();
project.status = EL.m_status.value;
project.deadline = EL.m_deadline.value;
// Tasks are updated live via other functions
if (project.name) {
saveProjects();
renderProjects(); // Re-render to update card details
closeProjectModal();
} else {
alert('Track name cannot be empty.');
}
};
const handleDeleteProject = () => {
if (!currentProjectId) return;
if (confirm(`Are you sure you want to delete "${EL.m_