<?php
require __DIR__.'/app/bootstrap.php';
$token=strtoupper(trim((string)request('token')));if($token===''){http_response_code(404);exit('Carteira não encontrada.');}
$q=db()->prepare("SELECT p.*,pc.name tutor_name,pc.phone tutor_phone,pc.whatsapp tutor_whatsapp,c.name company_name,c.logo company_logo,c.phone company_phone FROM pets p JOIN pet_clients pc ON pc.id=p.client_id JOIN companies c ON c.id=p.company_id WHERE p.public_token=? AND p.status='active' AND pc.status='active' LIMIT 1");$q->execute([$token]);$p=$q->fetch();if(!$p){http_response_code(404);exit('Carteira não encontrada ou inativa.');}
$vq=db()->prepare("SELECT * FROM pet_vaccines WHERE pet_id=? ORDER BY COALESCE(next_dose_at,expires_at,applied_at) DESC LIMIT 8");$vq->execute([$p['id']]);$vaccines=$vq->fetchAll();
?><!doctype html><html lang="pt-BR"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Carteira Digital • <?=e($p['name'])?></title><style>body{margin:0;background:#eef4ff;color:#162033;font-family:system-ui,-apple-system,Segoe UI,sans-serif;padding:18px}.card{max-width:760px;margin:auto;background:#fff;border-radius:26px;overflow:hidden;box-shadow:0 18px 55px #17345d22}.head{padding:28px;background:linear-gradient(135deg,#145da8,#2496e8);color:#fff}.head h1{margin:5px 0;font-size:34px}.head p{margin:0;opacity:.9}.body{padding:26px}.grid{display:grid;grid-template-columns:1fr 1fr;gap:12px}.box{border:1px solid #dce4ee;border-radius:16px;padding:15px;background:#f9fbfd}.box small{display:block;color:#64748b;margin-bottom:5px}.alert{padding:14px;border-radius:15px;background:#fff7df;border:1px solid #f2d690;margin:14px 0}.btn{display:inline-block;padding:12px 15px;border-radius:12px;background:#145da8;color:#fff;text-decoration:none;font-weight:800;margin-top:8px}@media(max-width:580px){.grid{grid-template-columns:1fr}.head h1{font-size:28px}}</style></head><body><main class="card"><header class="head"><small>🐾 CARTEIRA DIGITAL DO PET</small><h1><?=e($p['name'])?></h1><p><?=e($p['company_name'])?> • Identificação <?=e(substr($p['public_token'],0,8))?></p></header><section class="body"><div class="grid"><div class="box"><small>Espécie / raça</small><strong><?=e(trim(($p['species']??'Pet').' • '.($p['breed']??''),' •'))?></strong></div><div class="box"><small>Tutor</small><strong><?=e($p['tutor_name'])?></strong></div><div class="box"><small>Nascimento</small><strong><?=!empty($p['birth_date'])?e(date('d/m/Y',strtotime($p['birth_date']))):'Não informado'?></strong></div><div class="box"><small>Microchip</small><strong><?=e($p['microchip']?:'Não informado')?></strong></div></div><?php if(!empty($p['allergies'])):?><div class="alert"><strong>⚠️ Alergias</strong><br><?=nl2br(e($p['allergies']))?></div><?php endif;?><?php if(!empty($p['medications'])):?><div class="box" style="margin-top:12px"><small>Medicamentos em uso</small><strong><?=nl2br(e($p['medications']))?></strong></div><?php endif;?><?php if(!empty($p['diseases'])):?><div class="box" style="margin-top:12px"><small>Informações clínicas importantes</small><strong><?=nl2br(e($p['diseases']))?></strong></div><?php endif;?><h2>💉 Vacinas recentes</h2><?php if(!$vaccines):?><p>Nenhuma vacina registrada.</p><?php endif;?><?php foreach($vaccines as $v):?><div class="box" style="margin-bottom:8px"><strong><?=e($v['vaccine_name'])?></strong><br><small>Aplicação: <?=!empty($v['applied_at'])?e(date('d/m/Y',strtotime($v['applied_at']))):'—'?> • Próxima dose: <?=!empty($v['next_dose_at'])?e(date('d/m/Y',strtotime($v['next_dose_at']))):'—'?></small></div><?php endforeach;?><h2>🚨 Contato do tutor</h2><p><?=e(format_phone($p['tutor_whatsapp']?:$p['tutor_phone']))?></p><?php if($p['tutor_whatsapp']||$p['tutor_phone']):?><a class="btn" href="<?=e(whatsapp_url($p['tutor_whatsapp']?:$p['tutor_phone'],'Olá! Encontrei/acesso a carteira digital do pet '.$p['name'].'.'))?>">WhatsApp do tutor</a><?php endif;?></section></main></body></html>
