<?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;

$_show_date     = !empty($data['show_date']);
$_show_category = !empty($data['show_category']);
?>
<section id="<?= e($sectionId) ?>" class="<?= build_section_classes('blog-preview', $data) ?>">
    <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>
