<?php $roleColors = ['fa' => 'cyan','benevole' => 'green','dirigeant' => 'purple','veterinaire' => 'red','cabinet' => 'orange','crematorium' => 'dark','adoptant' => 'blue','deposant' => 'yellow','fourriere' => 'azure'];
$contactRoles = array_filter(explode(',', (string)$contact['roles']));
$money = static fn (int$cents): string => number_format($cents / 100, 2, ',', ' ') . ' €';
$frDate = static function (?string$date): string {
    if (!$date) {
        return'—';
    }try {
        return(new DateTimeImmutable($date))->format('d/m/Y');
    } catch (Throwable) {
        return$date;
    }
};
$animalRows = static function (array$rows, string$title, string$color, string$icon) use ($frDate): void {
    if (!$rows) {
        return;
    } ?><div class="card mb-3">
    <div class="card-status-top bg-<?= h($color) ?>"></div>
    <div class="card-header"><span class="fs-2 me-2"><?= $icon ?></span>
        <h3 class="card-title"><?= h($title) ?></h3><span
            class="badge bg-<?= h($color) ?>-lt ms-2"><?= count($rows) ?></span>
    </div>
    <div class="list-group list-group-flush"><?php foreach ($rows as $row): ?><a
            class="list-group-item list-group-item-action d-flex align-items-center justify-content-between gap-3"
            href="/animal?id=<?= (int)$row['id'] ?>"><span><strong><?= h($row['name']) ?></strong><span
                    class="text-muted small ms-2"><?= h($row['internal_code']) ?> ·
                    <?= h(species_label($row['species'])) ?></span></span><span
                class="text-muted small"><?= h($frDate($row['event_date'] ?? null)) ?></span></a><?php endforeach; ?>
    </div>
</div><?php }; ?>
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
    <div class="d-flex flex-wrap gap-2"><a class="btn" href="/directory">←
            <?= h(t('directory.back')) ?></a><?php if ($expensePeriodRequested): ?><a class="btn btn-outline-orange"
            href="/statistics?<?= h(http_build_query(['year' => $expensePeriodYear ?? 'all','municipality' => $expensePeriodMunicipality])) ?>">←
            <?= h(t('directory.back_statistics')) ?></a><?php endif; ?></div><a class="btn btn-primary"
        href="/directory/edit?id=<?= (int)$contact['id'] ?>"><?= h(t('common.edit')) ?></a>
</div>
<div class="row g-3 mb-3">
    <div class="col-lg-7">
        <div class="card h-100">
            <div class="card-body">
                <div class="d-flex align-items-start justify-content-between gap-3">
                    <div>
                        <div class="text-muted">
                            <?= h(t($contact['kind'] === 'organization' ? 'directory.organization' : 'directory.person')) ?>
                        </div>
                        <h2 class="mb-2"><?= h($contact['name']) ?></h2>
                        <div class="d-flex flex-wrap gap-1"><?php foreach ($contactRoles as $role): ?><span
                                class="badge bg-<?= h($roleColors[$role] ?? 'secondary') ?>-lt"><?= h($roles[$role] ?? $role) ?></span><?php endforeach; ?>
                        </div>
                    </div>
                    <div class="fs-1"><?= $contact['kind'] === 'organization' ? '🏢' : '👤' ?></div>
                </div><?php if ($contact['organization_name']): ?><div class="mt-3"><span
                        class="text-muted"><?= h(t('directory.organization')) ?> :</span>
                    <?= h($contact['organization_name']) ?></div><?php endif; ?><?php if ($contact['notes']): ?>
                <hr>
                <div class="text-muted small"><?= nl2br(h($contact['notes'])) ?></div><?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card h-100">
            <div class="card-header">
                <h3 class="card-title"><?= h(t('directory.contact_details')) ?></h3>
            </div>
            <div class="card-body"><?php if ($contact['phone']): ?><div class="mb-2">☎ <a
                        href="tel:<?= h(preg_replace('/[^0-9+]/', '', $contact['phone'])) ?>"><?= h($contact['phone']) ?></a>
                </div><?php endif; ?><?php if ($contact['email']): ?><div class="mb-2">✉ <a
                        href="mailto:<?= h($contact['email']) ?>"><?= h($contact['email']) ?></a></div><?php endif; ?><?php $address = implode(', ', array_filter([$contact['address'],trim(($contact['postal_code'] ?? '') . ' ' . ($contact['city'] ?? '')),$contact['country']]));
if ($address): ?><div>📍 <?= h($address) ?></div>
                <?php endif; ?><?php if (!$contact['phone'] && !$contact['email'] && !$address): ?><div
                    class="text-muted"><?= h(t('directory.no_contact_details')) ?></div><?php endif; ?></div>
        </div>
    </div>
</div>
<?php if ($contact['kind'] === 'organization'): ?><div class="card mb-3">
    <div class="card-status-top bg-orange"></div>
    <div class="card-body">
        <div class="row align-items-center">
            <div class="col">
                <div class="subheader"><?= h(t('directory.total_invoiced')) ?><?php if ($expensePeriodRequested): ?> ·
                    <?= h($expensePeriodYear ?? t('statistics.all_years')) ?><?php if ($expensePeriodMunicipality !== ''): ?>
                    · <?= h($expensePeriodMunicipality) ?><?php endif; ?><?php endif; ?></div>
                <div class="h1 mb-0 text-orange"><?= h($money((int)$expenseTotal)) ?></div>
                <div class="text-muted small"><?= h(t('directory.expense_count', ['count' => (int)$expenseCount])) ?>
                </div>
            </div>
            <div class="col-auto fs-1">💶</div>
        </div>
    </div>
</div><?php endif; ?>
<?php $animalRows($adoptions, t('directory.adopted_animals'), 'blue', '🐾');
$animalRows($fosters, t('directory.fostered_animals'), 'cyan', '🏠');
$animalRows($deposited, t('directory.deposited_animals'), 'yellow', '📥'); ?>
<?php if ($linkedPeople): ?><div class="card mb-3">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('directory.linked_people')) ?></h3><span
            class="badge bg-secondary-lt ms-2"><?= count($linkedPeople) ?></span>
    </div>
    <div class="list-group list-group-flush"><?php foreach ($linkedPeople as $person): ?><a
            class="list-group-item list-group-item-action"
            href="/directory/view?id=<?= (int)$person['id'] ?>"><strong><?= h($person['name']) ?></strong><?php if ($person['phone'] || $person['email']): ?><span
                class="text-muted small ms-2"><?= h($person['phone'] ?: $person['email']) ?></span><?php endif; ?></a><?php endforeach; ?>
    </div>
</div><?php endif; ?>
<?php if ($expenses): ?><div class="card">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('directory.expense_history')) ?></h3>
    </div>
    <div class="table-responsive">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th><?= h(t('common.date')) ?></th>
                    <th><?= h(t('animal.animal')) ?></th>
                    <th><?= h(t('common.label')) ?></th>
                    <th class="text-end"><?= h(t('common.amount')) ?></th>
                </tr>
            </thead>
            <tbody><?php foreach ($expenses as $expense): ?><tr>
                    <td><?= h($frDate($expense['occurred_on'])) ?></td>
                    <td><a href="/animal?id=<?= (int)$expense['animal_id'] ?>"><?= h($expense['animal_name']) ?></a>
                        <div class="small text-muted"><?= h($expense['internal_code']) ?></div>
                    </td>
                    <td><?= h($expense['label']) ?></td>
                    <td class="text-end fw-bold"><?= h($money((int)$expense['total_cents'])) ?></td>
                </tr><?php endforeach; ?></tbody>
            <tfoot>
                <tr>
                    <th colspan="3" class="text-end"><?= h(t('common.total')) ?></th>
                    <th class="text-end"><?= h($money((int)$expenseTotal)) ?></th>
                </tr>
            </tfoot>
        </table>
    </div>
</div><?php endif; ?>
