<?php
if ( ! get_theme_mod( 'social_blog_enable_banner_section', true ) ) {
    return;
}

$social_blog_slider_content_type = get_theme_mod( 'social_blog_banner_slider_content_type', 'post' );
$social_blog_banner_slider_category = get_theme_mod('social_blog_banner_slider_category', 'slider');

$social_blog_slider_content_ids = [];
for ( $social_blog_i = 1; $social_blog_i <= 6; $social_blog_i++ ) {
    $content_id = $social_blog_slider_content_type === 'page'
        ? get_theme_mod( 'social_blog_banner_slider_content_page_' . $social_blog_i )
        : get_theme_mod( 'social_blog_banner_slider_content_post_' . $social_blog_i );

    if ( !empty($content_id) ) {
        $social_blog_slider_content_ids[] = $content_id;
    }
}

$social_blog_args = array(
    'post_type'           => $social_blog_slider_content_type,
    'posts_per_page'      => 6,
    'ignore_sticky_posts' => true,
);

// Check if a category is selected
if ( 'post' === $social_blog_slider_content_type && ! empty( $social_blog_banner_slider_category ) ) {
    // If there are specific posts selected, fetch those
    if ( ! empty( $social_blog_slider_content_ids ) ) {
        $social_blog_args['post__in'] = $social_blog_slider_content_ids;
        $social_blog_args['orderby'] = 'post__in';
    } else {
        // If no specific posts, fallback to latest posts from the selected category
        $social_blog_args['category_name'] = $social_blog_banner_slider_category;
    }
}

$social_blog_query = new WP_Query( $social_blog_args );
?>

<section id="social_blog_banner_section" class="banner-section banner-style-1">
    <div class="banner-section-wrapper">
        <?php
        if ( $social_blog_query->have_posts() ) :
            $social_blog_counter = 1; // Initialize counter for unique IDs
            ?>
            <div class="asterthemes-banner-wrapper banner-slider">
                <?php
                while ( $social_blog_query->have_posts() ) :
                    $social_blog_query->the_post();
                    $social_blog_image_url = has_post_thumbnail() ? get_the_post_thumbnail_url(get_the_ID(), 'full') : get_template_directory_uri() . '/resource/image/default-img.png';
                    ?>
                    <div class="banner-single" id="banner-single-<?php echo $social_blog_counter; ?>">
                        <div class="banner-img">
                            <img src="<?php echo esc_url( $social_blog_image_url ); ?>" alt="<?php the_title_attribute(); ?>" />
                            <div class="video-icon">
                                <a href="<?php the_permalink(); ?>"><i class="fas fa-play"></i></a>
                            </div>
                        </div>
                        <h3 class="banner-caption-title">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h3>
                    </div>
                    <?php
                    $social_blog_counter++; // Increment the counter for each post
                endwhile;
                wp_reset_postdata();
                ?>
            </div>
            <?php
        endif;
        ?>
    </div>
</section>