`;
html += '
';
for (const category in data.plan) {
html += `
`;
}
html += '
';
workoutContentDiv.innerHTML = html;
workoutPlanDiv.classList.remove('hidden');
};
// --- PDF Generation ---
const generatePdf = () => {
const { jsPDF } = window.jspdf;
if (typeof jsPDF === 'undefined' || typeof jsPDF.API.autoTable !== 'function') {
console.error("jsPDF or autoTable library not loaded correctly.");
return;
}
const selectedSport = sportSelect.value;
const selectedLevel = levelSelect.value;
const data = workoutData[selectedSport];
const doc = new jsPDF();
const pageWidth = doc.internal.pageSize.width;
let yPos = 20;
// Header
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.setTextColor(41, 128, 185); // Blue
doc.text(`${selectedSport} Workout Plan`, pageWidth / 2, yPos, { align: 'center' });
yPos += 8;
doc.setFont('helvetica', 'normal');
doc.setFontSize(12);
doc.setTextColor(127, 140, 141); // Gray
doc.text(`Fitness Level: ${selectedLevel.charAt(0).toUpperCase() + selectedLevel.slice(1)}`, pageWidth / 2, yPos, { align: 'center' });
yPos += 15;
// Description
doc.setFontSize(11);
doc.setTextColor(44, 62, 80);
const descriptionLines = doc.splitTextToSize(data.description, pageWidth - 28);
doc.text(descriptionLines, 14, yPos);
yPos += descriptionLines.length * 5 + 10;
// Workout Table
for (const category in data.plan) {
if (yPos > doc.internal.pageSize.height - 40) { // Page break check
doc.addPage();
yPos = 20;
}
const tableData = data.plan[category].map(exercise => {
let sets = typeof exercise.sets === 'string' ? exercise.sets : exercise.sets[selectedLevel];
return [exercise.name, sets];
});
doc.autoTable({
startY: yPos,
head: [[{ content: category, styles: { fillColor: [41, 128, 185], textColor: 255 } }]],
body: tableData,
theme: 'striped',
styles: { fontSize: 10 },
columnStyles: { 1: { halign: 'right', fontStyle: 'bold' } }
});
yPos = doc.autoTable.previous.finalY + 10;
}
// Footer
const pageCount = doc.internal.getNumberOfPages();
for (let i = 1; i <= pageCount; i++) {
doc.setPage(i);
doc.setFontSize(9);
doc.setTextColor(150);
doc.text('Disclaimer: Consult a professional before starting any new workout regimen.', 14, doc.internal.pageSize.height - 10);
doc.text(`Page ${i} of ${pageCount}`, pageWidth - 20, doc.internal.pageSize.height - 10);
}
doc.save(`${selectedSport}_Workout_Plan.pdf`);
};
init();
});
${category}
- `;
data.plan[category].forEach(exercise => {
let sets = typeof exercise.sets === 'string' ? exercise.sets : exercise.sets[selectedLevel];
html += `
- ${exercise.name} ${sets} `; }); html += `
