<?php
// Sous-vue de la fiche animale ; les variables sont préparées par AnimalsController::show().
?>
<!-- EMPLACEMENTS -->
<div class="tab-pane" id="tab-placements">
    <div class="d-flex justify-content-between align-items-start gap-2 mb-3">
        <div>
            <h3 class="mb-1"><?= h(t('animal.placement_journey')) ?></h3>
            <div class="text-muted"><?= h(t('animal.placement_journey_help')) ?></div>
        </div><button class="btn btn-primary btn-sm" data-bs-toggle="modal"
            data-bs-target="#modalPlacement"><?= h(t('animal.add_event')) ?></button>
    </div>
    <?php $placementLabels = ['reservation' => t('animal.reservation'),'foster' => t('animal.form.foster'),'adoption' => t('animal.final_adoption'),'return' => t('animal.return_shelter'),'cancellation' => t('animal.cancellation')];
$placementColors = ['reservation' => 'purple','foster' => 'cyan','adoption' => 'green','return' => 'orange','cancellation' => 'secondary']; ?>
    <?php if (empty($placements)): ?><div class="text-muted"><?= h(t('animal.no_placement')) ?></div><?php else: ?><ul
        class="list list-timeline"><?php foreach ($placements as $placement): ?><li>
            <div class="list-timeline-icon bg-<?= h($placementColors[$placement['event_type']] ?? 'secondary') ?>"><i
                    class="ti ti-home"></i></div>
            <div class="list-timeline-content">
                <div class="d-flex justify-content-between gap-2">
                    <div><span
                            class="badge bg-<?= h($placementColors[$placement['event_type']] ?? 'secondary') ?>-lt"><?= h($placementLabels[$placement['event_type']] ?? $placement['event_type']) ?></span><?php if (!empty($placement['contact_name'])): ?>
                        <strong class="ms-2"><?= h($placement['contact_name']) ?></strong><?php endif; ?></div>
                    <div class="text-muted small"><?= h(date('d/m/Y', strtotime((string)$placement['event_date']))) ?>
                    </div>
                </div><?php if (!empty($placement['reason'])): ?><div class="mt-1"><strong><?= h(t('animal.reason')) ?>
                        :</strong> <?= h($placement['reason']) ?></div>
                <?php endif; ?><?php if (!empty($placement['notes'])): ?><div class="text-muted">
                    <?= nl2br(h($placement['notes'])) ?></div>
                <?php endif; ?><?php if (!empty($placement['actor_name'])): ?><div class="text-muted small">
                    <?= h(t('common.by', ['name' => $placement['actor_name']])) ?></div><?php endif; ?>
            </div>
        </li><?php endforeach; ?></ul><?php endif; ?>
</div>

<div class="tab-pane" id="tab-locations">
    <div class="d-flex flex-wrap justify-content-between align-items-start gap-2 mb-3">
        <div>
            <h3 class="mb-1"><?= h(t('animal.location_history')) ?></h3>
            <div class="text-muted"><?= h(t('animal.location_history_help')) ?></div>
        </div>
        <span class="badge bg-blue-lt"><?= h(tn('animal.step', 'animal.steps', count($locationHistory ?? []))) ?></span>
    </div>
    <?php if (empty($locationHistory)): ?>
    <div class="text-muted"><?= h(t('animal.no_location')) ?></div>
    <?php else: ?>
    <ul class="list list-timeline">
        <?php foreach ($locationHistory as $location):
            $fromLabel = LocationHistoryService::label($location, 'from');
            $toLabel = LocationHistoryService::label($location, 'to');
            $isInitial = empty($location['from_status']) && empty($location['from_refuge_room']) && empty($location['from_care_box_key']) && empty($location['from_address']);
            ?>
        <li>
            <div class="list-timeline-icon bg-blue"><i class="ti ti-map-pin"></i></div>
            <div class="list-timeline-content">
                <div class="d-flex flex-wrap justify-content-between gap-2">
                    <div class="fw-bold">
                        <?= $isInitial ? h($toLabel) : h($fromLabel) . ' <span class="text-muted mx-1">→</span> ' . h($toLabel) ?>
                    </div>
                    <div class="text-muted small"><?= h($location['moved_at']) ?></div>
                </div>
                <?php if (!empty($location['reason'])): ?><div class="text-muted mt-1"><?= h($location['reason']) ?>
                </div><?php endif; ?>
                <?php if (!empty($location['actor_name'])): ?><div class="text-muted small mt-1">
                    <?= h(t('common.by', ['name' => $location['actor_name']])) ?></div><?php endif; ?>
            </div>
        </li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>
</div>
