<?php
$teacherID=$_GET['id'];


include('_serveur.php');
mysqli_set_charset($link, 'utf8mb4');

/* ID DU PROF connecté */
?>

<form method="post" class="w3-container w3-card-4 w3-light-grey w3-padding">

  <!-- CLASSES DU PROF UNIQUEMENT -->
  <label>Classe :</label>
  <select name="classID" class="w3-select" required>
    <option value="">-- Choisir --</option>
    <?php
    $sql = "
      SELECT DISTINCT c.id, c.name
      FROM classes c
      INNER JOIN teachersSubjects ts ON ts.classID = c.id
      WHERE ts.userID = $teacherID
      ORDER BY c.name ASC
    ";
    $res = mysqli_query($link, $sql);
    while ($r = mysqli_fetch_assoc($res)) {
        echo '<option value="'.$r['id'].'">'.strtoupper($r['name']).'</option>';
    }
    ?>
  </select>

  <!-- MATIERES DU PROF UNIQUEMENT -->
  <label>Matière :</label>
  <select name="subjectID" class="w3-select" required>
    <option value="">-- Choisir --</option>
   <?php
if (!isset($teacherID)) {
    die("Teacher ID is missing");
}

$sql = "
  SELECT DISTINCT s.id, s.name
  FROM subjects s
  INNER JOIN teachersSubjects ts ON ts.subjectID = s.id
  WHERE ts.userID = ?
";

$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "i", $teacherID);
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_get_result($stmt);

while ($r = mysqli_fetch_assoc($res)) {
    echo '<option value="'.$r['id'].'">'.$r['name'].'</option>';
}
?>

  </select>

  <button type="submit" class="w3-button w3-blue w3-margin-top">
    Afficher les fiches
  </button>
</form>

<?php
/* AFFICHAGE DES FICHES */
if (!empty($_POST['classID']) && !empty($_POST['subjectID'])) {

    $classID   = intval($_POST['classID']);
    $subjectID = intval($_POST['subjectID']);

    $sql = "
      SELECT f.*
      FROM fiches f
      INNER JOIN teachersSubjects ts 
        ON ts.subjectID = f.subjectID
        AND ts.classID = f.classID
      WHERE 
        ts.userID = $teacherID
        AND f.classID = $classID
        AND f.subjectID = $subjectID
      ORDER BY f.date DESC
    ";

    $result = mysqli_query($link, $sql);

echo '
<table class="w3-table-all w3-hoverable w3-card-4 w3-margin-top">
  <thead>
    <tr class="w3-blue-grey">
      <th style="width:35%">Titre</th>
      <th style="width:15%">Date</th>
      <th style="width:25%">Fichier</th>
      <th style="width:15%" class="w3-center">Action</th>
    </tr>
  </thead>
  <tbody>
';

while ($row = mysqli_fetch_assoc($result)) {

    echo '<tr>';
    
    echo '<td>
            <strong>'.htmlspecialchars($row['title']).'</strong>
          </td>';

    echo '<td>
            '.date("d/m/Y", strtotime($row['date'])).'
          </td>';

    echo '<td>
            <a href="'.$row['url'].'" target="_blank" 
               class="w3-button w3-small w3-light-grey w3-round">
               📄 Voir
            </a>
          </td>';

    echo '<td class="w3-center">
      <form method="post" action="_deletefichep.php" onsubmit="return confirm(\'Supprimer cette fiche ?\');">
        <input type="hidden" name="ficheID" value="'.$row['id'].'">
        <input type="hidden" name="classID" value="'.$row['classID'].'">
        <button type="submit" class="w3-button w3-red w3-small">🗑️</button>
      </form>
          </td>';

    echo '</tr>';
}

echo '
  </tbody>
</table>
';
}
?>
