<?php snippet('header') ?>

<main class="article">
  <article class="article__content">
    <!-- Header -->
    <header class="article__header">
      <?php if ($page->date()->isNotEmpty()): ?>
        <time class="article__date" datetime="<?= $page->date()->toDate('Y-m-d') ?>">
          Publié le <?= $page->date()->toDate('d/m/Y') ?>
        </time>
      <?php endif ?>

      <h1 class="article__title"><?= $page->title() ?></h1>

      <?php if ($page->intro()->isNotEmpty()): ?>
        <p class="article__intro"><?= $page->intro() ?></p>
      <?php endif ?>
    </header>

    <!-- Author -->
    <?php if ($page->author_name()->isNotEmpty()): ?>
      <div class="article__author">
        <?php if ($photo = $page->author_photo()->toFile()): ?>
          <img src="<?= $photo->url() ?>" alt="<?= $page->author_name() ?>" class="article__author-photo">
        <?php endif ?>

        <div class="article__author-info">
          <span class="article__author-name"><?= $page->author_name() ?></span>
          <?php if ($page->author_role()->isNotEmpty()): ?>
            <span class="article__author-role"><?= $page->author_role() ?></span>
          <?php endif ?>
        </div>
      </div>
    <?php endif ?>

    <!-- Cover Image -->
    <?php if ($cover = $page->cover()->toFile()): ?>
      <figure class="article__cover">
        <img src="<?= $cover->url() ?>" alt="<?= $page->title() ?>">
      </figure>
    <?php endif ?>

    <!-- Content -->
    <div class="article__body">
      <?= $page->article_content()->toBlocks() ?>
    </div>

    <!-- Tags -->
    <?php if ($page->tags()->isNotEmpty()): ?>
      <div class="article__tags">
        <?php foreach ($page->tags()->split() as $tag): ?>
          <span class="article__tag"><?= $tag ?></span>
        <?php endforeach ?>
      </div>
    <?php endif ?>
  </article>

  <!-- Recommendations -->
  <?php
  $related = $page->related_articles()->toPages();
  if ($related->isEmpty()) {
    $related = $page->siblings()->listed()->not($page)->shuffle()->limit(3);
  }
  ?>

  <?php if ($related->isNotEmpty()): ?>
    <section class="article__recommendations">
      <h2 class="article__recommendations-title">Nos recommandations</h2>
      <a href="<?= $page->parent()->url() ?>" class="article__recommendations-link">Voir tous les articles &rarr;</a>

      <div class="article__recommendations-grid">
        <?php foreach ($related as $rec): ?>
          <article class="recommendation-card">
            <?php if ($cover = $rec->cover()->toFile()): ?>
              <div class="recommendation-card__cover">
                <img src="<?= $cover->thumb(['width' => 400])->url() ?>" alt="<?= $rec->title() ?>">
              </div>
            <?php endif ?>

            <?php if ($rec->category()->isNotEmpty()): ?>
              <span class="recommendation-card__category"><?= $rec->category() ?></span>
            <?php endif ?>

            <h3 class="recommendation-card__title">
              <a href="<?= $rec->url() ?>"><?= $rec->title() ?></a>
            </h3>
          </article>
        <?php endforeach ?>
      </div>
    </section>
  <?php endif ?>

  <!-- Navigation -->
  <nav class="article__nav">
    <a href="<?= $page->parent()->url() ?>" class="article__nav-back">
      &larr; Tous les articles
    </a>
  </nav>
</main>

<?php snippet('footer') ?>
