<?php
/* Vue liste des candidatures — variables fournies par public/admin/index.php :
   $lignes, $tropDeResultats, $compteurs, $total, $filiereFiltre, $statutFiltre,
   $q, $queryExport, $qsCourante (filtres courants à rethreader vers la fiche). */
$suffixeRetour = $qsCourante !== '' ? '&amp;retour=' . htmlspecialchars(urlencode($qsCourante)) : '';
?>
<h1>Candidatures</h1>

<div class="cartes-compteurs">
  <div class="carte-compteur">
    <span class="chiffre"><?= (int)$total ?></span>
    <span class="libelle">Total</span>
  </div>
  <?php foreach (STATUTS_LABELS as $code => $libelle): ?>
    <div class="carte-compteur">
      <span class="chiffre"><?= (int)($compteurs[$code] ?? 0) ?></span>
      <span class="libelle"><?= htmlspecialchars($libelle) ?></span>
    </div>
  <?php endforeach; ?>
</div>

<form method="get" action="/admin/" class="filtres-admin">
  <div class="champ">
    <label for="filiere">Filière</label>
    <select id="filiere" name="filiere">
      <option value="">Toutes</option>
      <?php foreach (filieres() as $code => $f): ?>
        <option value="<?= htmlspecialchars($code) ?>"<?= $filiereFiltre === $code ? ' selected' : '' ?>><?= htmlspecialchars($code . ' — ' . $f['nom']) ?></option>
      <?php endforeach; ?>
    </select>
  </div>
  <div class="champ">
    <label for="statut">Statut</label>
    <select id="statut" name="statut">
      <option value="">Tous</option>
      <?php foreach (STATUTS_LABELS as $code => $libelle): ?>
        <option value="<?= htmlspecialchars($code) ?>"<?= $statutFiltre === $code ? ' selected' : '' ?>><?= htmlspecialchars($libelle) ?></option>
      <?php endforeach; ?>
    </select>
  </div>
  <div class="champ">
    <label for="q">Recherche</label>
    <input type="text" id="q" name="q" placeholder="Nom, prénoms, numéro, téléphone…" value="<?= htmlspecialchars($q) ?>">
  </div>
  <div class="champ champ-actions">
    <button type="submit" class="btn-cta">Filtrer</button>
    <a class="btn-secondaire" href="/admin/export.php?<?= htmlspecialchars($queryExport) ?>">Exporter Excel</a>
  </div>
</form>

<?php if ($tropDeResultats): ?>
  <p class="bandeau-info">Plus de 500 candidatures correspondent à ces critères : seules les 500 plus récentes sont affichées. Affinez les filtres pour voir le reste.</p>
<?php endif; ?>

<?php if (!$lignes): ?>
  <p class="bandeau-info">Aucune candidature ne correspond à ces critères.</p>
<?php else: ?>
  <div class="tableau-defilant">
    <table class="admin-tableau">
      <thead>
        <tr>
          <th>Numéro</th>
          <th>Nom &amp; prénoms</th>
          <th>Filière</th>
          <th>Téléphone</th>
          <th>Date</th>
          <th>Statut</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($lignes as $c): ?>
          <tr>
            <td><?= htmlspecialchars($c['numero']) ?></td>
            <td><?= htmlspecialchars($c['nom'] . ' ' . $c['prenoms']) ?></td>
            <td><?= htmlspecialchars($c['filiere1']) ?></td>
            <td><?= htmlspecialchars($c['telephone']) ?></td>
            <td><?= htmlspecialchars(date('d/m/Y H:i', strtotime($c['created_at']))) ?></td>
            <td>
              <span class="badge-statut <?= htmlspecialchars($c['statut']) ?>"><?= htmlspecialchars(STATUTS_LABELS[$c['statut']] ?? $c['statut']) ?></span>
              <?php if (!(int)$c['dossier_complet']): ?><span class="badge-statut badge-incomplet">Dossier incomplet</span><?php endif; ?>
            </td>
            <td><a href="/admin/candidature.php?numero=<?= htmlspecialchars(urlencode($c['numero'])) ?><?= $suffixeRetour ?>" aria-label="Voir la candidature de <?= htmlspecialchars($c['nom'] . ' ' . $c['prenoms']) ?>">Voir</a></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
<?php endif; ?>
