<?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 mt-5">
    <h2 class="text-center mb-4">
        <?= esc($title) ?> Scores for 
        <a href="#" class="text-decoration-none" data-family-student-id="<?= (int)($student['id'] ?? 0) ?>">
            <?= esc($student['firstname'] . ' ' . $student['lastname']) ?>
        </a>
    </h2>
    <?php if ($scoresLocked): ?>
        <div class="alert alert-warning text-center">
            Scores are locked for this class. Unlock to edit.
        </div>
    <?php endif; ?>

    <form action="<?= base_url('grading/update') ?>" method="post">
        <input type="hidden" name="type" value="<?= esc($type) ?>">
        <input type="hidden" name="student_id" value="<?= esc($student['id']) ?>">
        <input type="hidden" name="class_section_id" value="<?= esc($classSectionId) ?>">


        <?php if (in_array($type, ['homework', 'quiz', 'project'])): ?>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Score</th>
                        <th>Comment</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($scores as $index => $row): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td>
                                <input type="hidden" name="score_ids[]" value="<?= $row['id'] ?>">
                                <input type="number" class="form-control" name="scores[]" value="<?= $row['score'] ?>" <?= $lockAttr ?>>
                            </td>
                            <td>
                                <textarea class="form-control" name="comments[]" <?= $lockAttr ?>><?= esc($row['comment'] ?? '') ?></textarea>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php elseif (in_array($type, ['midterm', 'final', 'test'])): ?>
            <div class="form-group">
                <label>Score</label>
                <input type="number" class="form-control" name="score" value="<?= $scores[0]['score'] ?? '' ?>" <?= $lockAttr ?>>
            </div>
        <?php elseif ($type === 'comments'): ?>
            <div class="form-group">
                <label>General Comment</label>
                <textarea class="form-control" name="comment" rows="5" <?= $lockAttr ?>><?= $scores[0]['comment'] ?? '' ?></textarea>
            </div>
        <?php endif; ?>

        <div class="form-group text-center mt-3">
            <button type="submit" class="btn btn-primary" <?= $lockAttr ?>>Save Changes</button>
        </div>
    </form>
     <div class="text-center mt-4">
        <a href="<?= base_url('grading') ?>" class="btn btn-secondary">Return to Main Page</a>
    </div>
    <br>
</div>
<?= $this->endSection() ?>
