<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="w3.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600;700&family=Source+Serif+4:ital,wght@0,300;0,400;0,600;1,400&display=swap" rel="stylesheet">
<title>Réservation des Matières</title>
<style>
:root {
    --navy:   #1a2744;
    --gold:   #c9a84c;
    --cream:  #faf8f3;
    --light:  #f0ece0;
    --text:   #1c1c1c;
    --muted:  #6b6356;
    --green:  #2d6a4f;
    --red:    #9b2335;
    --border: #d4c9a8;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    background: var(--cream);
    font-family: 'Source Serif 4', Georgia, serif;
    font-size: 14px;
    color: var(--text);
    min-height: 100vh;
}

/* ---- Top bar ---- */
.top-bar {
    background: var(--navy);
    padding: 18px 36px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.25);
}
.top-bar h1 {
    font-family: 'Playfair Display', serif;
    color: var(--gold);
    font-size: 22px;
    letter-spacing: 1px;
}
.top-bar .subtitle {
    color: rgba(255,255,255,0.55);
    font-size: 12px;
    letter-spacing: 2px;
    text-transform: uppercase;
    margin-top: 3px;
}

/* ---- Page wrapper ---- */
.page-wrap {
    max-width: 860px;
    margin: 36px auto;
    padding: 0 20px 60px;
}

/* ---- Card ---- */
.card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 2px;
    box-shadow: 0 2px 16px rgba(0,0,0,0.07);
    overflow: hidden;
}
.card::before {
    content: '';
    display: block;
    height: 5px;
    background: linear-gradient(90deg, var(--navy) 0%, var(--gold) 50%, var(--navy) 100%);
}
.card-body { padding: 28px 32px 32px; }

/* ---- Section titles ---- */
.section-label {
    font-family: 'Playfair Display', serif;
    font-size: 13px;
    color: var(--navy);
    text-transform: uppercase;
    letter-spacing: 1.5px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 8px;
    margin-bottom: 16px;
    margin-top: 28px;
}
.section-label:first-of-type { margin-top: 0; }

/* ---- Form controls ---- */
.form-select {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--cream);
    font-family: 'Source Serif 4', Georgia, serif;
    font-size: 13px;
    color: var(--text);
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%236b6356' d='M6 8L0 0h12z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    cursor: pointer;
    transition: border-color 0.2s;
}
.form-select:focus { outline: none; border-color: var(--gold); }

/* ---- Checkbox / Radio grid ---- */
.check-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 10px;
}
.check-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 14px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--cream);
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
    font-size: 13px;
}
.check-item:hover { border-color: var(--gold); background: #fdf9ee; }
.check-item input[type="checkbox"],
.check-item input[type="radio"] {
    accent-color: var(--navy);
    width: 15px;
    height: 15px;
    cursor: pointer;
    flex-shrink: 0;
}
.check-item.selected {
    border-color: var(--navy);
    background: #eef1f8;
}

/* Radio: subject highlight */
.radio-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 10px;
}

/* ---- Submit button ---- */
.btn-submit {
    background: var(--gold);
    color: var(--navy);
    border: none;
    padding: 11px 32px;
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 0.5px;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s;
    margin-top: 28px;
}
.btn-submit:hover { background: #b8923a; }

/* ---- Error / info ---- */
.alert-err {
    background: #fdf0f0;
    border: 1px solid #e8b4b4;
    color: var(--red);
    padding: 10px 16px;
    border-radius: 4px;
    font-size: 13px;
    margin-bottom: 16px;
}
</style>
</head>
<body>

<?php
include 'header.php';

$cycle = $_GET['cycle'] ?? '';
include '_serveur.php';
?>

<div class="top-bar">
    <div>
        <div class="h1-wrap">
            <h1>📚 Réservation des Matières</h1>
        </div>
        <div class="subtitle">Assigner des sous-matières aux enseignants</div>
    </div>
</div>

<div class="page-wrap">
<div class="card">
<div class="card-body">

<form method="post" action="teacherreservesubdir.php?cycle=<?= htmlspecialchars($cycle) ?>">
    <input type="hidden" name="cycle" value="<?= htmlspecialchars($cycle) ?>">

    <!-- TEACHER -->
    <div class="section-label">Enseignant</div>
    <?php
    $sql = 'SELECT id, firstName, lastName FROM users WHERE type=2 ORDER BY firstName, lastName ASC';
    $req = mysqli_query($link, $sql);
    if (!$req) { echo '<div class="alert-err">Erreur: ' . mysqli_error($link) . '</div>'; }
    ?>
    <select name="name" class="form-select" required>
        <option value="">— Sélectionner un enseignant —</option>
        <?php while ($data = mysqli_fetch_assoc($req)): ?>
        <option value="<?= (int)$data['id'] ?>"
            <?= (isset($_POST['name']) && $_POST['name'] == $data['id']) ? 'selected' : '' ?>>
            <?= htmlspecialchars($data['firstName'] . ' ' . $data['lastName']) ?>
        </option>
        <?php endwhile; ?>
    </select>

    <!-- CLASSES -->
    <div class="section-label">Classes</div>
    <div class="check-grid">
    <?php
    $sql = 'SELECT id, name FROM classes WHERE cycleID="' . mysqli_real_escape_string($link, $cycle) . '" ORDER BY id ASC';
    $req = mysqli_query($link, $sql);
    if (!$req) { echo '<div class="alert-err">Erreur: ' . mysqli_error($link) . '</div>'; }
    while ($data = mysqli_fetch_assoc($req)):
        $classeid  = (int)$data['id'];
        $classname = strtoupper(htmlspecialchars($data['name']));
        $checked   = (isset($_POST['cla']) && in_array($classeid . ';' . strtoupper($data['name']), $_POST['cla'])) ? 'checked' : '';
    ?>
        <label class="check-item">
            <input type="checkbox" name="cla[]" value="<?= $classeid ?>;<?= $classname ?>" <?= $checked ?>>
            <?= $classname ?>
        </label>
    <?php endwhile; ?>
    </div>

    <!-- MATIÈRES -->
    <div class="section-label">Matière</div>
    <div class="radio-grid">
    <?php
    $sql = 'SELECT id, name FROM subjects WHERE id < 24 ORDER BY ordering ASC';
    $req = mysqli_query($link, $sql);
    if (!$req) { echo '<div class="alert-err">Erreur: ' . mysqli_error($link) . '</div>'; }
    while ($data = mysqli_fetch_assoc($req)):
        $matiereid   = (int)$data['id'];
        $matierename = htmlspecialchars($data['name']);
        $checked     = (isset($_POST['mat']) && $_POST['mat'] == $matiereid) ? 'checked' : '';
    ?>
        <label class="check-item">
            <input type="radio" name="mat" value="<?= $matiereid ?>" <?= $checked ?> required>
            <?= $matierename ?>
        </label>
    <?php endwhile; ?>
    </div>

    <button type="submit" name="envoyer" class="btn-submit">Suivant →</button>
</form>

</div><!-- /card-body -->
</div><!-- /card -->
</div><!-- /page-wrap -->

<?php
if (isset($_POST['envoyer'])) {
    $cla1 = $_POST['cla']  ?? [];
    $mat1 = $_POST['mat']  ?? '';
    $id   = $_POST['name'] ?? '';

    if (empty($cla1) || empty($mat1) || empty($id)) {
        // handled by HTML required — but just in case
    } else {
        $_SESSION['id']   = $id;
        $_SESSION['cla1'] = $cla1;
        $_SESSION['mat1'] = $mat1;
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=teachersubcont.php">';
    }
}
?>
</body>
</html>
