<?php
/**
 * ITSWEBER CMS – Default Theme: Events 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($upcoming_events)) return;

$_show_image = !isset($data['show_image']) || !empty($data['show_image']);
?>
<section id="<?= e($sectionId) ?>" class="<?= build_section_classes('events-preview', $data) ?>">
    <div class="container">
        <h2 class="reveal"><?= e($data['title'] ?? 'Upcoming Events') ?></h2>
        <?php if (!empty($data['subtitle'])): ?>
        <p class="section-subtitle reveal"><?= e($data['subtitle']) ?></p>
        <?php endif; ?>
        <div class="events-preview-grid">
            <?php foreach ($upcoming_events as $event): ?>
            <article class="event-card reveal <?= ($event['event_status'] ?? 'scheduled') !== 'scheduled' ? 'event-card--' . e($event['event_status'] ?? '') : '' ?>">
                <?php if ($_show_image): ?>
                    <?php if (!empty($event['media_path'])): ?>
                    <?= picture_tag($event['media_path'], $event['media_alt'] ?? $event['title'], 'event-card-image', true) ?>
                    <?php else: ?>
                    <div class="event-card-placeholder"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M16 3v4M8 3v4M3 9h18"/><path d="M10 14l2 2 4-4"/></svg></div>
                    <?php endif; ?>
                <?php endif; ?>
                <div class="event-card-body">
                    <?php if (($event['event_status'] ?? 'scheduled') !== 'scheduled'): ?>
                    <span class="event-status-badge event-status-badge--<?= e($event['event_status'] ?? '') ?>">
                        <?= ($event['event_status'] ?? '') === 'postponed' ? 'Postponed' : 'Cancelled' ?>
                    </span>
                    <?php endif; ?>
                    <h3><a href="/events/<?= e($event['slug']) ?>"><?= e($event['title']) ?></a></h3>
                    <div class="event-card-meta">
                        <span class="event-date"><?= e(date('d.m.Y', strtotime($event['start_date']))) ?></span>
                        <?php if (!empty($event['start_time'])): ?>
                        <span class="event-time"><?= e(substr($event['start_time'], 0, 5)) ?></span>
                        <?php endif; ?>
                        <?php if (!empty($event['location'])): ?>
                        <span class="event-location"><?= e($event['location']) ?></span>
                        <?php endif; ?>
                    </div>
                    <a href="/events/<?= e($event['slug']) ?>" class="event-card-link">View details &rarr;</a>
                </div>
            </article>
            <?php endforeach; ?>
        </div>
        <?php if (!empty($data['cta_text'])): ?>
        <p class="events-preview-cta">
            <a href="<?= e($data['cta_url'] ?? '/events') ?>" class="btn btn-secondary">
                <?= e($data['cta_text']) ?>
            </a>
        </p>
        <?php endif; ?>
    </div>
</section>
