| Task Description |
Status |
Notes |
${tasks.map(task => `
| ${task.description} |
${task.status} |
${task.notes || '-'} |
`).join('')}
`;
reportPreviewOutput.innerHTML = reportHTML;
reportPreviewContainer.style.display = 'block';
downloadPdfBtn.style.display = 'inline-block';
// Scroll to the preview
reportPreviewContainer.scrollIntoView({ behavior: 'smooth' });
};
/**
* Generates and triggers the download of a PDF report.
*/
const generatePDF = () => {
if (typeof jspdf === 'undefined' || !jspdf.jsPDF) {
alert('PDF generation library is not loaded.'); return;
}
const employeeName = employeeNameInput.value.trim() || 'N/A';
const reportDate = reportDateInput.value ? new Date(reportDateInput.value).toLocaleDateString('en-US', { timeZone: 'UTC' }) : 'N/A';
const tasks = [];
const rows = taskListBody.querySelectorAll('tr');
rows.forEach(row => {
const description = row.querySelector('.task-description').value.trim();
const status = row.querySelector('.task-status').value;
const notes = row.querySelector('.task-notes').value.trim();
if (description) {
tasks.push([description, status, notes || '-']);
}
});
if (tasks.length === 0) {
alert("Cannot generate PDF. Please add at least one task.");
return;
}
const { jsPDF } = jspdf;
const doc = new jsPDF();
// --- PDF Content ---
doc.setFontSize(20);
doc.setTextColor('#1f2937'); // gray-800
doc.text('Daily Productivity Report', 105, 20, { align: 'center' });
doc.setFontSize(12);
doc.setTextColor('#4b5563'); // gray-600
doc.text(`Employee: ${employeeName}`, 14, 35);
doc.text(`Date: ${reportDate}`, 14, 42);
doc.autoTable({
startY: 50,
head: [['Task Description', 'Status', 'Notes / Comments']],
body: tasks,
theme: 'grid',
headStyles: {
fillColor: '#4f46e5', // indigo-600
textColor: '#ffffff'
},
styles: {
fontSize: 10,
cellPadding: 3
},
columnStyles: {
0: { cellWidth: 80 },
1: { cellWidth: 30 },
2: { cellWidth: 'auto' }
}
});
doc.save(`Productivity-Report-${employeeName.replace(/\s/g, '_')}-${reportDateInput.value}.pdf`);
};
// --- Event Listeners ---
if (addTaskBtn) {
addTaskBtn.addEventListener('click', addTaskRow);
}
if (taskListBody) {
taskListBody.addEventListener('click', (e) => {
if (e.target.classList.contains('remove-task-btn')) {
removeTaskRow(e.target);
}
});
}
if (generateReportBtn) {
generateReportBtn.addEventListener('click', generateReportPreview);
}
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', generatePDF);
}
// --- Initial Setup ---
const today = new Date().toISOString().split('T')[0];
if (reportDateInput) {
reportDateInput.value = today;
}
addInitialTaskRows(3); // Start with 3 empty rows
});