<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Activation des Contrôles par Classe</title>
<link rel="stylesheet" href="w3.css">
<style>
body {width:90%; margin:auto;}
table {width:100%; border-collapse: collapse;}
th, td {padding:8px; border-bottom:1px solid #ccc;}
.w3-btn {margin:0 2px;}
h3 {margin-top:30px;}
.w3-panel {margin-top:10px;}
</style>
<script>
window.addEventListener('load', function() {
    var params = new URLSearchParams(window.location.search);
    var classID = params.get('classID');
    if (classID) {
        var el = document.getElementById('class-' + classID);
        if (el) { el.scrollIntoView({behavior: 'smooth', block: 'start'}); }
    }
});
</script>
</head>
<body>

<div class="w3-card-4 w3-padding">
<h2 class="w3-text-teal">Activation / Désactivation des Contrôles par Classe</h2>

<?php
include 'header.php';
include '_serveur.php';
mysqli_set_charset($link, "utf8");

// === HANDLE INDIVIDUAL TOGGLE ===
if (isset($_GET['toggle']) && isset($_GET['classID']) && isset($_GET['semID']) && isset($_GET['type'])) {
    $classID = intval($_GET['classID']);
    $semID = intval($_GET['semID']);
    $type = $_GET['type']; // 'teacher' or 'parent'
    $col = ($type === 'teacher') ? 'teachersActive' : 'parentsActive';

    $getVal = mysqli_query($link, "SELECT $col FROM classesSemesters WHERE classID='$classID' AND semesterID='$semID'");
    $cur = mysqli_fetch_assoc($getVal)[$col];
    $newVal = $cur ? 0 : 1;

    $update = "UPDATE classesSemesters SET $col='$newVal' WHERE classID='$classID' AND semesterID='$semID'";
    if (mysqli_query($link, $update)) {
        $msg = ($newVal) ? 'activé' : 'désactivé';
        echo '<div class="w3-panel w3-green"><b>Contrôle '.$msg.' ('.($type==='teacher'?'professeurs':'parents').').</b></div>';
    } else {
        echo '<div class="w3-panel w3-red"><b>Erreur: '.mysqli_error($link).'</b></div>';
    }
}

// === HANDLE BULK ACTIONS ===
if (isset($_GET['bulk']) && isset($_GET['classID']) && isset($_GET['type']) && isset($_GET['value'])) {
    $classID = intval($_GET['classID']);
    $type = $_GET['type']; // teacher, parent, both
    $value = ($_GET['value'] == '1') ? 1 : 0;
    $msg = ($value == 1) ? 'activés' : 'désactivés';

    if ($type === 'teacher') {
        $sql = "UPDATE classesSemesters SET teachersActive='$value' WHERE classID='$classID'";
    } elseif ($type === 'parent') {
        $sql = "UPDATE classesSemesters SET parentsActive='$value' WHERE classID='$classID'";
    } else {
        $sql = "UPDATE classesSemesters SET teachersActive='$value', parentsActive='$value' WHERE classID='$classID'";
    }

    if (mysqli_query($link, $sql)) {
        echo '<div class="w3-panel w3-green"><b>Tous les contrôles '.$msg.' pour '.htmlspecialchars($type).'.</b></div>';
    } else {
        echo '<div class="w3-panel w3-red"><b>Erreur: '.mysqli_error($link).'</b></div>';
    }
}

// === FETCH ALL CLASSES ===
$sqlClasses = "SELECT id, name FROM classes ORDER BY name ASC";
$resClasses = mysqli_query($link, $sqlClasses) or die("Erreur SQL classes: " . mysqli_error($link));

if (mysqli_num_rows($resClasses) > 0) {
    while ($class = mysqli_fetch_assoc($resClasses)) {
        $classID = $class['id'];
        echo '<h3 id="class-'.$classID.'" class="w3-text-blue">'.htmlspecialchars($class['name']).'</h3>';

        // === Bulk action buttons ===
        echo '<div class="w3-margin-bottom">
                <a href="?bulk=1&classID='.$classID.'&type=teacher&value=1" class="w3-button w3-green w3-small">Activer tous (Professeurs)</a>
                <a href="?bulk=1&classID='.$classID.'&type=teacher&value=0" class="w3-button w3-amber w3-small">Désactiver tous (Professeurs)</a>
                <a href="?bulk=1&classID='.$classID.'&type=parent&value=1" class="w3-button w3-green w3-small">Activer tous (Parents)</a>
                <a href="?bulk=1&classID='.$classID.'&type=parent&value=0" class="w3-button w3-amber w3-small">Désactiver tous (Parents)</a>
                <a href="?bulk=1&classID='.$classID.'&type=both&value=1" class="w3-button w3-blue w3-small">Activer tous (Prof + Parents)</a>
                <a href="?bulk=1&classID='.$classID.'&type=both&value=0" class="w3-button w3-red w3-small">Désactiver tous (Prof + Parents)</a>
              </div>';

        // === Fetch all semesters for the class ===
        $sql = "SELECT cs.semesterID, s.name AS semName, cs.teachersActive, cs.parentsActive
                FROM classesSemesters cs
                JOIN semesters s ON cs.semesterID = s.id
                WHERE cs.classID = '$classID'
                ORDER BY s.id ASC";
        $res = mysqli_query($link, $sql);

        if (mysqli_num_rows($res) > 0) {
            echo '<table class="w3-table-all w3-hoverable">';
            echo '<tr class="w3-light-grey">
                    <th>Contrôle</th>
                    <th>Professeurs actifs</th>
                    <th>Parents actifs</th>
                    <th>Actions</th>
                  </tr>';
            while ($row = mysqli_fetch_assoc($res)) {
                $teacherBtnClass = $row['teachersActive'] ? 'w3-amber' : 'w3-green';
                $parentBtnClass  = $row['parentsActive'] ? 'w3-amber' : 'w3-green';
                $teacherLabel = $row['teachersActive'] ? 'Désactiver professeurs' : 'Activer professeurs';
                $parentLabel  = $row['parentsActive'] ? 'Désactiver parents' : 'Activer parents';

                echo '<tr>';
                echo '<td>'.htmlspecialchars($row['semName']).'</td>';
                echo '<td>'.($row['teachersActive'] ? '<b>Oui</b>' : 'Non').'</td>';
                echo '<td>'.($row['parentsActive'] ? '<b>Oui</b>' : 'Non').'</td>';
                echo '<td>
                        <a href="?toggle=1&classID='.$classID.'&semID='.$row['semesterID'].'&type=teacher" 
                           class="w3-button '.$teacherBtnClass.' w3-small">'.$teacherLabel.'</a>
                        <a href="?toggle=1&classID='.$classID.'&semID='.$row['semesterID'].'&type=parent" 
                           class="w3-button '.$parentBtnClass.' w3-small">'.$parentLabel.'</a>
                      </td>';
                echo '</tr>';
            }
            echo '</table>';
        } else {
            echo '<p><i>Aucun contrôle pour cette classe.</i></p>';
        }
    }
} else {
    echo '<div class="w3-panel w3-yellow"><b>Aucune classe trouvée.</b></div>';
}
?>
</div>

</body>
</html>
