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

$_s_classes = ['events-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_image = !isset($data['show_image']) || !empty($data['show_image']);
?>
<section id="<?= e($sectionId) ?>" class="<?= e(implode(' ', $_s_classes)) ?>">
    <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>
