<?php
// Variables disponibles : $animal, $medical_notes, $treatments, $vaccinations, $photos, $title

if (!function_exists('h')) {
    function h($s)
    {
        return htmlspecialchars((string)$s, ENT_QUOTES);
    }
}

$baseDir = realpath(__DIR__ . '/../../public');
$photoDir = $baseDir . '/media/animals/' . (int)$animal['id'];

function fileToDataUri(string $path): ?string
{
    if (!is_file($path)) {
        return null;
    }
    $data = file_get_contents($path);
    if ($data === false) {
        return null;
    }
    $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
    $mime = match($ext) {
        'jpg','jpeg' => 'image/jpeg',
        'png' => 'image/png',
        'webp' => 'image/webp',
        'gif' => 'image/gif',
        default => 'application/octet-stream'
    };
    return "data:$mime;base64," . base64_encode($data);
}

?>
<!doctype html>
<html lang="<?= h(I18n::locale()) ?>">

<head>
    <meta charset="utf-8">
    <title><?= h($title) ?></title>
    <style>
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
            color: #111;
        }

        h1 {
            font-size: 18px;
            margin: 0 0 6px;
        }

        h2 {
            font-size: 14px;
            margin: 18px 0 8px;
            border-bottom: 1px solid #ddd;
            padding-bottom: 4px;
        }

        .muted {
            color: #666;
        }

        .grid {
            width: 100%;
            border-collapse: collapse;
        }

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

        .grid th {
            background: #f3f3f3;
            text-align: left;
        }

        .badge {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 10px;
            border: 1px solid #ccc;
            font-size: 11px;
        }

        .row {
            display: block;
        }

        .photos {
            width: 100%;
        }

        .photo {
            display: inline-block;
            width: 32%;
            margin: 0 1% 10px 0;
        }

        .photo img {
            width: 100%;
            height: 140px;
            object-fit: cover;
            border: 1px solid #ddd;
        }

        .small {
            font-size: 11px;
        }

    </style>
</head>

<body>

    <h1><?= h(t('pdf.medical_record', ['name' => $animal['name'] ?? ''])) ?></h1>
    <div class="muted small">
        <?= h(t('pdf.generated_on', ['date' => date('d/m/Y H:i')])) ?>
    </div>

    <h2><?= h(t('pdf.identity')) ?></h2>
    <table class="grid">
        <tr>
            <th><?= h(t('pdf.internal_code')) ?></th>
            <td><?= h($animal['code'] ?? '') ?></td>
            <th><?= h(t('animal.species')) ?></th>
            <td><?= h(match (strtolower((string)($animal['species'] ?? ''))) {
                'cat', 'chat' => t('species.cat'), 'dog', 'chien' => t('species.dog'), 'nac' => t('species.nac'), default => (string)($animal['species'] ?? '')
            }) ?></td>
        </tr>
        <tr>
            <th><?= h(t('animal.sex')) ?></th>
            <td><?= h($animal['sex'] ?? '') ?></td>
            <th><?= h(t('animal.form.birth_date')) ?></th>
            <td><?= h($animal['birth_date'] ?? '') ?></td>
        </tr>
        <tr>
            <th><?= h(t('animal.status')) ?></th>
            <td><?= h($animal['status'] ?? '') ?></td>
            <th><?= h(t('common.notes')) ?></th>
            <td><?= h($animal['notes'] ?? '') ?></td>
        </tr>
    </table>

    <?php if (!empty($photos)): ?>
    <h2><?= h(t('animal.photos')) ?></h2>
    <div class="photos">
        <?php foreach ($photos as $p): ?>
        <?php
              $path = $photoDir . '/' . (string)$p['filename'];
            $uri = fileToDataUri($path);
            if (!$uri) {
                continue;
            }
            ?>
        <div class="photo">
            <img src="<?= h($uri) ?>" alt="">
            <?php if ((int)$p['is_primary'] === 1): ?><div class="small muted"><?= h(t('pdf.main_photo')) ?></div>
            <?php endif; ?>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <h2><?= h(t('pdf.medical_notes')) ?></h2>
    <?php if (empty($medical_notes)): ?>
    <div class="muted"><?= h(t('pdf.no_medical_notes')) ?></div>
    <?php else: ?>
    <table class="grid">
        <tr>
            <th style="width:90px;"><?= h(t('common.date')) ?></th>
            <th style="width:70px;"><?= h(t('animal.form.type')) ?></th>
            <th><?= h(t('pdf.content')) ?></th>
            <th style="width:160px;"><?= h(t('pdf.clinic_vet')) ?></th>
        </tr>
        <?php foreach ($medical_notes as $n): ?>
        <tr>
            <td><?= h(substr((string)$n['noted_at'], 0, 10)) ?></td>
            <td><span class="badge"><?= h($n['kind'] ?? '') ?></span></td>
            <td>
                <?php if (!empty($n['reason'])): ?><b><?= h(t('pdf.reason')) ?>:</b>
                <?= h($n['reason']) ?><br><?php endif; ?>
                <?php if (!empty($n['weight_kg'])): ?><b><?= h(t('pdf.weight')) ?>:</b> <?= h($n['weight_kg']) ?>
                kg<br><?php endif; ?>
                <?php if (!empty($n['temperature_c'])): ?><b><?= h(t('pdf.temperature')) ?>:</b>
                <?= h($n['temperature_c']) ?> °C<br><?php endif; ?>
                <?php if (!empty($n['symptoms'])): ?><b><?= h(t('pdf.symptoms')) ?>:</b>
                <?= nl2br(h($n['symptoms'])) ?><br><?php endif; ?>
                <?php if (!empty($n['exam'])): ?><b><?= h(t('pdf.exam')) ?>:</b>
                <?= nl2br(h($n['exam'])) ?><br><?php endif; ?>
                <?php if (!empty($n['diagnosis'])): ?><b><?= h(t('pdf.diagnosis')) ?>:</b>
                <?= nl2br(h($n['diagnosis'])) ?><br><?php endif; ?>
                <?php if (!empty($n['plan'])): ?><b><?= h(t('pdf.plan')) ?>:</b>
                <?= nl2br(h($n['plan'])) ?><br><?php endif; ?>
            </td>
            <td>
                <?php if (!empty($n['clinic_name'])): ?><?= h($n['clinic_name']) ?><br><?php endif; ?>
                <?php if (!empty($n['vet_name'])): ?><?= h($n['vet_name']) ?><?php endif; ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endif; ?>

    <h2><?= h(t('pdf.treatments')) ?></h2>
    <?php if (empty($treatments)): ?>
    <div class="muted"><?= h(t('pdf.no_treatment')) ?></div>
    <?php else: ?>
    <table class="grid">
        <tr>
            <th style="width:60px;"><?= h(t('pdf.state')) ?></th>
            <th><?= h(t('pdf.medication')) ?></th>
            <th style="width:120px;"><?= h(t('pdf.start')) ?></th>
            <th style="width:120px;"><?= h(t('pdf.end')) ?></th>
            <th style="width:160px;"><?= h(t('pdf.clinic_vet')) ?></th>
            <th><?= h(t('common.notes')) ?></th>
        </tr>
        <?php foreach ($treatments as $t): ?>
        <tr>
            <td><?= h(((int)$t['ongoing'] === 1) ? t('pdf.ongoing') : t('pdf.finished')) ?></td>
            <td><?= h($t['medication_name'] ?? '') ?> <?= !empty($t['molecule']) ? '(' . h($t['molecule']) . ')' : '' ?>
            </td>
            <td><?= h($t['start_date'] ?? '') ?></td>
            <td><?= h($t['end_date'] ?? '') ?></td>
            <td>
                <?php if (!empty($t['clinic_name'])): ?><?= h($t['clinic_name']) ?><br><?php endif; ?>
                <?php if (!empty($t['vet_name'])): ?><?= h($t['vet_name']) ?><?php endif; ?>
            </td>
            <td class="small">
                <?php if (!empty($t['route'])): ?><b><?= h(t('pdf.route')) ?>:</b>
                <?= h($t['route']) ?><br><?php endif; ?>
                <?php if (!empty($t['dose_text'])): ?><b><?= h(t('pdf.dosage')) ?>:</b>
                <?= h($t['dose_text']) ?><br><?php endif; ?>
                <?= !empty($t['notes']) ? nl2br(h($t['notes'])) : '' ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endif; ?>

    <h2><?= h(t('pdf.vaccines')) ?></h2>
    <?php if (empty($vaccinations)): ?>
    <div class="muted"><?= h(t('pdf.no_vaccine')) ?></div>
    <?php else: ?>
    <table class="grid">
        <tr>
            <th><?= h(t('pdf.vaccine')) ?></th>
            <th style="width:110px;"><?= h(t('pdf.done_on')) ?></th>
            <th style="width:110px;"><?= h(t('pdf.booster')) ?></th>
            <th style="width:160px;"><?= h(t('pdf.clinic_vet')) ?></th>
            <th><?= h(t('common.notes')) ?></th>
        </tr>
        <?php foreach ($vaccinations as $v): ?>
        <tr>
            <td><?= h($v['vaccine_name'] ?? '') ?></td>
            <td><?= h($v['done_date'] ?? '') ?></td>
            <td><?= h($v['due_date'] ?? '') ?></td>
            <td>
                <?php if (!empty($v['clinic_name'])): ?><?= h($v['clinic_name']) ?><br><?php endif; ?>
                <?php if (!empty($v['vet_name'])): ?><?= h($v['vet_name']) ?><?php endif; ?>
            </td>
            <td class="small">
                <?php if (!empty($v['lot'])): ?><b><?= h(t('pdf.lot')) ?>:</b> <?= h($v['lot']) ?><br><?php endif; ?>
                <?= !empty($v['notes']) ? nl2br(h($v['notes'])) : '' ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endif; ?>

</body>

</html>
