<?php

/**
 * Testimoinals Block Template.
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during backend preview render.
 * @param   int $post_id The post ID the block is rendering content against.
 *          This is either the post ID currently being displayed inside a query loop,
 *          or the post ID of the post hosting this block.
 * @param   array $context The context provided to the block by the post or it's parent block.
 */

// Support custom "anchor" values.
$anchor = '';
if (! empty($block['anchor'])) {
  $anchor = 'id="' . esc_attr($block['anchor']) . '" ';
}

// Load values and assign defaults.
$class_name = "olympus-block-testimonial";

$theme = get_field('theme');
if ($theme) {
  $class_name .= ' ' . $theme;
}

// Load values and assign defaults.
$wrapper_attributes = get_block_wrapper_attributes(
  [
    'class' => $class_name
  ]
);

$show_all = get_field('show_all');
$testimonials_to_show = get_field('testimonials_to_show') ?? array();

$args = array(
  'post_type' => 'olympus-testimonials',
  'posts_per_page' => -1,
);

if (!$show_all) {
  $args['post__in'] = $testimonials_to_show;
}

$primary_color = (get_field('primary_color')) ? get_field('primary_color') : "var(--wp--preset--color--primary)";
$primary_contrast_color = (get_field('primary_contrast_color')) ? get_field('primary_contrast_color') : "var(--wp--preset--color--base)";
$secondary_color = (get_field('secondary_color')) ? get_field('secondary_color') : "var(--wp--preset--color--neutral)";
$secondary_contrast_color = (get_field('secondary_contrast_color')) ? get_field('secondary_contrast_color') : "var(--wp--preset--color--primary)";

$query = new WP_Query($args);
$total = $query->found_posts;

$perpage = 2;
if ($theme == "triumph") $perpage = 3;
if ($total <= 1 || $theme == "ace") $perpage = 1;
?>
<div <?php echo $anchor ?><?php echo $wrapper_attributes; ?>>
  <section class="splide" aria-label="Testimonials from users" data-perpage="<?php echo $perpage ?>">
    <div class="splide__track">
      <ul class="splide__list">
        <?php
        if ($query->have_posts()):
          while ($query->have_posts()) : $query->the_post();
        ?>
            <li class="splide__slide">
              <div class="olympus-block-testimonial__container" style="background:<?php echo ($theme == 'triumph') ? 'transparent' : $primary_color ?>;color:<?php echo ($theme == 'triumph') ? $primary_color : $primary_contrast_color ?>;">
                <div class="olympus-block-testimonial__svg__absolute">
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
                    <path d="M192 296c0 66.3-53.7 120-120 120l-8 0-32 0 0-64 32 0 8 0c30.9 0 56-25.1 56-56l0-8L0 288 0 96l192 0 0 96 0 96 0 8zm256 0c0 66.3-53.7 120-120 120l-8 0-32 0 0-64 32 0 8 0c30.9 0 56-25.1 56-56l0-8-128 0 0-192 192 0 0 96 0 96 0 8z" />
                  </svg>
                </div>
                <div class="olympus-block-testimonial__text"> <?php the_content() ?></div>
                <div class="olympus-block-testimonial__item">
                  <div class="olympus-block-testimonial__client__author">
                    <span class="olympus-block-testimonial__client__name"><?php echo get_field('testimonial_name', get_the_ID()); ?></span>
                    <?php if (get_field('testimonial_name', get_the_ID()) && get_field('job_position', get_the_ID())): ?>
                      ,&nbsp;
                    <?php endif; ?>
                    <span class="olympus-block-testimonial__client__position"><?php echo get_field('job_position', get_the_ID()) ?></span>
                  </div>
                </div>
              </div>
            </li>
        <?php
          endwhile;
        endif;
        wp_reset_postdata();
        ?>
      </ul>
    </div>
    <div class="olympus-block-testimonial__button__slider__custom">
      <button class="olympus-block-testimonial__button__slider left" style="background:<?php echo $secondary_color; ?>;color:<?php echo $secondary_contrast_color; ?>">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
          <path d="M30.1 256l17-17L207 79l17-17L257.9 96l-17 17L97.9 256 241 399l17 17L224 449.9l-17-17L47 273l-17-17z" />
        </svg>
      </button>
      <button class="olympus-block-testimonial__button__slider right" style="background:<?php echo $secondary_color; ?>;color:<?php echo $secondary_contrast_color; ?>">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
          <path d="M289.9 256l-17 17L113 433l-17 17L62.1 416l17-17 143-143L79 113l-17-17L96 62.1l17 17L273 239l17 17z" />
        </svg>
      </button>
    </div>
  </section>

</div>