<?php
include('_serveur.php');
?>

<form method="post" action="listdevoirs.php" class="w3-container w3-card-4 w3-light-grey w3-padding">
  <label>Classe:</label>
  <select name="classID" class="w3-select" required>
    <option value="">-- Choisir --</option>
    <?php
    $result = mysqli_query($link, "SELECT * FROM classes ORDER BY name ASC");
    while ($row = mysqli_fetch_array($result)) {
      echo '<option value="'.$row['id'].'">'.strtoupper($row['name']).'</option>';
    }
    ?>
  </select>

  <label>MatiÃ¨re:</label>
  <select name="subjectID" class="w3-select" required>
    <option value="">-- Choisir --</option>
    <?php
    $result = mysqli_query($link, "SELECT * FROM subjects ORDER BY ordering ASC");
    while ($row = mysqli_fetch_array($result)) {
      echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
    }
    ?>
  </select>

  <button type="submit" class="w3-button w3-blue w3-margin-top">Afficher les devoirs</button>
</form>
<?php
if (isset($_POST['classID']) && isset($_POST['subjectID'])) {
  $classID = intval($_POST['classID']);
  $subjectID = intval($_POST['subjectID']);

  $sql = "SELECT * FROM devoirs WHERE classID = $classID AND subjectID = $subjectID ORDER BY date DESC";
  $result = mysqli_query($link, $sql);

  echo '<table class="w3-table-all w3-margin-top">';
  echo '<tr class="w3-teal"><th>Titre</th><th>Date</th><th>Devoir</th><th>Action</th></tr>';

  while ($row = mysqli_fetch_array($result)) {
    echo '<tr>';
    echo '<td>'.htmlspecialchars($row['title']).'</td>';
    echo '<td>'.$row['date'].'</td>';
    echo '<td><a href="'.$row['url'].'" target="_blank">ðŸ“„ Voir</a></td>';
    echo '<td>
      <form method="post" action="_deletedevoir.php" onsubmit="return confirm(\'Supprimer ce devoir ?\');">
        <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 '</table>';
}
?>