`).join('');
}
// --- Core Logic ---
function handleGenerateRandom() {
const randomIndex = Math.floor(Math.random() * poses.length);
const pose = poses[randomIndex];
randomPoseTitle.textContent = pose.title;
randomPoseDesc.textContent = pose.description;
randomPoseCard.style.display = 'block';
}
// --- PDF Generation ---
async function downloadPDF() {
try {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const activeTab = container.querySelector('.ppil-tab-link.ppil-active');
const tabTitle = activeTab.textContent;
let posesToExport = [];
if (tabTitle === 'Pose Generator') {
if (randomPoseTitle.textContent) {
posesToExport.push({ title: randomPoseTitle.textContent, description: randomPoseDesc.textContent, category: 'Random' });
} else {
alert('Please generate a random pose first to download.');
return;
}
} else {
posesToExport = poses.filter(p => p.category === tabTitle || p.category.includes(tabTitle.split(' & ')[0]));
}
if (posesToExport.length === 0) {
alert('There are no poses in the current list to download.');
return;
}
doc.setFontSize(22);
doc.setTextColor(primaryColor);
doc.text(`Portrait Pose List: ${tabTitle}`, doc.internal.pageSize.getWidth() / 2, 20, { align: 'center' });
doc.autoTable({
startY: 30,
head: [['Pose Title', 'Description']],
body: posesToExport.map(p => [p.title, p.description]),
theme: 'grid',
headStyles: { fillColor: primaryColor, textColor: textLightColor },
styles: { cellPadding: 3, fontSize: 10 },
didDrawPage: (data) => {}
});
doc.save(`Pose_List_${tabTitle.replace(/\s/g, '_')}.pdf`);
} catch (e) {
console.error("PDF Generation Failed:", e);
alert("An error occurred generating the PDF.");
}
}
// --- Event Listeners ---
tabs.forEach((tab, index) => tab.addEventListener('click', () => showTab(index)));
nextBtn.addEventListener('click', () => showTab(currentTabIndex + 1));
prevBtn.addEventListener('click', () => showTab(currentTabIndex - 1));
generateBtn.addEventListener('click', handleGenerateRandom);
// --- PDF Dependency Loader & Listener ---
(function loadPdfDependencies() {
const scripts = [
"https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.8.2/jspdf.plugin.autotable.min.js",
];
let scriptsLoaded = 0;
downloadPdfBtn.disabled = true;
downloadPdfBtn.title = "Loading PDF libraries...";
scripts.forEach(url => {
const script = document.createElement('script');
script.src = url;
script.async = true;
script.onload = () => {
scriptsLoaded++;
if (scriptsLoaded === scripts.length) {
downloadPdfBtn.disabled = false;
downloadPdfBtn.title = "Download Current Pose List as PDF";
downloadPdfBtn.addEventListener('click', downloadPDF);
}
};
script.onerror = () => {
console.error(`Error loading script: ${url}`);
downloadPdfBtn.title = "PDF download disabled due to network error.";
};
document.head.appendChild(script);
});
})();
// --- Initial Setup ---
renderPoseList('Standing', standingList);
renderPoseList('Sitting', sittingList);
renderPoseList('Creative', creativeList);
showTab(0);
}
initializeGenerator();
})();
