<?php
if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly

$query = $this->get_query_result();
if ( ! $query->found_posts ) {

    rivax_query_not_found_msg ();

    return;
}
?>
<div class="spot-slider <?php echo esc_attr($settings["thumbs_direction"]); if($settings["thumbs_vertical_inside"]) echo ' inside'; ?>">
    <div class="rivax-posts-wrapper">
        <?php $this->render_slider_header(); ?>
        <?php
        if ( $query->have_posts() ) :
            while ( $query->have_posts() ) :
                $query->the_post();

                $this->render_post_body($query->current_post);

            endwhile;
        endif;
        wp_reset_postdata();
        ?>
        <?php $this->render_carousel_footer('slider'); ?>

        <?php
        $id = 'rivax-thumbs-' . $this->get_id();

        $this->add_render_attribute( 'thumbs', 'id', $id );
        $this->add_render_attribute( 'thumbs', 'class', 'thumbs-slider-wrapper' );

        $thumbs_settings = [
            "loop"                  => ( $settings["slider_loop"] == "yes" ),
            "speed"                 => $settings["slider_speed"]["size"],
            "grabCursor"            => ( $settings["slider_grab_cursor"] == "yes" ),
            "slideToClickedSlide"   => true,
            "direction"             => "horizontal",
            "touchRatio"            => 0.2,
            "spaceBetween"          => intval($settings["thumbs_spacing"]["size"]),
            "loopedSlides"          => 6,
            "slidesPerView"         => ($settings["thumb_layout"] == 'image')? 2 : 1.5,
            "breakpoints"           => [
                768 => [
                    "slidesPerView"   => min(3, $settings["thumbs_count"]),
                    "direction"       => "horizontal",
                ],
                1025 => [
                    "slidesPerView"   => intval($settings["thumbs_count"]),
                    "direction"       => $settings["thumbs_direction"],
                ],
            ],
        ];

        $this->add_render_attribute('thumbs', 'data-settings', wp_json_encode(array_filter($thumbs_settings)) );

        $bar_time = ($settings["slider_autoplay"] && $settings["slider_autoplay_speed"]) ? $settings["slider_autoplay_speed"] : 2000;
        $this->add_render_attribute('thumbs', 'style', "--bar-time: {$bar_time}ms;" );

        $thumb_size = ($settings["thumb_layout"] == 'image-left')? 'thumbnail' : 'rivax-small';
        ?>
        <div <?php $this->print_render_attribute_string( 'thumbs' ); ?>>
            <div class="thumbs-slider-inner">
                <div class="swiper thumbs-slider layout-<?php echo esc_attr($settings["thumb_layout"]); ?>">
                    <div class="swiper-wrapper">
                        <?php
                        if ( $query->have_posts() ) :
                            while ( $query->have_posts() ) :
                                $query->the_post();
                                ?>
                                <div class="thumb-wrapper swiper-slide">
                                    <?php if($settings["thumb_layout"] != 'content'): ?>
                                    <div class="image-wrapper">
                                        <?php the_post_thumbnail($thumb_size, array( 'title' => get_the_title() )); ?>
                                    </div>
                                    <?php endif; ?>
                                    <?php if($settings["thumb_layout"] != 'image'): ?>
                                        <div class="thumb-content">
                                            <h6 class="thumb-title"><?php the_title(); ?></h6>
                                            <?php if($settings["show_thumb_date"]): ?>
                                                <span class="thumb-date"><?php echo get_the_date(); ?></span>
                                            <?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php
                            endwhile;
                        endif;
                        wp_reset_postdata();
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
