<?php
require_once __DIR__ . '/inc/template.php';
require_once __DIR__ . '/inc/data.php';

$slug = isset($_GET['slug']) ? preg_replace('/[^a-z0-9-]/','',$_GET['slug']) : '';
$t = $slug ? temoignage_par_slug($slug) : null;

if (!$t) {
    http_response_code(404);
    page_title('Témoignage introuvable');
    ob_start(); ?>
    <section class="wrap" style="padding:6rem 2rem;text-align:center">
      <h1 class="cor" style="font-size:2rem">Témoignage introuvable</h1>
      <p style="color:var(--ink2);margin-top:1rem">Ce témoignage n'existe pas ou n'est plus disponible.</p>
      <p style="margin-top:1.5rem"><a class="btn btn-blue" href="<?= url_temoignages() ?>">Voir tous les témoignages</a></p>
    </section>
    <?php
    render_page(ob_get_clean());
    exit;
}

page_active('temoignages');
page_title($t['titre']);
page_desc(!empty($t['meta_desc']) ? $t['meta_desc'] : extrait_texte($t['contenu'], 155));
if (!empty($t['meta_titre'])) page_title($t['meta_titre']);
if ($t['image_url']) page_og_image(SITE_URL.'/'.$t['image_url']);

$schema = [
  '@context'=>'https://schema.org',
  '@type'=>'Review',
  'name'=>$t['titre'],
  'reviewBody'=>extrait_texte($t['contenu'], 500),
  'author'=>['@type'=>'Person','name'=>(!empty($t['auteur']) ? $t['auteur'] : 'Patient')],
  'itemReviewed'=>[
    '@type'=>'MedicalBusiness',
    'name'=>'Tuina Loygue',
    'address'=>['@type'=>'PostalAddress','addressLocality'=>'Caen','addressRegion'=>'Normandie','addressCountry'=>'FR'],
  ],
];
if (!empty($t['date_pub'])) $schema['datePublished'] = substr($t['date_pub'],0,10);
page_head('<script type="application/ld+json">'.json_encode($schema,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).'</script>');

ob_start();
?>
<article class="wrap tem-single">
  <div class="breadcrumb breadcrumb-dark"><?php
    $bc = [['Témoignages', '/temoignages/']];
    if (!empty($t['type_nom']) && !empty($t['type_slug'])) $bc[] = [$t['type_nom'], url_type($t['type_slug'])];
    $bc[] = [$t['titre'], null];
    echo fil_ariane($bc);
  ?></div>
  <a href="<?= $t['type_slug'] ? url_type($t['type_slug']) : url_temoignages() ?>" class="retour">← Tous les témoignages</a>

  <?php if ($t['type_nom']): ?><div class="ts-type tag"><?= e($t['type_nom']) ?></div><?php endif; ?>
  <h1 class="ts-titre cor"><?= e($t['titre']) ?></h1>
  <?php if (!empty($t['date_pub'])): ?><div class="ts-date"><?= e(mois_an($t['date_pub'])) ?></div><?php endif; ?>

  <?php if ($t['image_url']): ?>
    <div class="ts-img"><img src="<?= e($t['image_url']) ?>" alt="<?= e($t['image_alt'] ?: $t['titre']) ?>" loading="lazy" decoding="async"></div>
  <?php endif; ?>

  <div class="ts-contenu">
    <?= $t['contenu'] ?>
  </div>

  <div class="ts-foot">
    <a href="<?= url_temoignages() ?>" class="btn btn-o-d">← Tous les témoignages</a>
    <a href="/proposer-temoignage.php" class="btn btn-blue">Proposer mon témoignage</a>
  </div>
</article>
<?php
render_page(ob_get_clean());
