<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Publier une Fiche</title>
<link rel="stylesheet" href="w3.css">
</head>
<body>
<?php
include "header.php";
include "_serveur.php";

$userID = $_SESSION['id'] ?? 0;
if (!$userID) {
    echo "<div class='w3-panel w3-red'>Utilisateur non connecté.</div>";
    exit;
}

// Get the subjects and classes for this teacher
$sql = "SELECT ts.classID, c.name AS className, ts.subjectID, s.name AS subjectName
        FROM teachersSubjects ts
        JOIN subjects s ON ts.subjectID = s.id
        JOIN classes c ON ts.classID = c.id
        WHERE ts.userID = $userID
        ORDER BY ts.classID";
$req = mysqli_query($link, $sql);
if (!$req) {
    die("Erreur SQL: " . mysqli_error($link));
}

?>

<form method="post" enctype="multipart/form-data" action="_uploadfichep_backend.php" class="w3-container w3-card-4 w3-padding">
    <h3>Publier une fiche</h3>

    <label class="w3-text-black">Sélectionner les classes et matières:</label><br>
    <?php while ($data = mysqli_fetch_assoc($req)): ?>
        <input class="w3-check" type="checkbox" name="cont[]" 
               value="<?= $data['classID'].';'.$data['className'].';'.$data['subjectID'].';'.$data['subjectName'] ?>">
        <?= $data['className'] ?> - <?= $data['subjectName'] ?><br>
    <?php endwhile; ?>

    <br>
    <label class="w3-text-black">Titre:</label>
    <input class="w3-input w3-border" type="text" name="title" required>

    <label class="w3-text-black">Date:</label>
    <input class="w3-input w3-border" type="date" name="date" required>

    <label class="w3-text-black">Fichier (fiche):</label>
    <input class="w3-input w3-border" type="file" name="userfile" required>

    <br>
    <button type="submit" class="w3-button w3-blue">Publier</button>
</form>

</body>
</html>


