`;
}
scheduleOutput.innerHTML = html;
}
// --- EVENT LISTENERS ---
function addEventListeners() {
tabButtons.forEach(button => {
button.addEventListener('click', () => {
currentTab = button.dataset.tab;
switchTab();
});
});
addTaskForm.addEventListener('submit', addTask);
taskListBody.addEventListener('click', (e) => {
if (e.target.classList.contains('delete-btn')) {
const taskId = parseInt(e.target.dataset.id);
deleteTask(taskId);
}
});
generateScheduleBtn.addEventListener('click', () => {
generateSchedule();
currentTab = 'dashboard';
switchTab();
});
downloadPdfBtn.addEventListener('click', generatePdf);
}
// --- UI & NAVIGATION ---
function switchTab() {
tabButtons.forEach(button => button.classList.toggle('active', button.dataset.tab === currentTab));
tabContents.forEach(content => content.classList.toggle('active', content.id.startsWith(currentTab)));
}
// --- PDF GENERATION ---
function generatePdf() {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content');
if (!pdfContent || downloadPdfBtn.disabled) return;
html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false })
.then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdf.internal.pageSize.getHeight();
while (heightLeft > 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdf.internal.pageSize.getHeight();
}
pdf.save('Optimized-Work-Shift.pdf');
})
.catch(error => {
console.error("Error generating PDF:", error);
alert("Sorry, there was an error generating the PDF.");
});
}
// --- START THE APP ---
initialize();
});
