<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<div class="container my-5">
    <div class="d-flex flex-wrap align-items-center justify-content-between gap-2">
        <h3 class="text-success mb-0" style="font-family: Arial, sans-serif;">Scores</h3>
        <?php if (!empty($selectedYear)): ?>
            <div class="text-muted small">School Year: <?= esc($selectedYear) ?></div>
        <?php endif; ?>
    </div>
</div>

<!-- Scores Table -->
<?php $yearScores = $organizedScores[$selectedYear] ?? []; ?>
<?php $semesterOrder = $semesterOrder ?? ['Fall', 'Spring']; ?>
<?php
$showExamScores = $showExamScores ?? true;
$showExamScoresBySemester = $showExamScoresBySemester ?? [];
?>

<?php if (empty($yearScores)): ?>
    <div class="alert alert-info mb-3 d-inline-block">No scores available for the selected year.</div>
<?php else: ?>
    <div class="accordion mt-4" id="parentScoresAccordion">
        <?php foreach ($semesterOrder as $index => $semester): ?>
            <?php
                $semesterScores = $yearScores[$semester] ?? [];
                $showExamScoresForSemester = $showExamScoresBySemester[$semester] ?? $showExamScores;
                $columnCount = $showExamScoresForSemester ? 11 : 8;
                $headingId = 'heading' . strtolower($semester);
                $collapseId = 'collapse' . strtolower($semester);
                $isActive = $index === 0;
            ?>
            <div class="accordion-item">
                <h2 class="accordion-header" id="<?= esc($headingId) ?>">
                    <button class="accordion-button <?= $isActive ? '' : 'collapsed' ?>" type="button"
                            data-bs-toggle="collapse" data-bs-target="#<?= esc($collapseId) ?>"
                            aria-expanded="<?= $isActive ? 'true' : 'false' ?>" aria-controls="<?= esc($collapseId) ?>">
                        <?= esc($semester) ?> Semester
                    </button>
                </h2>
                <div id="<?= esc($collapseId) ?>" class="accordion-collapse collapse <?= $isActive ? 'show' : '' ?>"
                     aria-labelledby="<?= esc($headingId) ?>" data-bs-parent="#parentScoresAccordion">
                    <div class="accordion-body p-0">
                        <?php if (!empty($semesterScores)): ?>
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered mb-0">
                                    <thead>
                                        <tr>
                                            <th>School ID</th>
                                            <th>Student Name</th>
                                            <th>Class Section</th>
                                            <th>Homework</th>
                                            <th>Project</th>
                                            <th>Participation</th>
                                            <th>Quiz</th>
                                            <th>Attendance</th>                                            
                                            <?php if ($showExamScoresForSemester): ?><th>PTAP</th><?php endif; ?>
                                            <?php if ($semester === 'Fall'): ?>
                                                <?php if ($showExamScoresForSemester): ?><th>Midterm</th><?php endif; ?>
                                            <?php elseif ($semester === 'Spring'): ?>
                                                <?php if ($showExamScoresForSemester): ?><th>Final Exam</th><?php endif; ?>
                                            <?php else: ?>
                                                <?php if ($showExamScoresForSemester): ?><th>Exam</th><?php endif; ?>
                                            <?php endif; ?>
                                            <?php if ($showExamScoresForSemester): ?><th>Semester Score</th><?php endif; ?>
                                            <?php if ($showExamScoresForSemester): ?><th>Report Card</th><?php endif; ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($semesterScores as $student): ?>
                                            <tr>
                                                <td><?= esc($student['school_id']) ?></td>
                                                <td><?= esc($student['student_firstname'] . ' ' . $student['student_lastname']) ?></td>
                                                <td><?= esc($student['class_section_name']) ?></td>
                                                <td><?= esc($student['scores']['homework']['score'] ?? '-') ?></td>
                                                <td><?= esc($student['scores']['project']['score'] ?? '-') ?></td>
                                                <td><?= esc($student['scores']['participation_score']['score'] ?? '-') ?></td>
                                                <td><?= esc($student['scores']['quiz']['score'] ?? '-') ?></td>
                                                <td><?= esc($student['scores']['attendance']['score'] ?? '-') ?></td>
                                                <?php if ($showExamScoresForSemester): ?><td><?= esc($student['scores']['ptap']['score'] ?? '-') ?></td><?php endif; ?>
                                               
                                                <?php if ($semester === 'Fall'): ?>
                                                    <?php if ($showExamScoresForSemester): ?><td><?= esc($student['scores']['midterm_exam']['score'] ?? '-') ?></td><?php endif; ?>
                                                <?php elseif ($semester === 'Spring'): ?>
                                                    <?php if ($showExamScoresForSemester): ?><td><?= esc($student['scores']['final_exam']['score'] ?? '-') ?></td><?php endif; ?>
                                                <?php else: ?>
                                                    <?php if ($showExamScoresForSemester): ?><td><?= esc($student['scores']['midterm_exam']['score'] ?? $student['scores']['final_exam']['score'] ?? '-') ?></td><?php endif; ?>
                                                <?php endif; ?>
                                                <?php if ($showExamScoresForSemester): ?><td><?= esc($student['scores']['semester']['score'] ?? '-') ?></td><?php endif; ?>
                                                <?php if ($showExamScoresForSemester): ?>
                                                    <td>
                                                        <?php
                                                            $reportStudentId = (int) ($student['student_id'] ?? 0);
                                                            if ($reportStudentId > 0) {
                                                                $reportSemester = (string) ($semester ?? '');
                                                                $reportDate = date('Y-m-d');
                                                                $reportUrl = base_url('parent/report-cards/view/' . $reportStudentId);
                                                                $query = http_build_query([
                                                                    'semester' => $reportSemester,
                                                                    'report_date' => $reportDate,
                                                                ]);
                                                                if ($query) {
                                                                    $reportUrl .= '?' . $query;
                                                                }
                                                            }
                                                        ?>
                                                        <?php if ($reportStudentId > 0): ?>
                                                            <a class="btn btn-sm btn-outline-primary" href="<?= esc($reportUrl) ?>" target="_blank" rel="noopener">View</a>
                                                        <?php else: ?>
                                                            <span class="text-muted">Unavailable</span>
                                                        <?php endif; ?>
                                                    </td>
                                                <?php endif; ?>
                                            </tr>
                                            <?php if (!empty($student['comment'])): ?>
                                                <tr>
                                                    <td colspan="<?= esc($columnCount) ?>"><strong>Teacher Comment:</strong> <?= esc($student['comment']) ?></td>
                                                </tr>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-secondary mb-0">No <?= esc($semester) ?> semester scores recorded yet.</div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
</main>
</div>
</div>
<?= $this->endSection() ?>
