<?php
/**
 * ITSWEBER CMS – Default Theme: Testimonials Section Template
 * Page Builder section template (3.4.0)
 *
 * Displays customer testimonials/quotes with author info.
 * Supports staggered layout and optional avatars.
 *
 * Variables: $data (array), $section (DB row), $sectionId (string)
 *
 * Data fields:
 *   title, subtitle, color_scheme, section_text_color, section_surface, columns (1-3)
 *   items[] => { quote, author, role, avatar, rating }
 *
 * @author  Benny Weber
 * @copyright ITSWEBER, www.itsweber.de
 */

$title    = $data['title'] ?? '';
$subtitle = $data['subtitle'] ?? '';
$columns  = $data['columns'] ?? '2';
$layout   = $data['layout'] ?? 'staggered';
$items    = (isset($data['items']) && is_array($data['items'])) ? $data['items'] : [];

if (empty($items)) return;

$extra = ['testimonials--cols-' . $columns];
if ($layout === 'staggered') $extra[] = 'testimonials--staggered';
?>
<section id="<?= e($sectionId) ?>" class="<?= build_section_classes('testimonials', $data, $extra) ?>">
    <div class="container">
        <?php if ($title !== '' || $subtitle !== ''): ?>
        <div class="testimonials-header reveal">
            <?php if ($title !== ''): ?>
            <h2 class="testimonials-title"><?= e($title) ?></h2>
            <?php endif; ?>
            <?php if ($subtitle !== ''): ?>
            <p class="testimonials-subtitle"><?= e($subtitle) ?></p>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div class="testimonials-grid">
            <?php foreach ($items as $i => $item): ?>
            <blockquote class="testimonial-card reveal">
                <div class="testimonial-quote-mark" aria-hidden="true">&ldquo;</div>
                <p class="testimonial-text"><?= e($item['quote'] ?? '') ?></p>

                <?php
                $rating = (int)($item['rating'] ?? 0);
                if ($rating > 0 && $rating <= 5):
                ?>
                <div class="testimonial-rating" aria-label="<?= $rating ?> von 5 Sternen">
                    <?php for ($s = 1; $s <= 5; $s++): ?>
                    <svg class="testimonial-star<?= $s <= $rating ? ' star--filled' : '' ?>" width="16" height="16" viewBox="0 0 24 24" fill="<?= $s <= $rating ? 'currentColor' : 'none' ?>" stroke="currentColor" stroke-width="2" aria-hidden="true">
                        <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>
                    </svg>
                    <?php endfor; ?>
                </div>
                <?php endif; ?>

                <footer class="testimonial-author">
                    <?php if (!empty($item['avatar'])): ?>
                    <div class="testimonial-avatar">
                        <?= picture_tag($item['avatar'], e($item['author'] ?? ''), 'testimonial-avatar-img') ?>
                    </div>
                    <?php elseif (!empty($item['author'])): ?>
                    <div class="testimonial-avatar testimonial-avatar--initials">
                        <?= e(mb_substr($item['author'], 0, 1)) ?>
                    </div>
                    <?php endif; ?>
                    <div class="testimonial-author-info">
                        <?php if (!empty($item['author'])): ?>
                        <cite class="testimonial-author-name"><?= e($item['author']) ?></cite>
                        <?php endif; ?>
                        <?php if (!empty($item['role'])): ?>
                        <span class="testimonial-author-role"><?= e($item['role']) ?></span>
                        <?php endif; ?>
                    </div>
                </footer>
            </blockquote>
            <?php endforeach; ?>
        </div>
    </div>
</section>
