<?php
if ( ! get_theme_mod( 'dark_fitness_enable_banner_section', false ) ) {
    return;
}

$dark_fitness_slider_content_ids  = array();
$dark_fitness_slider_content_type = get_theme_mod( 'dark_fitness_banner_slider_content_type', 'post' );

for ( $dark_fitness_i = 1; $dark_fitness_i <= 3; $dark_fitness_i++ ) {
    $dark_fitness_slider_content_ids[] = get_theme_mod( 'dark_fitness_banner_slider_content_' . $dark_fitness_slider_content_type . '_' . $dark_fitness_i );
}

// Get the category for the banner slider from theme mods or a default category
$dark_fitness_banner_slider_category = get_theme_mod('dark_fitness_banner_slider_category', 'slider');

// Modify query to fetch posts from a specific category
$dark_fitness_banner_slider_args = array(
    'post_type'           => $dark_fitness_slider_content_type,
    'post__in'            => array_filter( $dark_fitness_slider_content_ids ),
    'orderby'             => 'post__in',
    'posts_per_page'      => absint(3),
    'ignore_sticky_posts' => true,
);

// Apply category filter only if content type is 'post'
if ( 'post' === $dark_fitness_slider_content_type && ! empty( $dark_fitness_banner_slider_category ) ) {
    $dark_fitness_banner_slider_args['category_name'] = $dark_fitness_banner_slider_category;
}

$dark_fitness_banner_slider_args = apply_filters('dark_fitness_banner_section_args', $dark_fitness_banner_slider_args);

dark_fitness_render_banner_section($dark_fitness_banner_slider_args);


/**
 * Render Banner Section.
 */
function dark_fitness_render_banner_section($dark_fitness_banner_slider_args) { ?>
    <section id="dark_fitness_banner_section" class="banner-section banner-style-1">
        <?php if (is_customize_preview()) : dark_fitness_section_link('dark_fitness_banner_section'); endif; ?>
        <div class="banner-section-wrapper">
            <?php
            $dark_fitness_query = new WP_Query($dark_fitness_banner_slider_args);
            if ($dark_fitness_query->have_posts()) : ?>
                <div class="asterthemes-banner-wrapper banner-slider dark-fitness-carousel-navigation" data-slick='{"autoplay": false }'>
                    <?php
                    $dark_fitness_i = 1;
                    while ($dark_fitness_query->have_posts()) :
                        $dark_fitness_query->the_post();
                        $dark_fitness_subtitle = get_theme_mod('dark_fitness_banner_subtitle_' . $dark_fitness_i, '');
                        $dark_fitness_button_label = get_theme_mod('dark_fitness_banner_button_label_' . $dark_fitness_i, '');
                        $dark_fitness_button_link = get_theme_mod('dark_fitness_banner_button_link_' . $dark_fitness_i, '');
                        $dark_fitness_button_link = !empty($dark_fitness_button_link) ? $dark_fitness_button_link : get_the_permalink();
                        ?>
                        <div class="banner-single-outer">
                            <div class="banner-single">
                                <div class="banner-img">
                                    <?php the_post_thumbnail('full'); ?>
                                </div>
                                <div class="banner-caption">
                                    <div class="asterthemes-wrapper">
                                        <div class="banner-catption-wrapper wow bounceInUp delay-1000" data-wow-duration="2s">
                                            <?php if (!empty($dark_fitness_subtitle)) {
                                                $dark_fitness_words = explode(' ', esc_html($dark_fitness_subtitle));
                                                $dark_fitness_first_word = array_shift($dark_fitness_words);
                                                $dark_fitness_rest_of_subtitle = implode(' ', $dark_fitness_words); ?>
                                                <p class="caption-subtitle"><span class="highlight"><?php echo $dark_fitness_first_word; ?></span> <?php echo $dark_fitness_rest_of_subtitle; ?></p>
                                            <?php } ?>
                                            <h1 class="banner-caption-title">
                                                <?php
                                                $dark_fitness_title = '<a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
                                                $dark_fitness_title_words = explode(' ', $dark_fitness_title);
                                                $dark_fitness_last_two_words = array_splice($dark_fitness_title_words, -2);
                                                $dark_fitness_first_part = implode(' ', $dark_fitness_title_words);
                                                $dark_fitness_highlighted_part = implode(' ', $dark_fitness_last_two_words);
                                                echo $dark_fitness_first_part; ?> <span style="color: var(--primary-color);"><?php echo $dark_fitness_highlighted_part; ?></span>
                                            </h1>
                                            <div class="caption-description">
                                                <p><?php echo wp_kses_post(wp_trim_words(get_the_content(), 18)); ?></p>
                                            </div>
                                            <?php if (!empty($dark_fitness_button_label)) { ?>
                                                <div class="banner-slider-btn">
                                                    <a href="<?php echo esc_url($dark_fitness_button_link); ?>" class="asterthemes-button"><?php echo esc_html($dark_fitness_button_label); ?></a>
                                                </div>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php
                    $dark_fitness_i++;
                    endwhile;
                    wp_reset_postdata();
                    ?>
                </div>
            <?php endif; ?>
        </div>
    </section>
<?php }