`;
resultsContainer.appendChild(card);
});
noResultsDiv.classList.add('hidden');
resultsContainer.classList.remove('hidden');
downloadPdfBtn.classList.remove('hidden');
} else {
noResultsDiv.classList.remove('hidden');
resultsContainer.classList.add('hidden');
downloadPdfBtn.classList.add('hidden');
}
resultsSection.classList.remove('hidden');
// Scroll to results for better UX on mobile
resultsSection.scrollIntoView({ behavior: 'smooth' });
}
/**
* Generates and downloads a PDF of the current match results.
* This function uses the jsPDF and jsPDF-AutoTable libraries.
*/
function downloadResultsAsPDF() {
if (currentMatches.length === 0) {
alert('No results to download.');
return;
}
// Ensure jsPDF and autoTable are available
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
console.error('jsPDF or jsPDF-AutoTable is not loaded.');
alert('Could not generate PDF. A required library is missing.');
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
const userName = userNameInput.value || 'your';
// Set document title
doc.setFontSize(18);
doc.text(`Study Partner Matches for ${userName}`, 14, 22);
// Set document subtitle
doc.setFontSize(11);
doc.setTextColor(100);
doc.text(`Generated on: ${new Date().toLocaleDateString()}`, 14, 30);
// Prepare data for the table
const tableColumn = ["Name", "Matching Subjects", "Matching Availability"];
const tableRows = [];
currentMatches.forEach(match => {
const selectedSubjects = Array.from(userSubjectsSelect.selectedOptions).map(option => option.value);
const selectedAvailability = Array.from(userAvailabilityContainer.querySelectorAll('input:checked')).map(input => input.value);
const commonSubjects = match.subjects.filter(s => selectedSubjects.includes(s)).join(',\n');
const commonAvailability = match.availability.filter(a => selectedAvailability.includes(a)).join(',\n');
const matchData = [
match.name,
commonSubjects,
commonAvailability
];
tableRows.push(matchData);
});
// Create the table using autoTable
doc.autoTable({
head: [tableColumn],
body: tableRows,
startY: 40,
theme: 'grid',
headStyles: { fillColor: [79, 70, 229] }, // Indigo color for header
styles: { cellPadding: 3, fontSize: 10 },
});
// Save the PDF
doc.save(`study-group-matches-for-${userName.replace(/\s+/g, '_')}.pdf`);
}
// --- V. EVENT LISTENERS ---
// Attach event listeners, ensuring elements exist to prevent errors.
if (findMatchesBtn) {
findMatchesBtn.addEventListener('click', findAndDisplayMatches);
}
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', downloadResultsAsPDF);
}
// --- RUN INITIALIZATION ---
initializeAvailability();
});
