<?php $e = static fn ($v) => htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
$m = static fn ($v) => number_format((float)$v, 2, ',', ' ') . ' €'; ?>
<!doctype html>
<html lang="<?= h(I18n::locale()) ?>">

<head>
    <meta charset="utf-8">
    <title><?= h(t('grant.annual_report', ['year' => $year])) ?></title>
    <style>
        @page {
            size: A4;
            margin: 15mm
        }

        body {
            font: 11pt Arial, sans-serif;
            color: #222
        }

        h1 {
            color: #76552d;
            margin-bottom: 4px
        }

        h2 {
            margin-top: 24px;
            border-bottom: 2px solid #ddd;
            padding-bottom: 5px
        }

        .meta {
            color: #666
        }

        .grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 8px
        }

        .stat {
            border: 1px solid #ddd;
            border-radius: 7px;
            padding: 10px
        }

        .stat strong {
            font-size: 20px;
            display: block
        }

        table {
            width: 100%;
            border-collapse: collapse
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 6px;
            text-align: left;
            vertical-align: top
        }

        th {
            background: #f3f0eb
        }

        .text {
            white-space: pre-wrap
        }

        .cert {
            margin-bottom: 20px;
            padding: 8px;
            background: #f3f0eb;
            font-size: 9pt
        }

        .cols {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 14px
        }

        .avoid {
            break-inside: avoid
        }

        @media print {
            .no-print {
                display: none
            }
        }

    </style>
</head>

<body><button class="no-print" onclick="window.print()"><?= $e(t('grant.print_report')) ?></button>
    <div class="cert">
        <?= $e(t('grant.generated', ['association' => AppSettings::get('association_name'),'date' => $generatedAt->format('d/m/Y'),'time' => $generatedAt->format("H\hi")])) ?>
    </div>
    <h1><?= $e(t('grant.annual_report', ['year' => $year])) ?></h1>
    <div class="meta"><?= $e(AppSettings::get('association_name')) ?> · SIRET
        <?= $e(AppSettings::get('association_siret')) ?> · RNA <?= $e(AppSettings::get('association_rna')) ?></div>
    <h2><?= $e(t('grant.activity_summary')) ?></h2>
    <div class="grid">
        <?php foreach (['entries' => t('grant.entries'),'exits' => t('grant.exits'),'adoptions' => t('grant.adoptions'),'deaths' => t('grant.deaths'),'births' => t('grant.births'),'vaccinations' => t('grant.vaccinations'),'treatments' => t('export.treatments'),'current' => t('grant.current')] as $key => $label): ?>
        <div class="stat"><strong><?= (int)$stats[$key] ?></strong><?= $e($label) ?></div><?php endforeach; ?></div>
    <p><?= $e(t('grant.avg_adoption')) ?> :
        <strong><?= $stats['avg'] > 0 ? $e(t('common.days', ['count' => round($stats['avg'])])) : $e(t('grant.not_calculable')) ?></strong>.
    </p>
    <h2><?= $e(t('grant.comparison', ['year' => $year - 1])) ?></h2>
    <table>
        <thead>
            <tr>
                <th><?= $e(t('grant.indicator')) ?></th>
                <th><?= $year - 1 ?></th>
                <th><?= $year ?></th>
                <th><?= $e(t('grant.evolution')) ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach (['entries' => t('grant.entries'),'exits' => t('grant.exits'),'adoptions' => t('grant.adoptions'),'deaths' => t('grant.deaths'),'births' => t('grant.births'),'vaccinations' => t('grant.vaccinations'),'treatments' => t('export.treatments')] as $key => $label): $c = $comparison[$key]; ?>
            <tr>
                <td><?= $e($label) ?></td>
                <td><?= (int)$c['previous'] ?></td>
                <td><?= (int)$c['current'] ?></td>
                <td><?= $c['difference'] > 0 ? '+' : '' ?><?= (int)$c['difference'] ?><?= $c['percent'] !== null ? ' (' . number_format($c['percent'], 1, ',', ' ') . ' %)' : '' ?>
                </td>
            </tr><?php endforeach; ?></tbody>
    </table>
    <h2><?= $e(t('statistics.details', 'Detailed breakdown')) ?></h2>
    <div class="cols">
        <?php foreach ([[t('grant.sex_entries'),$details['sex'],[]],[t('grant.age_entry'),$details['ages'],[]],[t('grant.origins'),$details['origins'],[]],[t('grant.death_causes'),$details['deathCauses'],array_combine(array_keys(DeathController::CAUSES), array_map(static fn ($key) => DeathController::causeLabel($key), array_keys(DeathController::CAUSES)))],[t('grant.adoption_stay'),$details['stays'],[]]] as [$title,$items,$labels]): ?>
        <div class="avoid">
            <h3><?= $e($title) ?></h3><?php if ($items): ?><table><?php foreach ($items as $label => $total): ?><tr>
                    <td><?= $e($labels[$label] ?? $label) ?></td>
                    <td><?= (int)$total ?></td>
                </tr><?php endforeach; ?></table><?php else: ?><p class="meta">
                <?= $e(t('common.no_data', 'No data.')) ?></p><?php endif; ?>
        </div><?php endforeach; ?></div>
    <h2><?= $e(t('grant.association_life', 'Association life')) ?></h2>
    <table>
        <tr>
            <th><?= $e(t('grant.members')) ?></th>
            <td><?= (int)$annual['members_count'] ?></td>
            <th><?= $e(t('grant.volunteers')) ?></th>
            <td><?= (int)$annual['volunteers_count'] ?></td>
            <th><?= $e(t('grant.employees')) ?> / <?= $e(t('grant.employee_fte')) ?></th>
            <td><?= (int)$annual['employees_count'] ?> / <?= $e($annual['employees_fte']) ?></td>
        </tr>
    </table>
    <?php foreach (['annual_report' => t('grant.report_activity'),'completed_actions' => t('grant.completed_actions'),'planned_actions' => t('grant.actions_planned'),'beneficiaries' => t('grant.beneficiaries_short'),'in_kind_support' => t('grant.in_kind_short')] as $key => $label): ?><?php if (trim((string)$annual[$key]) !== ''): ?>
    <h3><?= $e($label) ?></h3>
    <div class="text"><?= $e($annual[$key]) ?></div><?php endif; ?><?php endforeach; ?><h2>
        <?= $e(t('grant.financial_summary')) ?></h2>
    <table>
        <tr>
            <th><?= (t('grant.actual_expenses')) ?></th>
            <td><?= $m($annual['actual_expenses']) ?></td>
            <th><?= (t('grant.actual_income')) ?></th>
            <td><?= $m($annual['actual_income']) ?></td>
            <th><?= (t('grant.treasury')) ?></th>
            <td><?= $m($annual['treasury_amount']) ?></td>
        </tr>
        <tr>
            <th><?= (t('grant.forecast_expenses')) ?></th>
            <td><?= $m($annual['forecast_expenses']) ?></td>
            <th><?= (t('grant.forecast_income')) ?></th>
            <td><?= $m($annual['forecast_income']) ?></td>
            <th><?= (t('grant.voluntary_contributions')) ?></th>
            <td><?= $m($annual['voluntary_contributions']) ?></td>
        </tr>
    </table>
    <h2><?= $e(t('grant.applications')) ?></h2>
    <table>
        <thead>
            <tr>
                <th><?= $e(t('grant.organization')) ?></th>
                <th><?= $e(t('grant.deadline')) ?></th>
                <th><?= $e(t('grant.requested')) ?></th>
                <th><?= $e(t('grant.awarded')) ?></th>
                <th><?= $e(t('grant.state')) ?></th>
            </tr>
        </thead>
        <tbody><?php foreach ($applications as $a): ?><tr>
                <td><?= $e($a['organization_name']) ?></td>
                <td><?= $e($a['deadline'] ?: '—') ?></td>
                <td><?= $m($a['requested_amount']) ?></td>
                <td><?= $a['awarded_amount'] === null ? '—' : $m($a['awarded_amount']) ?></td>
                <td><?= $e(GrantService::statusLabel($a['status'])) ?></td>
            </tr><?php endforeach; ?></tbody>
    </table>
</body>

</html>
