<?php
$previewMode = !empty($preview);
$displayDate = $post['published_at'] ?: ($post['updated_at'] ?? $post['created_at'] ?? date('Y-m-d H:i:s'));
$galleryImages = $post['gallery_images'] ?? [];
if ($galleryImages === [] && !empty($post['image_path'])) {
    $galleryImages[] = ['file_path' => $post['image_path'], 'alt_text' => $post['image_alt'] ?? ''];
}
$inlineImages = [];
foreach (($post['inline_images'] ?? []) as $image) $inlineImages[(int) $image['token']] = $image;
$paragraphs = preg_split('/\R{2,}/', trim($post['body'])) ?: [];
?>
<?php if ($previewMode): ?><aside class="preview-toolbar" role="status"><div><strong><?= $post['status'] === 'published' ? '공개 글 미리보기' : '초안 미리보기' ?></strong><span>저장된 내용을 실제 공개 페이지 형식으로 표시합니다.</span></div><a class="button button-secondary button-small" href="/admin/posts/<?= e($post['id']) ?>/edit">글 수정</a></aside><?php endif; ?>
<article class="article">
  <a class="back-link" href="<?= $previewMode ? '/admin/posts' : '/blog' ?>">← <?= $previewMode ? '글 관리' : '블로그 목록' ?></a>
  <header><span class="badge"><?= e($post['category']) ?></span><time datetime="<?= e(date('Y-m-d', strtotime($displayDate))) ?>"><?= e(date('Y.m.d', strtotime($displayDate))) ?></time><h1><?= e($post['title']) ?></h1><p><?= e($post['author']) ?> 기술팀</p></header>
  <?php if (count($galleryImages) > 1): ?>
    <section class="article-gallery" data-article-slider aria-label="대표 이미지 슬라이드">
      <div class="article-gallery-slides"><?php foreach ($galleryImages as $index => $image): ?><figure class="article-gallery-slide<?= $index === 0 ? ' is-active' : '' ?>" aria-hidden="<?= $index === 0 ? 'false' : 'true' ?>"><img src="<?= e($image['file_path']) ?>" alt="<?= e($image['alt_text']) ?>"></figure><?php endforeach; ?></div>
      <div class="article-gallery-controls"><button type="button" data-article-prev aria-label="이전 이미지">‹</button><div class="article-gallery-dots"><?php foreach ($galleryImages as $index => $_): ?><button type="button" data-article-dot="<?= e($index) ?>" class="<?= $index === 0 ? 'is-active' : '' ?>"<?= $index === 0 ? ' aria-current="true"' : '' ?> aria-label="<?= e($index + 1) ?>번째 이미지"></button><?php endforeach; ?></div><button type="button" data-article-next aria-label="다음 이미지">›</button><button class="article-gallery-pause" type="button" data-article-pause aria-pressed="false">자동재생 일시정지</button></div>
    </section>
  <?php elseif ($galleryImages): ?><figure class="article-image"><img src="<?= e($galleryImages[0]['file_path']) ?>" alt="<?= e($galleryImages[0]['alt_text']) ?>"></figure><?php endif; ?>
  <div class="article-body">
    <?php foreach ($paragraphs as $paragraph): ?>
      <?php if (preg_match('/^\[\[image:(\d+)\]\]$/', trim($paragraph), $tokenMatch) === 1 && isset($inlineImages[(int) $tokenMatch[1]])): $image = $inlineImages[(int) $tokenMatch[1]]; ?>
        <figure class="article-inline-image"><img src="<?= e($image['file_path']) ?>" alt="<?= e($image['alt_text']) ?>"></figure>
      <?php else: ?><p><?= nl2br(e($paragraph)) ?></p><?php endif; ?>
    <?php endforeach; ?>
  </div>
  <aside class="article-cta"><div><strong>비슷한 시공이 필요하신가요?</strong><p>현장 조건에 맞는 구성을 상담해 드립니다.</p></div><a class="button button-primary" href="/contact">상담 신청</a></aside>
</article>
