<?php
declare(strict_types=1);
require_once __DIR__ . '/../library/auth.php';
require_once __DIR__ . '/helpers/result-helper.php';
require_once __DIR__ . '/services/ResultPublishService.php';

allow_roles(['admin']);

$school_id = (int) $_SESSION['school_id'];
$page_title = 'Publish Results';

// Fetch Exams
$examStmt = $pdo->prepare("SELECT id, exam_name FROM exam_types WHERE school_id = ? AND status = 'Active' ORDER BY display_order 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");
$classStmt->execute([$school_id]);
$classes = $classStmt->fetchAll(PDO::FETCH_ASSOC);

require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';
?>

<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid"><h1>Publish Results</h1></div>
    </section>

    <section class="content">
        <div class="container-fluid">
            <div class="card card-outline card-primary">
                <div class="card-header"><h3 class="card-title">Manage Result Visibility</h3></div>
                <form id="publishResultForm" method="POST">
                    <?= csrfField(); ?>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-4">
                                <label>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 $e): ?>
                                        <option value="<?= (int)$e['id']; ?>"><?= htmlspecialchars($e['exam_name']); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label>Class</label>
                                <select name="class_id" id="class_id" class="form-control select2" required>
                                    <option value="">Select Class</option>
                                    <?php foreach ($classes as $c): ?>
                                        <option value="<?= (int)$c['id']; ?>"><?= htmlspecialchars($c['class_name']); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-4 d-flex align-items-end">
                                <button type="button" id="publishResultsBtn" class="btn btn-success mr-2"><i class="fas fa-upload"></i> Publish</button>
                                <button type="button" id="unpublishResultsBtn" class="btn btn-danger"><i class="fas fa-times"></i> Unpublish</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

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

<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

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

<script src="assets/js/result-publish.js"></script>

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