<?php
// views/grading/homework.php, quiz.php, project.php, test.php, midterm.php, final.php, comments.php
// Use this as a shared structure but each type will set its own unique variables below

// Set title based on the view file
$viewFile = basename(__FILE__, '.php');

switch ($viewFile) {
    case 'homework':
        $type = 'homework';
        $title = 'Homework';
        break;
    case 'quiz':
        $type = 'quiz';
        $title = 'Quiz';
        break;
    case 'project':
        $type = 'project';
        $title = 'Project';
        break;
    case 'test':
        $type = 'test';
        $title = 'Test';
        break;
    case 'midterm':
        $type = 'midterm';
        $title = 'Midterm Exam';
        break;
    case 'final':
        $type = 'final';
        $title = 'Final Exam';
        break;
    case 'comments':
        $type = 'comments';
        $title = 'General Comments';
        break;
    default:
        $type = 'homework';
        $title = 'Homework';
}

$scoresLocked = !empty($scoresLocked);
$lockAttr = $scoresLocked ? 'disabled' : '';
?>

<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-xxl py-5">
    <div class="container">
        <div class="text-center mx-auto mb-5 wow fadeInUp" data-wow-delay="0.1s" style="max-width: 600px;">
            <br>
            <h2 class="text-dark" style="font-family: Arial, sans-serif;">Final Exam Scores</h2>
        </div>

        <?php if (session()->get('status')): ?>
            <div class="alert alert-success">
                <?= session()->get('status') ?>
            </div>
        <?php endif; ?>
        <?php if ($scoresLocked): ?>
            <div class="alert alert-warning">
                Scores are locked for this class. Unlock to edit.
            </div>
        <?php endif; ?>
<div class="d-flex justify-content-between mt-4 flex-wrap gap-2">
                <button type="submit" class="btn btn-success" <?= $lockAttr ?>>
                    <i class="bi bi-save"></i> Save Changes
                </button>
                <a href="<?= base_url('/grading') ?>" class="btn btn-secondary">
                    <i class="bi bi-x-circle"></i> Back to Scores
                </a>
            </div>
        <form action="<?= base_url('grading/updateFinalExamScores') ?>" method="post"
            onsubmit="return validateForm()">
            <?= csrf_field() ?>

            <input type="hidden" name="semester" value="<?= esc($semester) ?>">
            <input type="hidden" name="school_year" value="<?= esc($schoolYear) ?>">

            <table class="table table-bordered mt-4" data-no-mgmt-sticky>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>School ID</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th class="text-center">Final Exam Score</th>

                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($students as $index => $student): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= esc($student['school_id']) ?></td>
                            <td>
                                <a href="#" class="text-decoration-none" data-family-student-id="<?= (int)($student['student_id'] ?? 0) ?>">
                                    <?= esc($student['firstname']) ?>
                                </a>
                            </td>
                            <td>
                                <a href="#" class="text-decoration-none" data-family-student-id="<?= (int)($student['student_id'] ?? 0) ?>">
                                    <?= esc($student['lastname']) ?>
                                </a>
                            </td>
                            <td>
                            <input type="number" name="final_score[<?= esc($student['student_id']) ?>][score]"
                                value="<?= esc($student['score'] ?? '') ?>" class="form-control text-center" min="0" max="100" step="0.01"
                                placeholder="Enter score (optional)" <?= $lockAttr ?>>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>

            <div class="d-flex justify-content-between mt-4 flex-wrap gap-2">
                <button type="submit" class="btn btn-success" <?= $lockAttr ?>>
                    <i class="bi bi-save"></i> Save Changes
                </button>
                <a href="<?= base_url('/grading') ?>" class="btn btn-secondary">
                    <i class="bi bi-x-circle"></i> Back to Scores
                </a>
            </div>
        </form>

        <br>
    </div>
<?= $this->endSection() ?>
