<?php
/** @var array $rows */
/** @var int $year */
/** @var string $q */
/** @var string $species */
?>
<div class="card">
    <div class="card-body">
        <h3><?= h(t('admin.register')) ?></h3><br />
        <?php if (PermissionService::can('grants') || PermissionService::can('administrative')): ?><div
            class="d-flex justify-content-end gap-2 mb-3"><?php if (PermissionService::can('grants')): ?><a
                class="btn btn-outline-primary" href="/admin/grants">📊
                <?= h(t('admin.grants')) ?></a><?php endif; ?><?php if (PermissionService::can('administrative')): ?><a
                class="btn btn-outline-primary" href="/admin/exports">⬇
                <?= h(t('admin.export_data')) ?></a><?php endif; ?></div><?php endif; ?>
        <div class="card mb-3">
            <button class="card-header w-100 border-0 text-start bg-transparent d-flex align-items-center" type="button"
                data-bs-toggle="collapse" data-bs-target="#administrativeDocuments" aria-expanded="false"
                aria-controls="administrativeDocuments">
                <div>
                    <h3 class="card-title mb-1">📄 <?= h(t('admin.documents')) ?> <span
                            class="badge bg-blue-lt ms-1"><?= h(t('admin.models', ['count' => 5])) ?></span></h3>
                    <div class="text-muted small"><?= h(t('admin.models_help')) ?></div>
                </div><span class="ms-auto text-muted fs-2">⌄</span>
            </button>
            <div class="collapse" id="administrativeDocuments">
                <div class="card-body border-top">
                    <div class="vstack gap-3">
                        <?php foreach ([
  'adoption' => [t('admin.adoption_document'),'Version 1.5'],
  'abandon' => [t('admin.abandon_document'),'Version 1.0'],
  'benevole' => [t('admin.volunteer_document'),'Version 2.0'],
  'fa' => [t('admin.foster_document'),'Version 1.1'],
  'pre_adoption' => [t('admin.pre_adoption_document'),'Version 1.1'],
] as $documentType => $document): ?>
                        <div class="d-flex flex-wrap align-items-center gap-2">
                            <div class="me-auto">
                                <div class="fw-bold"><?= h($document[0]) ?></div>
                                <div class="text-muted small"><?= h($document[1]) ?> — <?= h(t('admin.blank_model')) ?>
                                </div>
                            </div><a class="btn btn-outline-primary"
                                href="/documents/blank?type=<?= h($documentType) ?>&format=odt"><?= h(t('animal.download_odt')) ?></a><a
                                class="btn btn-danger" href="/documents/blank?type=<?= h($documentType) ?>&format=pdf"
                                target="_blank"><?= h(t('animal.view_print_pdf')) ?></a>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>

        <form class="row g-2 align-items-end" method="get" action="/admin/register">
            <div class="col-md-2">
                <label class="form-label"><?= h(t('admin.year')) ?></label>
                <select class="form-select" name="year">
                    <?php
$currentYear = (int)date('Y');

for ($y = $currentYear; $y >= $currentYear - 10; $y--):
    ?>
                    <option value="<?= $y ?>" <?= $year == $y ? 'selected' : '' ?>>
                        <?= $y ?>
                    </option>
                    <?php endfor; ?>
                </select>
            </div>

            <div class="col-md-3">
                <label class="form-label"><?= h(t('admin.search')) ?></label>
                <input class="form-control" name="q" value="<?= h($q) ?>"
                    placeholder="<?= h(t('admin.search_placeholder')) ?>">
            </div>

            <div class="col-md-3">
                <label class="form-label"><?= h(t('animal.species')) ?></label>
                <select class="form-select" name="species">
                    <option value="">— <?= h(t('admin.all_species')) ?> —</option>
                    <option value="chat" <?= strtolower($species) === 'chat' ? 'selected' : '' ?>>
                        <?= h(t('admin.cat')) ?></option>
                    <option value="chien" <?= strtolower($species) === 'chien' ? 'selected' : '' ?>>
                        <?= h(t('admin.dog')) ?></option>
                    <option value="nac" <?= strtolower($species) === 'nac' ? 'selected' : '' ?>>NAC</option>
                </select>
            </div>

            <div class="col-md-4 d-flex gap-2">
                <button class="btn btn-primary" type="submit"><?= h(t('common.filter')) ?></button>
                <a class="btn btn-outline-secondary" href="/admin/register"><?= h(t('common.reset')) ?></a>
                <a class="btn btn-danger"
                    href="/admin/register/pdf?year=<?= $year ?>&q=<?= urlencode($q) ?>&species=<?= urlencode($species) ?>&sort=<?= $sort ?>&dir=<?= strtolower($dir) ?>">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-file-text" width="18"
                        height="18" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                        stroke-linecap="round" stroke-linejoin="round">
                        <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                        <path d="M14 3v4a1 1 0 0 0 1 1h4" />
                        <path d="M17 21h-10a2 2 0 0 1 -2 -2v-14a2 2 0 0 1 2 -2h7l5 5v11a2 2 0 0 1 -2 2z" />
                        <path d="M9 9l1 0" />
                        <path d="M9 13l6 0" />
                        <path d="M9 17l6 0" />
                    </svg>
                    <?= h(t('admin.print_register')) ?>
                </a>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="table-responsive">
        <table class="table table-vcenter table-striped">
            <thead>
                <tr>
                    <th>#</th>
                    <th><?= sortLink('type', t('animal.form.type'), $sort, $dir, $year, $q, $species) ?></th>
                    <th><?= sortLink('date', t('common.date'), $sort, $dir, $year, $q, $species) ?></th>
                    <th><?= sortLink('name', t('common.name'), $sort, $dir, $year, $q, $species) ?></th>
                    <th><?= sortLink('sex', t('animal.sex'), $sort, $dir, $year, $q, $species) ?></th>
                    <th><?= sortLink('species', t('animal.species'), $sort, $dir, $year, $q, $species) ?></th>
                    <th><?= h(t('admin.birth')) ?></th>
                    <th><?= h(t('admin.identification')) ?></th>
                    <th><?= h(t('admin.reason_contact')) ?></th>
                </tr>
            </thead>
            <tbody>
                <?php $i = 0;
foreach ($rows as $r): $i++; ?>
                <?php
    $rowClass = '';

    switch ($r['kind']) {
        case 'entry':
            $type_txt = t('admin.entry');
            $type_color = 'green';
            break;

        case 'exit':
            $type_txt = t('admin.exit');
            $type_color = 'red';
            break;

        default:
            $type_txt = ucfirst($r['kind']);
            $type_color = 'secondary';
    }

    switch ($r['lieu']) {
        case 'Adoption':
            $type2_txt = t('admin.adoption');
            $type2_color = 'azure';
            break;

        case 'Décès du propriétaire':
            $type2_txt = t('admin.owner_death');
            $type2_color = 'red';
            break;

        case 'Décès':
        case 'Décès de l\'animal':
            $type2_txt = t('admin.animal_death');
            $type2_color = 'red';
            break;

        case 'Sauvetage':
            $type2_txt = t('admin.rescue');
            $type2_color = 'green';
            break;

        default:
            $type2_txt = h($r['lieu']);
            $type2_color = 'secondary';
    }
    ?>
                <tr>
                    <td class="text-muted"><?= $i ?></td>

                    <td>
                        <span class="badge bg-<?= $type_color; ?>-lt"><?= $type_txt; ?></span>
                    </td>

                    <td><?= $r['created_at'] ? h(substr($r['created_at'], 0, 10)) : '—' ?></td>

                    <td>
                        <a href="/animal?id=<?= (int)$r['animal_id'] ?>" class="fw-bold"><?= h($r['name'] ?? '') ?></a>
                        <?php if (!empty($r['internal_code'])): ?>
                        <div class="text-muted small"><?= h($r['internal_code']) ?></div>
                        <?php endif; ?>
                    </td>

                    <td class="text-center"><?= $r['sex'] ? h($r['sex']) : '—' ?></td>
                    <td class="text-center"><?= h(species_label($r['species'] ?? '')) ?></td>
                    <td class="text-center"><?= $r['birth_date'] ? h($r['birth_date']) : '—' ?></td>
                    <td><?= $r['chip_id'] ? h($r['chip_id']) : '—' ?></td>
                    <td>
                        <?php if (!empty($r['lieu'])): ?>
                        <span class="badge bg-<?= $type2_color; ?>-lt"><?= $type2_txt; ?></span><br />

                        <?= $r['contact_name'] && empty($r['death_cause_code']) ? h($r['contact_name']) . '<br />' : '' ?>
                        <?= $r['place'] ? h($r['place']) . '<br />' : '' ?>
                        <?= $r['contact_phone'] ? h($r['contact_phone']) . '<br />' : '' ?>
                        <?= $r['contact_email'] ? h($r['contact_email']) . '<br />' : '' ?>

                        <?php if (!empty($r['death_cause_code'])): ?>
                        <div class="mt-1 small"><strong><?= h(t('animal.cause')) ?> :</strong>
                            <?= h(DeathController::causeLabel((string)$r['death_cause_code'])) ?><?= !empty($r['death_cause_details']) ? ' — ' . h($r['death_cause_details']) : '' ?>
                        </div>
                        <?php if ((int)$r['death_euthanized'] === 1): ?><div class="small">
                            <strong><?= h(t('animal.euthanasia')) ?> :</strong> <?= h(t('common.yes')) ?></div>
                        <?php endif; ?>
                        <?php if (!empty($r['death_veterinarian'])): ?><div class="small">
                            <strong><?= h(t('animal.form.veterinarian')) ?> :</strong>
                            <?= h($r['death_veterinarian']) ?></div><?php endif; ?>
                        <?php if (!empty($r['death_crematorium'])): ?><div class="small">
                            <strong><?= h(t('admin.crematorium')) ?> :</strong> <?= h($r['death_crematorium']) ?></div>
                        <?php endif; ?>
                        <div class="small"><strong><?= h(t('animal.care')) ?> :</strong>
                            <?= h(DeathController::dispositionLabel((string)$r['death_disposition'])) ?></div>
                        <?php endif; ?>


                        <?php else: ?>
                        —
                        <?php endif; ?>
                    </td>

                </tr>
                <?php endforeach; ?>

                <?php if (!$rows): ?>
                <tr>
                    <td colspan="12" class="text-center text-muted py-4"><?= h(t('admin.no_data')) ?></td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
