<section class="page-heading">
    <div>
        <span class="section-kicker">Dossiers administratifs</span>
        <h2>Familles</h2>
        <p>Carrefour des Arts de Bray-Dunes — <?= count($families) ?> dossier(s) affiché(s).</p>
    </div>
    <a class="primary-button button-link" href="/public/clients/nouveau">+ Nouvel adhérent et sa famille</a>
</section>

<section class="panel">
    <form class="family-search-bar" method="get" action="/public/familles">
        <div class="table-search">
            <span>⌕</span>
            <input type="search" name="q" value="<?= htmlspecialchars($query) ?>"
                   placeholder="Nom, responsable, téléphone, e-mail, ville ou référence…">
        </div>
        <button class="secondary-button" type="submit">Rechercher</button>
        <?php if ($query !== ''): ?>
            <a class="secondary-button button-link" href="/public/familles">Effacer</a>
        <?php endif; ?>
    </form>

    <?php if (!$families): ?>
        <div class="empty-state">
            <div class="empty-icon">👨‍👩‍👧</div>
            <h3>Aucune famille trouvée</h3>
            <p>Créez un premier dossier ou modifiez les critères de recherche.</p>
            <a class="primary-button button-link" href="/public/clients/nouveau">Créer l’adhérent titulaire</a>
        </div>
    <?php else: ?>
        <div class="family-list">
        <?php foreach ($families as $item): ?>
            <a class="family-card" href="/public/familles/voir?id=<?= (int)$item['id'] ?>">
                <div class="family-card-avatar">
                    <?= htmlspecialchars(mb_substr($item['nom_famille'], 0, 2)) ?>
                </div>
                <div class="family-card-main">
                    <div class="family-card-title">
                        <strong>Famille <?= htmlspecialchars($item['nom_famille']) ?></strong>
                        <span class="badge badge-neutral"><?= htmlspecialchars($item['reference_famille']) ?></span>
                    </div>
                    <div class="family-card-details">
                        <span><?= htmlspecialchars($item['responsable_principal'] ?: 'Responsable non renseigné') ?></span>
                        <span><?= htmlspecialchars($item['ville'] ?: 'Ville non renseignée') ?></span>
                        <span><?= (int)$item['nb_adherents'] ?> adhérent(s)</span>
                    </div>
                </div>
                <div class="family-card-contact">
                    <span><?= htmlspecialchars($item['telephone_principal'] ?: 'Téléphone non renseigné') ?></span>
                    <span><?= htmlspecialchars($item['email_principal'] ?: 'E-mail non renseigné') ?></span>
                </div>
                <span class="family-card-arrow">›</span>
            </a>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
</section>
