<?php
$escape = static fn ($value): string => htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
$speciesLabel = static fn ($value): string => match (strtolower((string)$value)) {
    'cat', 'chat' => t('species.cat'), 'dog', 'chien' => t('species.dog'), 'nac' => t('species.nac'), default => ucfirst((string)$value),
};
?>
<!doctype html>
<html lang="<?= $escape(I18n::locale()) ?>">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title><?= $escape(t('pdf.register', ['year' => (int)$year])) ?></title>
    <style>
        @page {
            size: A4 portrait;
            margin: 14mm 10mm 18mm
        }

        * {
            box-sizing: border-box
        }

        body {
            margin: 0;
            color: #111;
            font: 10px Arial, Helvetica, sans-serif
        }

        h1 {
            margin: 0 0 14px;
            text-align: center;
            font-size: 18px
        }

        .meta {
            margin: -8px 0 12px;
            text-align: center;
            color: #626976
        }

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

        thead {
            display: table-header-group
        }

        tr {
            break-inside: avoid
        }

        th {
            background: #f0f0f0;
            border: 1px solid #bbb;
            padding: 6px;
            text-align: left
        }

        td {
            border: 1px solid #ddd;
            padding: 5px;
            vertical-align: top
        }

        tbody tr:nth-child(even) {
            background: #fafafa
        }

        .entry {
            color: #1b7a1b;
            font-weight: 700
        }

        .exit {
            color: #b71c1c;
            font-weight: 700
        }

        .small {
            color: #666;
            font-size: 8px
        }

        .toolbar {
            position: sticky;
            top: 0;
            z-index: 10;
            display: flex;
            justify-content: flex-end;
            gap: 8px;
            margin: 0 0 16px;
            padding: 10px;
            background: #fff;
            border-bottom: 1px solid #ddd
        }

        .toolbar a,
        .toolbar button {
            border: 1px solid #1d273b;
            border-radius: 6px;
            padding: 8px 13px;
            background: #fff;
            color: #1d273b;
            font: 600 14px Arial, sans-serif;
            text-decoration: none;
            cursor: pointer
        }

        .toolbar button {
            background: #1d273b;
            color: #fff
        }

        .document-certification {
            margin: 0 0 12px;
            padding: 7px 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background: #f7f7f7;
            color: #626976;
            font-size: 9px
        }

        @media print {
            .toolbar {
                display: none !important
            }
        }

    </style>
</head>

<body>
    <div class="toolbar"><a href="/admin/register?year=<?= (int)$year ?>">←
            <?= $escape(t('pdf.back_register')) ?></a><button type="button" onclick="window.print()">🖨
            <?= $escape(t('pdf.print')) ?></button></div>
    <div class="document-certification">
        <?= $escape(t('pdf.generated_by', ['association' => AppSettings::get('association_name'),'date' => $generatedAt->format('d/m/Y'),'time' => $generatedAt->format('H\\hi')])) ?>
    </div>
    <h1><?= $escape(t('pdf.register', ['year' => (int)$year])) ?></h1>
    <div class="meta"><?= $escape(t('pdf.movements', ['count' => count($rows)])) ?></div>
    <table>
        <thead>
            <tr>
                <th><?= $escape(t('common.date')) ?></th>
                <th><?= $escape(t('animal.form.type')) ?></th>
                <th><?= $escape(t('common.name')) ?></th>
                <th><?= $escape(t('animal.sex')) ?></th>
                <th><?= $escape(t('animal.species')) ?></th>
                <th><?= $escape(t('animal.birth')) ?></th>
                <th><?= $escape(t('animal.form.identification')) ?></th>
                <th><?= $escape(t('pdf.reason_contact')) ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): $isEntry = ($row['kind'] ?? '') === 'entry'; ?><tr>
                <td><?= $escape(substr((string)$row['created_at'], 0, 10)) ?></td>
                <td class="<?= $isEntry ? 'entry' : 'exit' ?>"><?= $escape($isEntry ? t('pdf.entry') : t('pdf.exit')) ?>
                </td>
                <td><strong><?= $escape($row['name']) ?></strong><br><span
                        class="small"><?= $escape($row['internal_code']) ?></span></td>
                <td><?= $escape($row['sex']) ?></td>
                <td><?= $escape($speciesLabel($row['species'])) ?></td>
                <td><?= $escape($row['birth_date']) ?></td>
                <td><?= $escape($row['chip_id']) ?></td>
                <td><?= $escape($row['lieu'] ?? '') ?><br><?= $escape($row['contact_name'] ?? '') ?><br><?= $escape($row['place'] ?? '') ?><br><?= $escape($row['contact_phone'] ?? '') ?><br><?= $escape($row['contact_email'] ?? '') ?>
                </td>
            </tr><?php endforeach; ?>
            <?php if (!$rows): ?><tr>
                <td colspan="8" style="padding:20px;text-align:center;color:#666"><?= $escape(t('pdf.no_movement')) ?>
                </td>
            </tr><?php endif; ?>
        </tbody>
    </table>
</body>

</html>
