<?php
use App\Core\Csrf;
use App\Core\Permission;
?>
<link rel="stylesheet" href="/public/assets/css/cda-v6-groups.css">

<section class="page-intro">
<div>
<span class="page-kicker">Organisation pédagogique</span>
<h2>Groupes et créneaux</h2>
<p>Créneaux individuels et collectifs, successifs ou répartis sur plusieurs journées.</p>
</div>

<?php if(Permission::can('groupes.create')): ?>
<a class="button button-primary"
href="/public/groupes/nouveau">
+ Nouveau groupe
</a>
<?php endif; ?>
</section>

<?php if(!empty($_SESSION['success'])): ?>
<div class="alert alert-success">
<?= htmlspecialchars((string)$_SESSION['success']) ?>
</div>
<?php unset($_SESSION['success']); endif; ?>

<section class="group-v6-grid">
<?php foreach($groups as $group): ?>
<?php
$capacity=(int)($group['capacite_max'] ?? 0);
$participants=(int)($group['nb_participants'] ?? 0);
$remaining=$capacity>0
    ? max(0,$capacity-$participants)
    : null;
?>

<article class="group-v6-card">
<header>
<div class="group-day">
<span><?= htmlspecialchars(ucfirst((string)$group['jour_semaine'])) ?></span>
<strong><?= htmlspecialchars(substr((string)$group['heure_debut'],0,5)) ?></strong>
</div>

<div class="group-title">
<span><?= htmlspecialchars((string)$group['atelier_code']) ?></span>
<h3><?= htmlspecialchars((string)$group['libelle']) ?></h3>
<p><?= htmlspecialchars((string)$group['atelier_libelle']) ?></p>
</div>

<b><?= htmlspecialchars((string)$group['code']) ?></b>
</header>

<div class="group-meta">
<div><span>Professeur</span><strong><?= htmlspecialchars((string)($group['professeur_nom'] ?: 'Non affecté')) ?></strong></div>
<div><span>Salle</span><strong><?= htmlspecialchars((string)($group['salle_libelle'] ?: 'Non affectée')) ?></strong></div>
<div><span>Horaire</span><strong><?= htmlspecialchars(substr((string)$group['heure_debut'],0,5).'–'.substr((string)$group['heure_fin'],0,5)) ?></strong></div>
</div>

<div class="capacity-panel">
<div>
<strong><?= $participants ?></strong>
<span>participants</span>
</div>

<div>
<strong><?= $capacity ?: '—' ?></strong>
<span>capacité</span>
</div>

<div class="<?= $remaining===0 && $capacity>0 ? 'full' : '' ?>">
<strong><?= $remaining===null ? '—' : $remaining ?></strong>
<span>places restantes</span>
</div>
</div>

<footer>
<span class="group-type">
<?= htmlspecialchars((string)$group['type_groupe']) ?>
</span>

<div>
<?php if(Permission::can('groupes.edit')): ?>
<a href="/public/groupes/modifier?id=<?= (int)$group['id'] ?>">
Modifier
</a>
<?php endif; ?>

<?php if(Permission::can('groupes.manage_participants')): ?>
<a href="/public/groupes/voir?id=<?= (int)$group['id'] ?>">
Participants
</a>
<?php endif; ?>

<?php if(Permission::can('groupes.archive')): ?>
<form method="post"
action="/public/groupes/archiver"
onsubmit="return confirm('Archiver ce groupe ?');">
<?= Csrf::field() ?>
<input type="hidden" name="id"
value="<?= (int)$group['id'] ?>">
<button>Archiver</button>
</form>
<?php endif; ?>
</div>
</footer>
</article>
<?php endforeach; ?>
</section>
