        function generateComprehensiveReportCards() {
            const academicYear = document.getElementById('exam_academic_year').value;
            const term = document.getElementById('exam_term').value;
            const examId = document.getElementById('exam_exam_id').value;
            const classId = document.getElementById('exam_class_id').value;

            if (!academicYear || !term || !examId || examId === '0' || !classId || classId === '0') {
                alert('Please select all required fields: Academic Year, Term, Exam, and Class');
                return;
            }

            const generateBtn = document.getElementById('generateExamReport');
            const printBtn = document.getElementById('printExamReport');
            const downloadBtn = document.getElementById('downloadExamReport');
            const previewDiv = document.getElementById('examReportPreview');

            // Update button states
            generateBtn.innerHTML = '<i class="fas fa-spinner fa-spin" style="margin-right: 8px;"></i> Generating...';
            generateBtn.disabled = true;

            // Make AJAX request to generate report cards
            fetch('ajax/generate_report_cards.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    academic_year: academicYear,
                    term: term,
                    exam_id: examId,
                    class_id: classId,
                    school_id: <?php echo $current_user_school_id; ?>
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // Show the generated report cards in the preview
                    previewDiv.style.display = 'block';
                    previewDiv.innerHTML = data.html;

                    // Enable print and download buttons
                    printBtn.disabled = false;
                    downloadBtn.disabled = false;

                    // Set up print function
                    printBtn.onclick = () => {
                        const printWindow = window.open('', '_blank');
                        printWindow.document.write(`
                            <!DOCTYPE html>
                            <html>
                            <head>
                                <title>Report Cards</title>
                                <style>
                                    body { font-family: Arial, sans-serif; margin: 0; }
                                    .report-card { page-break-after: always; margin-bottom: 20px; }
                                    @media print { .report-card { page-break-after: always; } }
                                </style>
                            </head>
                            <body>${data.html}</body>
                            </html>
                        `);
                        printWindow.document.close();
                        printWindow.print();
                    };

                    // Set up download function
                    downloadBtn.onclick = () => {
                        // Create a downloadable HTML file
                        const blob = new Blob([`
                            <!DOCTYPE html>
                            <html>
                            <head>
                                <title>Report Cards</title>
                                <style>
                                    body { font-family: Arial, sans-serif; margin: 20px; }
                                    .report-card { page-break-after: always; margin-bottom: 20px; border: 1px solid #ccc; padding: 20px; }
                                </style>
                            </head>
                            <body>${data.html}</body>
                            </html>
                        `], { type: 'text/html' });
                        
                        const url = window.URL.createObjectURL(blob);
                        const a = document.createElement('a');
                        a.href = url;
                        a.download = `report_cards_${academicYear}_${term}_${document.getElementById('exam_class_id').selectedOptions[0].text}.html`;
                        a.click();
                        window.URL.revokeObjectURL(url);
                    };

                } else {
                    previewDiv.style.display = 'block';
                    previewDiv.innerHTML = `
                        <div style="text-align: center; color: #dc3545;">
                            <i class="fas fa-exclamation-triangle" style="font-size: 3rem; margin-bottom: 15px;"></i>
                            <h4>Error Generating Reports</h4>
                            <p>${data.message}</p>
                        </div>
                    `;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                previewDiv.style.display = 'block';
                previewDiv.innerHTML = `
                    <div style="text-align: center; color: #dc3545;">
                        <i class="fas fa-exclamation-triangle" style="font-size: 3rem; margin-bottom: 15px;"></i>
                        <h4>Network Error</h4>
                        <p>Failed to generate reports. Please try again.</p>
                    </div>
                `;
            })
            .finally(() => {
                // Restore generate button
                generateBtn.innerHTML = '<i class="fas fa-chart-line" style="margin-right: 8px;"></i> Generate Report Cards';
                generateBtn.disabled = false;
            });
        }