<?php if ($canViewMedical): ?>
<div class="card mt-3">
    <div class="card-body d-flex flex-wrap align-items-center gap-3">
        <?php $expenseCount = count($expenses ?? []); ?><div class="me-auto">
            <div class="subheader"><?= h(t('expense.estimated_vet')) ?></div>
            <div class="text-muted small"><?= h(t('expense.recorded', ['count' => $expenseCount])) ?></div>
        </div>
        <div class="h2 mb-0"><?= number_format(($expenseTotal ?? 0) / 100, 2, ',', ' ') ?> €</div>
        <?php if (!empty($expenses)): ?><button class="btn btn-sm btn-outline-secondary" type="button"
            data-bs-toggle="collapse" data-bs-target="#animalExpenseDetails"
            aria-expanded="false"><?= h(t('expense.details')) ?></button><?php endif; ?>
    </div>
    <?php if (!empty($expenses)): ?><div class="collapse" id="animalExpenseDetails">
        <div class="table-responsive border-top">
            <table class="table table-sm table-vcenter mb-0">
                <thead>
                    <tr>
                        <th><?= h(t('common.date')) ?></th>
                        <th><?= h(t('expense.expense')) ?></th>
                        <th><?= h(t('pricing.structure')) ?></th>
                        <th><?= h(t('pricing.discount')) ?></th>
                        <th class="text-end"><?= h(t('expense.total')) ?></th>
                    </tr>
                </thead>
                <tbody><?php foreach ($expenses as $e): ?><tr>
                        <td><?= h(date('d/m/Y', strtotime($e['occurred_on']))) ?></td>
                        <td><?= h($e['label']) ?><?php if ((float)$e['quantity'] !== 1.0): ?><span
                                class="text-muted small"> × <?= h((string)$e['quantity']) ?></span><?php endif; ?></td>
                        <td><?= h($e['clinic_name'] ?: '—') ?></td>
                        <td><?= number_format((float)$e['discount_percent'], 2, ',', ' ') ?> %</td>
                        <td class="text-end fw-bold"><?= number_format($e['total_cents'] / 100, 2, ',', ' ') ?> €</td>
                    </tr><?php endforeach; ?></tbody>
            </table>
        </div>
    </div><?php endif; ?>
</div>
<?php endif; ?>
