<?php
/* Vue détail candidature — variables fournies par public/admin/candidature.php :
   $candidature (ligne complète de la table candidatures), $pieces (liste de
   ['id','type','nom_original','nom_stockage','disponible']), $maj (bool),
   $erreurStatut (string|null), $retour (query string validée de la liste, ou ''). */
$c = $candidature;
$urlListe = $retour !== '' ? '/admin/?' . $retour : '/admin/';

if (!function_exists('libelle_filiere')) {
    function libelle_filiere(string $code): string {
        return isset(filieres()[$code]) ? $code . ' — ' . filieres()[$code]['nom'] : $code;
    }
}
?>
<a class="lien-retour" href="<?= htmlspecialchars($urlListe) ?>">&larr; Toutes les candidatures</a>

<h1><?= htmlspecialchars($c['nom'] . ' ' . $c['prenoms']) ?></h1>
<p>
  <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; ?>
</p>
<p class="aide">Numéro <?= htmlspecialchars($c['numero']) ?> — déposé le <?= htmlspecialchars(date('d/m/Y H:i', strtotime($c['created_at']))) ?></p>

<?php if ($maj): ?>
  <p class="bandeau-info">Statut mis à jour.</p>
<?php endif; ?>
<?php if ($erreurStatut): ?>
  <p class="bandeau-erreur"><?= htmlspecialchars($erreurStatut) ?></p>
<?php endif; ?>

<div class="fiche-groupe">
  <h2>Identité</h2>
  <dl>
    <div><dt>Nom</dt><dd><?= htmlspecialchars($c['nom']) ?></dd></div>
    <div><dt>Prénoms</dt><dd><?= htmlspecialchars($c['prenoms']) ?></dd></div>
    <div><dt>Date de naissance</dt><dd><?= htmlspecialchars(date('d/m/Y', strtotime($c['date_naissance']))) ?></dd></div>
    <div><dt>Lieu de naissance</dt><dd><?= htmlspecialchars($c['lieu_naissance']) ?></dd></div>
    <div><dt>Sexe</dt><dd><?= $c['sexe'] === 'F' ? 'Féminin' : 'Masculin' ?></dd></div>
    <div><dt>Nationalité</dt><dd><?= htmlspecialchars($c['nationalite']) ?></dd></div>
  </dl>
</div>

<div class="fiche-groupe">
  <h2>Contact</h2>
  <dl>
    <div><dt>Téléphone</dt><dd><?= htmlspecialchars($c['telephone']) ?></dd></div>
    <div><dt>E-mail</dt><dd><?= !empty($c['email']) ? htmlspecialchars($c['email']) : '—' ?></dd></div>
    <div><dt>Ville</dt><dd><?= htmlspecialchars($c['ville']) ?></dd></div>
  </dl>
</div>

<div class="fiche-groupe">
  <h2>Parcours</h2>
  <dl>
    <div><dt>Série du BAC</dt><dd><?= htmlspecialchars($c['bac_serie']) ?></dd></div>
    <div><dt>Année du BAC</dt><dd><?= htmlspecialchars($c['bac_annee']) ?></dd></div>
    <div><dt>Établissement d'origine</dt><dd><?= htmlspecialchars($c['etablissement_origine']) ?></dd></div>
  </dl>
</div>

<div class="fiche-groupe">
  <h2>Choix de filière</h2>
  <dl>
    <div><dt>1er choix</dt><dd><?= htmlspecialchars(libelle_filiere($c['filiere1'])) ?></dd></div>
    <div><dt>2e choix</dt><dd><?= !empty($c['filiere2']) ? htmlspecialchars(libelle_filiere($c['filiere2'])) : '—' ?></dd></div>
  </dl>
</div>

<div class="fiche-groupe">
  <h2>Tuteur</h2>
  <dl>
    <div><dt>Nom du tuteur</dt><dd><?= htmlspecialchars($c['tuteur_nom']) ?></dd></div>
    <div><dt>Téléphone du tuteur</dt><dd><?= htmlspecialchars($c['tuteur_telephone']) ?></dd></div>
  </dl>
</div>

<div class="fiche-groupe">
  <h2>Pièces jointes</h2>
  <?php if (!$pieces): ?>
    <p>Aucune pièce.</p>
  <?php else: ?>
    <ul>
      <?php foreach ($pieces as $p): ?>
        <li>
          <?= htmlspecialchars(PIECES_TYPES[$p['type']] ?? $p['type']) ?>
          — <?= htmlspecialchars($p['nom_original']) ?>
          <?php if ($p['disponible']): ?>
            — <a href="/admin/piece.php?id=<?= (int)$p['id'] ?>" aria-label="Télécharger <?= htmlspecialchars(PIECES_TYPES[$p['type']] ?? $p['type']) ?>">Télécharger</a>
          <?php else: ?>
            <span class="piece-manquante">(fichier manquant)</span>
          <?php endif; ?>
        </li>
      <?php endforeach; ?>
    </ul>
  <?php endif; ?>
</div>

<div class="fiche-groupe">
  <h2>Changer le statut</h2>
  <form method="post" action="/admin/candidature.php?numero=<?= htmlspecialchars(urlencode($c['numero'])) ?><?= $retour !== '' ? '&amp;retour=' . htmlspecialchars(urlencode($retour)) : '' ?>">
    <div class="champ">
      <label for="statut">Statut</label>
      <select id="statut" name="statut">
        <?php foreach (STATUTS_LABELS as $code => $libelle): ?>
          <option value="<?= htmlspecialchars($code) ?>"<?= $c['statut'] === $code ? ' selected' : '' ?>><?= htmlspecialchars($libelle) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <button type="submit" class="btn-cta">Mettre à jour</button>
  </form>
</div>
