<?php
    $html_id = pxl_get_element_id($settings);
    $tax = ['portfolio-category'];
    $select_post_by = $widget->get_setting('select_post_by', '');
    $post_type = $settings['post_type'] ?? 'post';
    $post_ids = ($select_post_by === 'post_selected') ? $widget->get_setting('source_'.$post_type.'_post_ids', '') : [];
    $source = ($select_post_by !== 'post_selected') ? $widget->get_setting('source_'.$post_type, '') : [];
    $orderby = $widget->get_setting('orderby', 'date');
    $order = $widget->get_setting('order', 'desc');
    $limit = $widget->get_setting('limit', 6);


    extract(pxl_get_posts_of_grid(
        $post_type, 
        [
            'source' => $source, 
            'orderby' => $orderby, 
            'order' => $order, 
            'limit' => $limit, 
            'post_ids' => $post_ids
        ],
        $tax
    ));

    $filter_default_title = $widget->get_setting('filter_default_title', 'All');
    // Grid
    $col_xs = $widget->get_setting('col_xs', '');
    $col_sm = $widget->get_setting('col_sm', '');
    $col_md = $widget->get_setting('col_md', '');
    $col_lg = $widget->get_setting('col_lg', '');
    $col_xl = $widget->get_setting('col_xl', '');
    $col_xxl = $widget->get_setting('col_xxl', '');
    $slides_to_scroll = $widget->get_setting('slides_to_scroll', '');

    $drap = $widget->get_setting('drap', false);

    // Animation
    $animate = $widget->get_setting('pxl_animate');
    $animate_delay = (int)$widget->get_setting('animate_delay', 0);

    // Display
    $show_category = !empty($settings['show_category']) || false;
    $num_words = !empty($settings['num_words']) ? $settings['num_words'] : 27;
    $img_size = !empty($settings['img_size']) ? $settings['img_size'] : '480x605';
    $num_categories = $settings['num_categories'] ?? '';
    $title_hover_style = $settings['title_hover_style']  ?? '';

    $pause_on_hover = !empty($settings['pause_on_hover']) || false;
    $autoplay = !empty($settings['autoplay']) || false;
    $autoplay_speed = !empty($settings['autoplay_speed']) ? $settings['autoplay_speed'] : 5000;
    $infinite = !empty($settings['infinite']) || false;
    $speed = !empty($settings['speed']) ? $settings['speed'] : 500;
    $arrows = !empty($settings['arrows']) || false;
    $pagination = !empty($settings['pagination']) || false;
    $pagination_type = $settings['pagination_carousel_type'];
    $slide_to_scroll = !empty($settings['slide_to_scroll']) ? $settings['slide_to_scroll'] : 2;


    $opts = [
        'slide_direction'               => 'horizontal',
        'slide_percolumn'               => 1, 
        'slide_mode'                    => 'slide', 
        'slides_to_show'                => 1,
        'slides_to_show_xxl'            => $col_xxl,
        'slides_to_show_xl'             => $col_xl,
        'slides_to_show_lg'             => $col_lg,
        'slides_to_show_md'             => $col_md,
        'slides_to_show_sm'             => $col_sm,
        'slides_to_show_xs'             => $col_xs,
        'slides_to_scroll'              => $slide_to_scroll,
        'arrow'                         => $arrows,
        'pagination'                    => $pagination,
        'pagination_type'               => $pagination_type,
        'autoplay'                      => $autoplay,
        'pause_on_hover'                => $pause_on_hover,
        'pause_on_interaction'          => true,
        'delay'                         => $autoplay_speed,
        'loop'                          => $infinite,
        'speed'                         => $speed
    ];
    $widget->add_render_attribute( 'carousel', [
        'class'         => 'pxl-swiper-container',
        'dir'           => is_rtl() ? 'rtl' : 'ltr',
        'data-settings' => wp_json_encode($opts)
    ]);
?>

<div class="pxl-post-carousel pxl-case-studies-carousel pxl-case-studies-carousel3 pxl-swiper-slider">
    <div class="pxl-swiper-filter">
        <div class="pxl-filter-inner">
            <span class="filter-item active" data-filter="*"><?php echo esc_html($filter_default_title); ?></span>
            <?php foreach ($categories as $category): ?>
                <?php 
                    $category_arr = explode('|', $category); 
                    $current_term = get_term_by('slug', $category_arr[0], $category_arr[1]); 
                    $_is = false;
                    foreach($posts as $post) {
                        $terms = get_the_terms($post->ID, $post_type . '-category');
                        if ($terms && !is_wp_error($terms)) { 
                            foreach($terms as $term) {
                                if ($current_term && $current_term->slug === $term->slug) {
                                    $_is = true; 
                                    break 2; 
                                }
                            }
                        }
                    }
                ?>
                <?php if($_is) : ?>
                    <span class="filter-item" data-filter="<?php echo esc_attr($current_term->slug); ?>">
                        <?php echo esc_html($current_term->name); ?>
                    </span>
                <?php endif; ?>
            <?php endforeach; ?>

        </div>
    </div>
    <div class="pxl-carousel-inner">
        <div <?php pxl_print_html($widget->get_render_attribute_string( 'carousel' )); ?>>
            <div class="pxl-swiper-wrapper">
                <?php foreach ($posts as $key => $post): ?>
                    <?php             
                        $img_id = get_post_thumbnail_id($post->ID) ?? '';
                        $featured = solarva_get_thumbnail($img_id, $img_size);
                        $filter_class = !empty($tax) ? pxl_get_term_of_post_to_class($post->ID, array_unique($tax)) : '';
                        $animate_delay += 100;
                    ?>
                    <div class="pxl-swiper-slide <?php echo esc_attr($settings['pxl_animate'].' '.$filter_class); ?>" data-wow-delay="<?php echo esc_attr($animate_delay.'ms'); ?>">
                        <div class="pxl-post--container ">
                            <div class="pxl-post--inner">
                                <div class="pxl-post--featured pxl-hover-zoom-in">
                                    <a class="pxl-featured--link" href="<?php echo esc_url(get_permalink($post->ID)); ?>">
                                        <?php echo wp_kses_post($featured); ?>
                                    </a>
                                </div>
                                <div class="pxl-post--content">
                                    <?php if (!empty($show_category) == 'true' && $show_category == 'true') : ?>
                                        <div class="pxl-post--category">
                                            <?php solarva_get_category_custom($post, $num_categories, $post_type); ?>
                                        </div>
                                    <?php endif; ?>
                                    <div class="pxl-post--title <?php echo esc_attr($title_hover_style); ?>">
                                        <a class="pxl-title--link" href="<?php echo esc_url(get_permalink($post->ID)); ?>">
                                            <?php echo esc_html(get_the_title($post->ID)); ?>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php solarva_swiper_controls($pagination, $arrows); ?>
    </div>
</div>

