<?php
/**
 * ITSWEBER CMS – Default Theme: Blog Preview Section Template
 * Page Builder section template (3.2.0)
 *
 * Variables: $data (array), $section (DB row), $sectionId (string)
 *
 * @author  Benny Weber
 * @copyright ITSWEBER, www.itsweber.de
 */

if (empty($latest_posts)) return;

$_s_classes = ['blog-preview-section'];
if (!empty($data['color_scheme'])) $_s_classes[] = 'section--scheme-' . $data['color_scheme'];
if (!empty($data['section_text_color'])) $_s_classes[] = 'section--text-' . $data['section_text_color'];
if (!empty($data['text_align']))   $_s_classes[] = 'section--align-' . $data['text_align'];
$_show_date     = !empty($data['show_date']);
$_show_category = !empty($data['show_category']);
?>
<section id="<?= e($sectionId) ?>" class="<?= e(implode(' ', $_s_classes)) ?>">
    <div class="container">
        <h2 class="reveal"><?= e($data['title'] ?? 'Latest News') ?></h2>
        <?php if (!empty($data['subtitle'])): ?>
        <p class="section-subtitle reveal"><?= e($data['subtitle']) ?></p>
        <?php endif; ?>
        <div class="blog-grid">
            <?php foreach ($latest_posts as $post): ?>
            <article class="blog-card reveal">
                <?php if (!empty($post['featured_image'])): ?>
                <?= picture_tag($post['featured_image'], $post['title'], 'blog-card-image', true) ?>
                <?php else: ?>
                <div class="blog-card-placeholder"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg></div>
                <?php endif; ?>
                <div class="blog-card-body">
                    <?php if ($_show_category && !empty($post['category_name'])): ?>
                    <span class="blog-category"><?= e($post['category_name']) ?></span>
                    <?php endif; ?>
                    <h3><a href="/blog/<?= e($post['slug']) ?>"><?= e($post['title']) ?></a></h3>
                    <p><?= e(str_truncate($post['excerpt'] ?? '', 120)) ?></p>
                    <?php if ($_show_date && !empty($post['published_at'])): ?>
                    <div class="blog-meta">
                        <time datetime="<?= e(date('Y-m-d', strtotime($post['published_at']))) ?>"><?= e(date('d.m.Y', strtotime($post['published_at']))) ?></time>
                    </div>
                    <?php endif; ?>
                </div>
            </article>
            <?php endforeach; ?>
        </div>
        <?php if (!empty($data['cta_text'])): ?>
        <p class="blog-preview-cta">
            <a href="<?= e($data['cta_url'] ?? '/blog') ?>" class="btn btn-secondary">
                <?= e($data['cta_text']) ?>
            </a>
        </p>
        <?php endif; ?>
    </div>
</section>
