<section class="family-profile-header">
    <div class="family-profile-identity">
        <div class="family-profile-avatar"><?= htmlspecialchars(mb_substr($family['nom_famille'], 0, 2)) ?></div>
        <div>
            <span class="section-kicker"><?= htmlspecialchars($family['reference_famille']) ?></span>
            <h2>Famille <?= htmlspecialchars($family['nom_famille']) ?></h2>
            <p><?= htmlspecialchars(trim(($family['adresse_ligne1'] ?? '') . ' ' . ($family['code_postal'] ?? '') . ' ' . ($family['ville'] ?? '')) ?: 'Adresse non renseignée') ?></p>
        </div>
    </div>
    <div class="hero-actions">
        <a class="secondary-button button-link" href="/public/familles">Retour</a>
        <?php if(\App\Core\Permission::can('familles.edit')):?><a class="primary-button button-link" href="/public/familles/modifier?id=<?= (int)$family['id'] ?>">Modifier le dossier</a><?php endif?>
        <?php if(\App\Core\Permission::can('familles.archive')):?><form method="post" action="/public/administration/corbeille/archiver" onsubmit="return confirm('Archiver cette famille ?');"><?= \App\Core\Csrf::field() ?><input type="hidden" name="entity_type" value="family"><input type="hidden" name="entity_id" value="<?= (int)$family['id'] ?>"><input type="hidden" name="entity_label" value="Famille <?= htmlspecialchars($family['nom_famille']??$family['nom_affichage']??'') ?>"><input type="hidden" name="return_to" value="/public/familles"><button class="secondary-button">Archiver</button></form><?php endif?>
    </div>
</section>

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

<section class="family-summary-grid">
    <article class="summary-card">
        <span>Téléphone</span>
        <strong><?= htmlspecialchars($family['telephone_principal'] ?: 'Non renseigné') ?></strong>
    </article>
    <article class="summary-card">
        <span>E-mail</span>
        <strong><?= htmlspecialchars($family['email_principal'] ?: 'Non renseigné') ?></strong>
    </article>
    <article class="summary-card">
        <span>Adhérents</span>
        <strong><?= count($members) ?></strong>
    </article>
    <article class="summary-card">
        <span>RGPD</span>
        <strong class="<?= $family['rgpd_accepte'] ? 'text-success' : 'text-danger' ?>">
            <?= $family['rgpd_accepte'] ? 'Validé' : 'À compléter' ?>
        </strong>
    </article>
</section>

<section class="family-dossier-grid">
    <div class="family-dossier-main">
        <article class="panel">
            <div class="panel-header">
                <div><span class="section-kicker">Composition</span><h3>Adhérents de la famille</h3></div>
                <a href="#" class="text-link">+ Ajouter un adhérent</a>
            </div>

            <?php if (!$members): ?>
                <div class="empty-inline">Aucun adhérent n’est encore rattaché à cette famille.</div>
            <?php else: ?>
                <div class="member-list">
                <?php foreach ($members as $member): ?>
                    <div class="member-row">
                        <div class="table-avatar"><?= htmlspecialchars(mb_substr($member['prenom'],0,1) . mb_substr($member['nom'],0,1)) ?></div>
                        <div>
                            <strong><?= htmlspecialchars($member['prenom'] . ' ' . $member['nom']) ?></strong>
                            <span>Né(e) le <?= htmlspecialchars(date('d/m/Y', strtotime($member['date_naissance']))) ?></span>
                        </div>
                        <span class="badge badge-success"><?= htmlspecialchars($member['statut']) ?></span>
                    </div>
                <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </article>

        <article class="panel">
            <div class="panel-header">
                <div><span class="section-kicker">Suivi administratif</span><h3>Informations du dossier</h3></div>
            </div>
            <dl class="detail-grid">
                <div><dt>Situation familiale</dt><dd><?= htmlspecialchars($family['situation_familiale'] ?: 'Non renseignée') ?></dd></div>
                <div><dt>Quotient familial</dt><dd><?= htmlspecialchars($family['quotient_familial'] ?: 'Non renseigné') ?></dd></div>
                <div><dt>Contact téléphone</dt><dd><?= $family['autorise_contact_telephone'] ? 'Autorisé' : 'Non autorisé' ?></dd></div>
                <div><dt>Contact e-mail</dt><dd><?= $family['autorise_contact_email'] ? 'Autorisé' : 'Non autorisé' ?></dd></div>
                <div><dt>Contact SMS</dt><dd><?= $family['autorise_contact_sms'] ? 'Autorisé' : 'Non autorisé' ?></dd></div>
                <div><dt>Dossier créé le</dt><dd><?= htmlspecialchars(date('d/m/Y', strtotime($family['created_at']))) ?></dd></div>
            </dl>
        </article>

        <article class="panel">
            <div class="panel-header">
                <div><span class="section-kicker">Notes</span><h3>Observations</h3></div>
            </div>
            <p class="notes-content"><?= nl2br(htmlspecialchars($family['notes'] ?: 'Aucune observation enregistrée.')) ?></p>
        </article>
    </div>

    <aside class="family-dossier-side">
        <article class="panel">
            <div class="panel-header">
                <div><span class="section-kicker">Contact principal</span><h3>Responsable</h3></div>
            </div>
            <?php if ($responsible): ?>
                <div class="responsible-card">
                    <div class="family-profile-avatar small"><?= htmlspecialchars(mb_substr($responsible['prenom'],0,1) . mb_substr($responsible['nom'],0,1)) ?></div>
                    <strong><?= htmlspecialchars(trim($responsible['civilite'] . ' ' . $responsible['prenom'] . ' ' . $responsible['nom'])) ?></strong>
                    <span><?= htmlspecialchars($responsible['lien_avec_adherent'] ?: 'Lien non renseigné') ?></span>
                    <hr>
                    <span><?= htmlspecialchars($responsible['telephone'] ?: 'Téléphone non renseigné') ?></span>
                    <span><?= htmlspecialchars($responsible['email'] ?: 'E-mail non renseigné') ?></span>
                </div>
            <?php else: ?>
                <div class="empty-inline">Responsable principal non renseigné.</div>
            <?php endif; ?>
        </article>

        <article class="panel">
            <div class="panel-header">
                <div><span class="section-kicker">État du dossier</span><h3>Documents</h3></div>
            </div>
            <div class="document-status-list">
                <div><span class="<?= $family['rgpd_accepte'] ? 'status-ok' : 'status-missing' ?>"></span>Consentement RGPD</div>
                <div><span class="status-missing"></span>Droits à l’image</div>
                <div><span class="status-missing"></span>Justificatif de domicile</div>
            </div>
        </article>
    </aside>
</section>
