<?php
// Sous-vue de la fiche animale ; les variables sont préparées par AnimalsController::show().
?>
<!-- PORTÉES -->
<?php if ($isCat): ?><div class="tab-pane" id="tab-litters">
    <div class="d-flex align-items-center justify-content-between mb-3">
        <div>
            <h3 class="mb-0"><?= h(t('animal.litters_parentage')) ?></h3>
            <div class="text-muted"><?= h(t('animal.litters_help')) ?></div>
        </div>
        <a class="btn btn-primary" href="/litter/new?animal_id=<?= (int)$animal['id'] ?>">+
            <?= h(t('animal.add_litter')) ?></a>
    </div>
    <?php if (empty($litters)): ?>
    <div class="empty">
        <div class="empty-description"><?= h(t('animal.no_litter')) ?></div>
    </div>
    <?php else: ?>
    <div class="row row-cards">
        <?php foreach ($litters as $litter): ?>
        <?php
    $litterDateLabel = t('animal.date_unknown');
            if (!empty($litter['birth_date'])) {
                $litterDate = DateTimeImmutable::createFromFormat('Y-m-d', (string)$litter['birth_date']);
                if ($litterDate) {
                    $litterDateLabel = $litterDate->format('d/m/Y');
                }
            }
            ?>
        <div class="col-12">
            <div class="card card-sm">
                <div class="card-header">
                    <div>
                        <h4 class="card-title mb-1"><?= h(t('animal.litter_on', ['date' => $litterDateLabel])) ?></h4>
                        <div class="text-muted small">
                            <?= h(t('animal.mother')) ?> :
                            <?= $litter['mother_id'] ? '<a href="/animal?id=' . (int)$litter['mother_id'] . '">' . h($litter['mother_name']) . '</a>' : h(t('animal.unknown_female')) ?>
                            · <?= h(t('animal.father')) ?> :
                            <?= $litter['father_id'] ? '<a href="/animal?id=' . (int)$litter['father_id'] . '">' . h($litter['father_name']) . '</a>' : h(t('animal.unknown_male')) ?>
                        </div>
                    </div>
                    <a class="btn btn-outline-primary btn-sm ms-auto"
                        href="/litter/add-existing?litter_id=<?= (int)$litter['id'] ?>&amp;animal_id=<?= (int)$animal['id'] ?>">+
                        <?= h(t('animal.add_existing_cat')) ?></a>
                </div>
                <div class="list-group list-group-flush">
                    <?php foreach ($litter['kittens'] as $index => $kitten): ?>
                    <a class="list-group-item list-group-item-action d-flex align-items-center gap-2"
                        href="/animal?id=<?= (int)$kitten['id'] ?>">
                        <span
                            class="text-muted font-monospace"><?= $index === count($litter['kittens']) - 1 ? '└' : '├' ?></span>
                        <span class="coat-swatch" style="<?= h(coat_swatch_style($kitten['color'] ?? '')) ?>"></span>
                        <strong><?= h($kitten['name']) ?></strong>
                        <?php if (!empty($kitten['archived_at'])): ?><span
                            class="badge bg-secondary-lt"><?= h(t('animal.archived')) ?></span><?php endif; ?>
                        <span
                            class="text-muted"><?= h(($kitten['sex'] ?? 'U') === 'F' ? t('sex.female') : (($kitten['sex'] ?? 'U') === 'M' ? t('sex.male') : t('animal.sex_define'))) ?></span>
                        <?php [$kittenStatusClass, $kittenStatusLabel] = status_badge((string)$kitten['status']); ?>
                        <span class="badge <?= h($kittenStatusClass) ?> ms-auto"><?= h($kittenStatusLabel) ?></span>
                    </a>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div><?php endif; ?>
