<?php
use App\Core\Csrf;
use App\Core\Permission;

$errors=$_SESSION['form_errors']??[];
unset($_SESSION['form_errors']);
?>
<link rel="stylesheet" href="/public/assets/css/cda-v4-10.css">
<link rel="stylesheet" href="/public/assets/css/cda-v5-2-2.css">

<section class="page-intro">
<div>
<span class="page-kicker">Administration</span>
<h2>Paramètres pédagogiques</h2>
<p>Les disciplines constituent l’unique référentiel pédagogique. Les places sont définies exclusivement dans les plannings prévisionnels des professeurs.</p>
</div>
<div class="page-actions">
<a class="button button-secondary" href="/public/demandes-inscription">Files d’attente</a>
<a class="button button-primary" href="/public/tarifs">Grille tarifaire TTC</a>
</div>
</section>

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


<section class="cda-settings-hub">
    <a href="#poles"><span>🏛</span><div><strong>Référentiel des disciplines</strong><small>Créer, renommer ou archiver</small></div></a>
    <a href="/public/demandes-inscription"><span>♫</span><div><strong>Files d’attente</strong><small>Demandes classées par discipline</small></div></a>
    <a href="/public/parametres/types-contrats"><span>📄</span><div><strong>Types de contrats</strong><small>Contrats des professeurs</small></div></a>
    <a href="/public/parametres/icones"><span>🎨</span><div><strong>Icônes</strong><small>Symboles des disciplines</small></div></a>
    <a href="/public/tarifs"><span>€</span><div><strong>Tarifs et réductions</strong><small>Grilles tarifaires</small></div></a>
    <a href="/public/administration/comptes"><span>👤</span><div><strong>Utilisateurs et rôles</strong><small>Comptes et permissions</small></div></a>
    <a href="/public/studio/mises-a-jour"><span>⬆</span><div><strong>Mises à jour</strong><small>Packages et historique</small></div></a>
</section>

<section class="hierarchy-banner">
<article><b>1</b><div><strong>Discipline</strong><span>Trompette, dessin, théâtre…</span></div></article>
<i>→</i>
<article><b>2</b><div><strong>Planning professeur</strong><span>Jours, horaires et places disponibles</span></div></article>
<i>→</i>
<article><b>3</b><div><strong>File d’attente</strong><span>Affectation d’un élève à une place validée</span></div></article>
</section>

<section class="parameter-grid-v410">
<article class="settings-card" id="poles">
<header><span>🏛</span><div><h3>Pôles</h3><p>Grandes familles artistiques.</p></div></header>

<?php if(Permission::can('parametres.poles.manage')): ?>
<form method="post" action="/public/parametres/poles" class="compact-form">
<?= Csrf::field() ?>
<input required maxlength="20" name="code" placeholder="Code : MUS">
<input required name="libelle" placeholder="Libellé : Musique">
<input type="hidden" name="ordre_affichage" value="100">
<button class="button button-primary">Ajouter</button>
</form>
<?php endif; ?>

<div class="settings-list managed-list">
<?php foreach($poles as $pole): ?>
<div>
<span class="code-pill"><?= htmlspecialchars((string)$pole['code']) ?></span>
<strong><?= htmlspecialchars((string)$pole['libelle']) ?></strong>

<div class="parameter-actions">
<?php if(Permission::can('parametres.poles.edit')): ?>
<a href="/public/parametres/poles/modifier?id=<?= (int)$pole['id'] ?>">
Modifier
</a>
<?php endif; ?>

<?php if(Permission::can('parametres.poles.archive')): ?>
<form method="post"
action="/public/parametres/poles/archiver"
onsubmit="return confirm('Archiver ce pôle ? Cette action sera refusée si des cours actifs y sont rattachés.');">
<?= Csrf::field() ?>
<input type="hidden" name="id" value="<?= (int)$pole['id'] ?>">
<button>Archiver</button>
</form>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</article>

<article class="settings-card" id="disciplines">
<header><span>🎺</span><div><h3>Liste des disciplines</h3><p>Référentiel pédagogique unique. La capacité n’est jamais définie ici : elle provient du planning professeur.</p></div></header>

<?php if(Permission::can('parametres.disciplines.manage')): ?>
<form method="post" action="/public/parametres/disciplines" class="compact-form discipline-form">
<?= Csrf::field() ?>
<select required name="pole_id">
<option value="">Choisir le pôle</option>
<?php foreach($poles as $pole): ?>
<option value="<?= (int)$pole['id'] ?>">
<?= htmlspecialchars((string)$pole['libelle']) ?>
</option>
<?php endforeach; ?>
</select>
<input required maxlength="20" name="code" placeholder="Code : TROMP">
<input required name="libelle" placeholder="Discipline : Trompette">
<label>
<span>Durée individuelle</span>
<select required name="duree_individuelle_minutes">
<?php foreach([15,20,25,30,40,45,50,60,75,90,120] as $minutes): ?>
<option value="<?= $minutes ?>" <?= $minutes===30?'selected':'' ?>><?= $minutes ?> min</option>
<?php endforeach; ?>
</select>
</label>
<label>
<span>Durée collective</span>
<select required name="duree_collective_minutes">
<?php foreach([30,45,60,75,90,105,120,150,180] as $minutes): ?>
<option value="<?= $minutes ?>" <?= $minutes===60?'selected':'' ?>><?= $minutes ?> min</option>
<?php endforeach; ?>
</select>
</label>
<button class="button button-primary">Ajouter</button>
</form>
<?php endif; ?>

<div class="settings-list managed-list">
<?php foreach($disciplines as $discipline): ?>
<div>
<span class="code-pill">
<?= htmlspecialchars((string)$discipline['pole_code']) ?>
-
<?= htmlspecialchars((string)$discipline['code']) ?>
</span>

<strong><?= htmlspecialchars((string)$discipline['libelle']) ?></strong>
<small><?= htmlspecialchars((string)$discipline['pole_libelle']) ?></small>
<small>
Individuel : <?= (int)$discipline['duree_individuelle_minutes'] ?> min
· Collectif : <?= (int)$discipline['duree_collective_minutes'] ?> min
</small>

<div class="parameter-actions">
<?php if(Permission::can('parametres.disciplines.edit')): ?>
<a href="/public/parametres/disciplines/modifier?id=<?= (int)$discipline['id'] ?>">
Modifier
</a>
<?php endif; ?>

<?php if(Permission::can('parametres.disciplines.archive')): ?>
<form method="post"
action="/public/parametres/disciplines/archiver"
onsubmit="return confirm('Archiver cette discipline ? Cette action sera refusée si des cours actifs y sont rattachés.');">
<?= Csrf::field() ?>
<input type="hidden" name="id" value="<?= (int)$discipline['id'] ?>">
<button>Archiver</button>
</form>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</article>

<article class="settings-card" id="publics">
<header><span>👥</span><div><h3>Publics</h3><p>Libellés et tranches d’âge modifiables.</p></div></header>

<div class="public-two">
<?php foreach($publics as $public): ?>
<form method="post" action="/public/parametres/publics/modifier" class="public-reference-form">
<?= Csrf::field() ?><input type="hidden" name="id" value="<?= (int)$public['id'] ?>">
<span><?= ['ENF'=>'🧒','ADO'=>'🧑','ADU'=>'👤'][$public['code']]??'👥' ?></span>
<label><small><?= htmlspecialchars((string)$public['code']) ?></small><input required name="libelle" value="<?= htmlspecialchars((string)$public['libelle']) ?>"></label>
<label><small>Âge min.</small><input type="number" min="0" name="age_min" value="<?= htmlspecialchars((string)($public['age_min']??'')) ?>"></label>
<label><small>Âge max.</small><input type="number" min="0" name="age_max" value="<?= htmlspecialchars((string)($public['age_max']??'')) ?>"></label>
<?php if(Permission::can('parametres.poles.edit')):?><button class="button button-secondary">Enregistrer</button><?php endif?>
</form>
<?php endforeach; ?>
</div>

<p class="info-note">
Le code comptable et technique reste stable même si le libellé est modifié.
</p>
</article>

<article class="settings-card">
<header><span>🔒</span><div><h3>Règles d’éligibilité</h3><p>Contrôles bloquants lors de l’inscription.</p></div></header>

<div class="eligibility-list">
<?php foreach($eligibilityRules as $rule): ?>
<div>
<span class="lock-icon">🔒</span>
<div>
<strong>
<?= htmlspecialchars((string)($rule['pole_libelle']??'Tous pôles')) ?>
—
<?= htmlspecialchars((string)($rule['public_libelle']??'Tous publics')) ?>
</strong>

<p><?= htmlspecialchars((string)$rule['message_blocage']) ?></p>

<small>
Niveau minimum :
<?= htmlspecialchars((string)($rule['niveau_min_libelle']??'Non défini')) ?>
</small>
</div>
</div>
<?php endforeach; ?>
</div>
</article>
</section>
