<?php 
/* 
* Block Name: Testimonials
* Post Type: page, post
*/
?>
<?php if (isset($block['data']['previewImage'])): ?>
    <?php do_action('block_image', __FILE__); ?>
<?php else: ?>
    <?php

    // Block helper
    require \get_template_directory() . '/template-parts/base/block-helper.php';
    if (isset($disable) && $disable) {
        return;
    }

    // Load values and assing defaults
    $imgId = get_field('image') ? get_field('image') : false;
    $right = get_field('image_position') ?: false;
    $testimonials = get_field('testimonials') ?: false;
    $interval = intval(get_field('auto_swipe')) ?: 0;
?>
    <section class="section testimonials-section block <?php echo $className; ?>" 
        <?php if ($bgUrl): ?>
            style="background-image: url('<?php echo $bgUrl; ?>');"
            data-desktop-image="<?php echo $bgUrl; ?>"
        <?php endif; ?>
        <?php if ($mobileBgUrl): ?>
            data-mobile-image="<?php echo $mobileBgUrl; ?>"
        <?php endif; ?>
        <?php if ($interval): ?>
            data-interval="<?php echo (int) $interval; ?>"
        <?php endif; ?>
        <?php if ($sectionId): ?>
            id="<?php echo $sectionId; ?>"
        <?php endif; ?>
    >
        <div class="container">
            <div class="col-lg-8 offset-lg-2 text-content">
                <?php if ($title): ?>
                    <h2 class="h2"><?php echo $title; ?></h2>
                <?php endif; ?>
                <?php if ($text): ?>
                    <div class="text"><?php echo $text; ?></div>
                <?php endif; ?>
            </div>
            <div class="row mt-5">
                <?php if ($imgId): ?>
                    <div class="col-md-5 d-none d-md-flex justify-content-center align-items-center">
                        <?php echo wp_get_attachment_image($imgId, 'services', false, ['class' => 'testimonials-image']); ?>
                    </div>
                    <div class="col-md-7 order-last <?php echo $right ? 'order-md-first' : ''; ?> text-content">
                <?php else: ?>
                    <div class="col-lg-8 offset-lg-2 text-content">
                <?php endif; ?>
                <div class="testimonials-slider">
                    <?php if (is_array($testimonials) && $all = count($testimonials)): ?>
                        <div class="testimonials row">
                            <?php foreach ($testimonials as $key => $testimonial): ?>
                                <div class="col-12 testimonial d-flex align-items-center <?php echo !$key ? 'active' : ''; ?> t<?php echo $key; ?>" data-no="<?php echo $key; ?>">
                                    <div>
                                        <?php if (array_key_exists('text', $testimonial) && $text = $testimonial['text']): ?>
                                            <div class="quote-text">
                                                <i class="fas icon fa-quote-right"></i>
                                                <span class="text"><?php echo $text; ?></span>
                                            </div>
                                        <?php endif; ?>

                                        <div class="quote-author d-flex align-items-center">
                                            <?php if (array_key_exists('image', $testimonial) && $imageId = $testimonial['image']): ?>
                                                <div class="author-picture">
                                                    <?php echo wp_get_attachment_image($imageId, 'thumbnail', false, ['class' => 'author-image']); ?>
                                                </div>
                                            <?php endif; ?>
                                            <div class="author-info">
                                                <?php if (array_key_exists('rating', $testimonial) && $rating = $testimonial['rating']): ?>
                                                    <div class="author-rating">
                                                        <?php for ($i=0; $i<5; $i++): ?>
                                                            <i class="fas fa-star icon <?php echo $rating > $i ? 'active' : ''; ?>"></i>
                                                        <?php endfor; ?>
                                                    </div>
                                                <?php endif; ?>
                                                <?php if (array_key_exists('name', $testimonial) && $name = $testimonial['name']): ?>
                                                    <h3 class="author-name"><?php echo $name; ?></h3>
                                                <?php endif; ?>
                                                <div class="author-data">
                                                    <?php if (array_key_exists('title', $testimonial) && $position = $testimonial['title']): ?>
                                                        <small><?php echo $position; ?></small>
                                                    <?php endif; ?>
                                                    <?php if (array_key_exists('company', $testimonial) && !empty($testimonial['company'])): ?>
                                                        <?php if ($position): ?>
                                                            <span class="separator"> | </span>
                                                        <?php endif; ?>
                                                        <a href="<?php echo $testimonial['company']['url']; ?>" target="<?php echo $testimonial['company']['target']; ?>" class="author-company">
                                                            <small><?php echo $testimonial['company']['title']; ?></small>
                                                        </a>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <div class="testimonials-controller d-flex justify-content-between align-items-center">
                            <div class="numbers light"><?php echo count($testimonials);?> <small>/</small> <span class="change">1</span></div>
                            <div class="controllers text-end">
                                <span class="controller ba-prev d-inline-flex justify-content-center align-items-center" role="button" data-action="prew">
                                    <i class="fas fa-angle-left ba-prev" aria-label="Previous"></i>
                                </span>
                                <span class="controller ba-next d-inline-flex justify-content-center align-items-center" role="button" data-action="next">
                                    <i class="fas fa-angle-right ba-next" aria-label="Next"></i>
                                </span>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>
<?php endif; ?>

