<?php
require_once '../config/database.php';
require_once '../includes/auth_check.php';
require_once '../includes/functions.php';
header('Content-Type: text/html; charset=utf-8');

checkRole(['super_admin','school_admin']);

// ── Sauvegarde ──
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_all'])) {
    verifyCsrf();
    foreach ($_POST['thresholds'] ?? [] as $classId => $t) {
        $classId = (int)$classId;
        $pt = max(0, min(20, (float)($t['pass'] ?? 14)));
        $at = max(0, min(20, (float)($t['admit'] ?? 10)));
        $ct = max(0, min(20, (float)($t['council'] ?? 8)));
        $pdo->prepare("UPDATE classes SET pass_threshold=?, admit_threshold=?, council_threshold=? WHERE id=?")->execute([$pt, $at, $ct, $classId]);
    }
    flashMessage('success', 'Seuils mis à jour.');
    header('Location: council_thresholds.php');
    exit;
}

$classes = $pdo->query("
    SELECT c.id, c.name, c.pass_threshold, c.admit_threshold, c.council_threshold
    FROM classes c
    ORDER BY " . getClassSortSQL('c') . "
")->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Seuils décision conseil - Nukunu Mofiala</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
<div class="dashboard">
    <?php include '../includes/sidebar.php'; ?>
    <div class="main-content">
        <?php $pageTitle = '<i class="fas fa-sliders-h"></i> Seuils de décision du Conseil'; include '../includes/topbar.php'; ?>
        <?= showFlash() ?>

        <?php if (empty($classes)): ?>
        <div class="card" style="text-align:center;padding:3rem;color:#999;">
            <i class="fas fa-school" style="font-size:3rem;display:block;margin-bottom:1rem;"></i>
            Aucune classe trouvée.
        </div>
        <?php else: ?>
        <form method="POST">
            <?= csrfField() ?>
            <div class="card" style="overflow-x:auto;">
                <div class="card-header">
                    <span class="card-title">
                        <i class="fas fa-users"></i> Classes
                        <span style="font-weight:400;font-size:.82rem;color:#718096;margin-left:.5rem;">(<?= count($classes) ?>)</span>
                    </span>
                    <button type="submit" name="save_all" class="btn-submit" style="width:auto;padding:.5rem 1.5rem;">
                        <i class="fas fa-save"></i> Enregistrer tout
                    </button>
                </div>
                <div class="data-table-wrapper">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Classe</th>
                            <th style="text-align:center;width:130px;">
                                Seuil Passage (≥)<br>
                                <small style="font-weight:400;color:#718096;">Moyenne pour "Passage"</small>
                            </th>
                            <th style="text-align:center;width:130px;">
                                Seuil Admis (≥)<br>
                                <small style="font-weight:400;color:#718096;">Moyenne minimale pour "Admis(e)"</small>
                            </th>
                            <th style="text-align:center;width:130px;">
                                Seuil Conseil (≥)<br>
                                <small style="font-weight:400;color:#718096;">Moyenne pour "Passe sur Décision du Conseil"</small>
                            </th>
                            <th style="text-align:center;width:110px;">Décisions auto</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($classes as $cl):
                            $pass = (float)($cl['pass_threshold'] ?? 14);
                            $admit = (float)($cl['admit_threshold'] ?? 10);
                            $council = (float)($cl['council_threshold'] ?? 8);
                        ?>
                        <tr>
                            <td><strong><?= e($cl['name']) ?></strong></td>
                            <td style="text-align:center;">
                                <input type="number" name="thresholds[<?= $cl['id'] ?>][pass]"
                                       value="<?= e($pass) ?>" min="0" max="20" step="0.5"
                                       class="grade-input" style="width:70px;font-size:.85rem;">
                            </td>
                            <td style="text-align:center;">
                                <input type="number" name="thresholds[<?= $cl['id'] ?>][admit]"
                                       value="<?= e($admit) ?>" min="0" max="20" step="0.5"
                                       class="grade-input" style="width:70px;font-size:.85rem;">
                            </td>
                            <td style="text-align:center;">
                                <input type="number" name="thresholds[<?= $cl['id'] ?>][council]"
                                       value="<?= e($council) ?>" min="0" max="20" step="0.5"
                                       class="grade-input" style="width:70px;font-size:.85rem;">
                            </td>
                            <td style="text-align:center;font-size:.78rem;">
                                <span style="display:inline-block;padding:.15rem .5rem;border-radius:4px;background:#c6f6d5;color:#22543d;font-weight:600;">
                                    ≥<?= e($pass) ?> → Passage
                                </span>
                                <br>
                                <span style="display:inline-block;margin-top:.2rem;padding:.15rem .5rem;border-radius:4px;background:#fefcbf;color:#975a16;font-weight:600;">
                                    ≥<?= e($admit) ?> → Admis(e)
                                </span>
                                <br>
                                <span style="display:inline-block;margin-top:.2rem;padding:.15rem .5rem;border-radius:4px;background:#e9d8fd;color:#553c9a;font-weight:600;">
                                    ≥<?= e($council) ?> → Passe sur Décision Conseil
                                </span>
                                <br>
                                <span style="display:inline-block;margin-top:.2rem;padding:.15rem .5rem;border-radius:4px;background:#fed7d7;color:#9b2c2c;font-weight:600;">
                                    &lt;<?= e($council) ?> → Redouble
                                </span>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                </div>
            </div>
            <div style="text-align:right;margin-top:1rem;">
                <button type="submit" name="save_all" class="btn-submit" style="width:auto;padding:.75rem 2.5rem;font-size:1rem;">
                    <i class="fas fa-save"></i> Enregistrer tous les seuils
                </button>
            </div>
        </form>
        <?php endif; ?>
    </div>
</div>
</body>
</html>
