<?php use App\Core\Csrf;$h=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');$summary=$analysis['summary']; ?>
<section class="page-header">
 <div><span class="page-kicker">INSCRIPTION-005</span><h2>Optimisation des groupes et créneaux</h2><p>Propositions calculées à partir des capacités, disponibilités, files d’attente et taux de remplissage. La validation humaine reste obligatoire.</p></div>
 <a class="button button-secondary" href="/public/demandes-inscription">Retour aux demandes</a>
</section>
<?php if(!$analysis['year']):?><section class="card"><p>Aucune année scolaire active.</p></section><?php else:?>
<section style="display:grid;grid-template-columns:repeat(auto-fit,minmax(170px,1fr));gap:12px;margin:16px 0">
 <article class="card"><strong style="font-size:1.8rem"><?=(int)$summary['requests']?></strong><p>Demandes analysées</p></article>
 <article class="card"><strong style="font-size:1.8rem"><?=(int)$summary['suggestions']?></strong><p>Affectations possibles</p></article>
 <article class="card"><strong style="font-size:1.8rem"><?=(int)$summary['places']?></strong><p>Places disponibles</p></article>
 <article class="card"><strong style="font-size:1.8rem"><?=(int)$summary['unmatched']?></strong><p>Sans solution actuelle</p></article>
</section>
<form method="post" action="/public/demandes-inscription/optimisation/proposer">
 <?=Csrf::field()?>
 <section class="card">
  <div style="display:flex;justify-content:space-between;gap:12px;align-items:center;flex-wrap:wrap"><div><h3>Propositions recommandées — <?=$h($analysis['year']['libelle'])?></h3><p>Les places déjà inscrites et les propositions en attente sont déduites de la capacité.</p></div><button class="button button-primary" type="submit">Transmettre les propositions sélectionnées</button></div>
  <?php if(!$analysis['suggestions']):?><p>Aucune affectation compatible n’est disponible.</p><?php else:?><div style="overflow-x:auto"><table class="table"><thead><tr><th></th><th>Élève</th><th>Discipline</th><th>Créneau proposé</th><th>Remplissage</th><th>Score</th><th>Motifs</th></tr></thead><tbody>
  <?php foreach($analysis['suggestions'] as $s):?><tr>
   <td><input type="checkbox" name="selected[]" value="<?=(int)$s['request_id']?>" checked><input type="hidden" name="slot[<?=(int)$s['request_id']?>]" value="<?=(int)$s['slot_id']?>"></td>
   <td><strong><?=$h($s['membre_nom'])?></strong><br><small>Attente depuis <?=date('d/m/Y',strtotime((string)$s['created_at']))?></small><?php if($s['disponibilites']):?><br><small>Disponibilités : <?=$h($s['disponibilites'])?></small><?php endif?></td>
   <td><?=$h($s['discipline_libelle'])?></td>
   <td><strong><?=ucfirst($h($s['jour_semaine']))?> <?=substr($h($s['heure_debut']),0,5)?>–<?=substr($h($s['heure_fin']),0,5)?></strong><br><small><?=$h($s['professeur_nom'])?> · <?=$h($s['salle_libelle']?:'Salle non renseignée')?></small></td>
   <td><?=(int)$s['occupation']?> / <?=(int)$s['capacite_effective']?></td>
   <td><strong><?=(int)$s['score']?></strong></td>
   <td><?=$h(implode(', ',$s['reasons']))?></td>
  </tr><?php endforeach?></tbody></table></div><?php endif?>
 </section>
</form>
<?php if($analysis['unmatched']):?><section class="card"><h3>Demandes sans créneau compatible</h3><div style="overflow-x:auto"><table class="table"><thead><tr><th>Élève</th><th>Discipline</th><th>Disponibilités</th></tr></thead><tbody><?php foreach($analysis['unmatched'] as $r):?><tr><td><?=$h($r['membre_nom'])?></td><td><?=$h($r['discipline_libelle'])?></td><td><?=$h($r['disponibilites']?:'Non précisées')?></td></tr><?php endforeach?></tbody></table></div></section><?php endif?>
<?php endif?>
