<?php

declare(strict_types=1);

require_once __DIR__ . '/../library/auth.php';
require_once __DIR__ . '/helpers/result-helper.php';

allow_roles(['admin', 'teacher']);

$school_id = (int) $_SESSION['school_id'];
$session_id = (int) ($_SESSION['session_id'] ?? 0);
$page_title = 'Marks Entry';

/*
|--------------------------------------------------------------------------
| FETCH ACTIVE SESSION
|--------------------------------------------------------------------------
*/
$sessionStmt = $pdo->prepare("
    SELECT id, session_name
    FROM academic_sessions
    WHERE school_id = ?
    AND id = ?
    LIMIT 1
");
$sessionStmt->execute([$school_id, $session_id]);
$currentSession = $sessionStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH EXAMS
|--------------------------------------------------------------------------
*/
$examStmt = $pdo->prepare("
    SELECT id, exam_name
    FROM exam_types
    WHERE school_id = ?
    AND status = 'Active'
    ORDER BY display_order ASC, exam_name ASC
");
$examStmt->execute([$school_id]);
$examTypes = $examStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH CLASSES
|--------------------------------------------------------------------------
*/
$classStmt = $pdo->prepare("
    SELECT id, class_name
    FROM classes
    WHERE school_id = ?
    ORDER BY sort_order ASC, class_name ASC
");
$classStmt->execute([$school_id]);
$classes = $classStmt->fetchAll(PDO::FETCH_ASSOC);

// Load main application headers safely
require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';
?>

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap4.min.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.5.0/css/responsive.bootstrap4.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" />

<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Marks Entry</h1>
                </div>
            </div>
        </div>
    </section>

    <section class="content">
        <div class="container-fluid">
            
            <div class="card card-outline card-primary">
                <div class="card-header">
                    <h3 class="card-title">Select Exam & Class</h3>
                </div>
 
                <form id="marksEntryFilterForm" method="POST">
                    <?= csrfField(); ?>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-4 mb-3">
                                <label for="exam_type_id">Exam Type</label>
                                <select name="exam_type_id" id="exam_type_id" class="form-control select2" required>
                                    <option value="">Select Exam</option>
                                    <?php foreach ($examTypes as $exam): ?>
                                        <option value="<?= (int) $exam['id']; ?>">
                                            <?= htmlspecialchars($exam['exam_name'], ENT_QUOTES, 'UTF-8'); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>

                            <div class="col-md-4 mb-3">
                                <label for="class_id">Class</label>
                                <select name="class_id" id="class_id" class="form-control select2" required>
                                    <option value="">Select Class</option>
                                    <?php foreach ($classes as $class): ?>
                                        <option value="<?= (int) $class['id']; ?>">
                                            <?= htmlspecialchars($class['class_name'], ENT_QUOTES, 'UTF-8'); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>

                            <div class="col-md-4 mb-3">
                                <label>Academic Session</label>
                                <input type="text" class="form-control" value="<?= htmlspecialchars($currentSession['session_name'] ?? 'N/A', ENT_QUOTES, 'UTF-8'); ?>" readonly>
                            </div>
                        </div>
                    </div>

                    <div class="card-footer text-right">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search mr-1"></i> Load Students
                        </button>
                    </div>
                </form>
            </div>

            <div id="studentsContainer" style="display:none;">
                <div class="card card-outline card-info">
                    <div class="card-header">
                        <h3 class="card-title">Student List</h3>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="studentsTable" class="table table-bordered table-striped w-100">
                                <thead>
                                    <tr>
                                        <th width="10%">Roll No</th>
                                        <th width="15%" class="text-center">Photo</th>
                                        <th>Student Name</th>
                                        <th width="15%" class="text-center">Action</th>
                                    </tr>
                                </thead>
                                <tbody id="studentsTableBody">
                                    </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </section>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

<script src="https://cdn.datatables.net/1.13.8/js/jquery.dataTables.min.js"></script>

<script src="https://cdn.datatables.net/1.13.8/js/dataTables.bootstrap4.min.js"></script>

<script src="assets/js/marks-entry.js"></script>

<script src="assets/js/marks-form.js"></script>

<?php require_once __DIR__ . '/../include/footer.php'; ?>