<?php use App\Core\Permission; ?>
<?php $familyPortal=Permission::isFamilyPortalIdentity(); ?>
<link rel="stylesheet" href="/public/assets/css/cda-member-consents.css">

<section class="member-hero">
<div>
<span class="page-kicker">Dossier famille</span>
<h2><?= htmlspecialchars($member['prenom'].' '.$member['nom']) ?></h2>
<p>Membre de la famille</p>
</div>

<div class="member-actions">
<a class="button button-secondary" href="/public/clients/voir?id=<?= (int)($member['client_id'] ?? 0) ?>">← Retour à la famille</a>
<?php if(Permission::can('membres.edit')): ?>
<a class="button button-secondary"
href="/public/membres/modifier?id=<?= (int)$member['id'] ?>">
Modifier
</a>
<?php endif; ?>

<?php if(Permission::can('demandes_inscription.create') && $annualFile): ?>
<a class="button button-primary"
href="/public/demandes-inscription/nouvelle?membre_id=<?= (int)$member['id'] ?>">
+ Demander une inscription
</a>
<?php endif; ?>
</div>
</section>
<?php if(!$annualFile): ?><section class="member-card"><strong>Étape préalable : année scolaire non activée.</strong><p>Activez d’abord le dossier annuel depuis la fiche de l’adhérent. L’inscription à un atelier sera ensuite disponible.</p><a class="button button-primary" href="/public/clients/voir?id=<?= (int)($member['client_id'] ?? 0) ?>">Activer l’année</a></section><?php endif; ?>

<?php if(!$familyPortal): ?>
<link rel="stylesheet" href="/public/assets/css/cda-music-levels-9628.css">
<section class="member-card music-levels"><div class="music-level-head"><div><span class="page-kicker">Suivi pédagogique facultatif</span><h3>Niveaux musicaux</h3><p>Formation musicale et pratique instrumentale sont suivies séparément et historisées par année scolaire.</p></div><?php if($currentSchoolYear):?><strong><?=htmlspecialchars($currentSchoolYear['libelle'])?></strong><?php endif?></div>
<?php if(Permission::can('membres.edit')&&$currentSchoolYear):?><form method="post" action="/public/membres/modifier" class="music-level-form"><?=\App\Core\Csrf::field()?><input type="hidden" name="id" value="<?=(int)$member['id']?>"><input type="hidden" name="form_action" value="save_music_level"><input type="hidden" name="annee_scolaire_id" value="<?=(int)$currentSchoolYear['id']?>"><label>Suivi concerné<select name="type_niveau" id="music-level-type" required><option value="formation_musicale">Formation musicale</option><option value="instrument">Instrument</option></select></label><label id="music-instrument-field" hidden>Instrument<select name="instrument_id"><option value="">Sélectionner</option><?php foreach($musicInstruments as $instrument):?><option value="<?=(int)$instrument['id']?>"><?=htmlspecialchars($instrument['libelle'])?></option><?php endforeach?></select></label><label>Niveau actuel<select name="niveau_code" required><option value="">Non renseigné</option><?php $cat='';foreach($musicLevelReferences as $level):if($cat!==$level['categorie']):if($cat!==''):?></optgroup><?php endif;$cat=$level['categorie'];?><optgroup label="<?=htmlspecialchars($cat)?>"><?php endif?><option value="<?=htmlspecialchars($level['code'])?>"><?=htmlspecialchars($level['libelle'])?></option><?php endforeach;if($cat!==''):?></optgroup><?php endif?></select></label><label>Observation<input name="observation" placeholder="Facultatif"></label><button class="button button-primary">Enregistrer le niveau</button></form><?php endif?>
<div class="music-level-history"><?php if(!$musicLevels):?><p>Aucun niveau musical renseigné.</p><?php endif;foreach($musicLevels as $level):?><article><div><b><?=htmlspecialchars($level['type_niveau']==='formation_musicale'?'Formation musicale':($level['instrument_libelle']?:'Instrument'))?></b><small><?=htmlspecialchars($level['annee_libelle'])?></small></div><strong><?=htmlspecialchars($level['niveau_libelle'])?></strong><?php if($level['observation']):?><small><?=htmlspecialchars($level['observation'])?></small><?php endif?></article><?php endforeach?></div></section>
<script>document.addEventListener('DOMContentLoaded',()=>{const t=document.querySelector('#music-level-type'),f=document.querySelector('#music-instrument-field');if(!t||!f)return;const sync=()=>{f.hidden=t.value!=='instrument';f.querySelector('select').required=t.value==='instrument'};t.addEventListener('change',sync);sync()});</script>
<?php endif; ?>

<section class="member-grid">
<article class="member-card">
<h3>Informations</h3>
<dl>
<dt>Date de naissance</dt>
<dd><?= htmlspecialchars((string)($member['date_naissance'] ?: 'Non renseignée')) ?></dd>
<dt>E-mail</dt>
<dd><?= htmlspecialchars((string)($member['email'] ?: 'Non renseigné')) ?></dd>
<dt>Téléphone</dt>
<dd><?= htmlspecialchars((string)($member['telephone'] ?: 'Non renseigné')) ?></dd>
</dl>
</article>

<article class="member-card">
<h3>Documents</h3>
<div class="document-actions">
<a href="/public/consentements/nouveau?membre_id=<?= (int)$member['id'] ?>&type=rgpd">
Créer RGPD
</a>
<a href="/public/consentements/nouveau?membre_id=<?= (int)$member['id'] ?>&type=droit_image">
Créer droit à l’image
</a>
</div>

<?php foreach($documents as $document): ?>
<div class="document-row">
<div>
<strong><?= htmlspecialchars($document['type_document']) ?></strong>
<small><?= htmlspecialchars((string)$document['annee_scolaire']) ?> · <?= htmlspecialchars($document['statut']) ?></small>
</div>
<?php if($document['fichier_pdf']): ?>
<a href="/<?= htmlspecialchars($document['fichier_pdf']) ?>" target="_blank">Voir</a>
<?php else: ?>
<a href="/public/consentements/signature?id=<?= (int)$document['id'] ?>">Signer</a>
<?php endif; ?>
</div>
<?php endforeach; ?>
</article>
</section>
<div class="member-actions" style="margin-top:1rem"><a class="button button-secondary" href="/public/clients/voir?id=<?= (int)($member['client_id'] ?? 0) ?>">← Retour à la famille</a></div>

<section class="member-card">
<h3>Demandes d’inscription</h3>

<?php if(!$requests): ?>
<p>Aucune demande enregistrée.</p>
<?php endif; ?>

<?php foreach($requests as $request): ?>
<div class="document-row">
<div>
<strong><?= htmlspecialchars($request['atelier_libelle']) ?></strong>
<small><?= htmlspecialchars($request['atelier_code']) ?> · <?= htmlspecialchars(in_array($request['statut'],['file_attente','a_etudier','creneau_propose'],true)?'File d’attente':$request['statut']) ?></small>
</div>
<span><?= htmlspecialchars(date('d/m/Y',strtotime($request['created_at']))) ?></span>
<?php if(Permission::can('demandes_inscription.review') && in_array($request['statut'],['file_attente','a_etudier','creneau_propose'],true)): ?><a class="button button-secondary" href="/public/demandes-inscription/etudier?id=<?= (int)$request['id'] ?>">Étudier</a><?php endif; ?>
</div>
<?php endforeach; ?>
</section>
