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

$aster_vlogger_slider_content_type = get_theme_mod( 'aster_vlogger_banner_slider_content_type', 'post' );
$aster_vlogger_banner_slider_category = get_theme_mod('aster_vlogger_banner_slider_category', 'slider');

$aster_vlogger_slider_content_ids = [];
for ( $aster_vlogger_i = 1; $aster_vlogger_i <= 6; $aster_vlogger_i++ ) {
    $aster_vlogger_content_id = $aster_vlogger_slider_content_type === 'page'
        ? get_theme_mod( 'aster_vlogger_banner_slider_content_page_' . $aster_vlogger_i )
        : get_theme_mod( 'aster_vlogger_banner_slider_content_post_' . $aster_vlogger_i );

    if ( !empty($aster_vlogger_content_id) ) {
        $aster_vlogger_slider_content_ids[] = $aster_vlogger_content_id;
    }
}

$aster_vlogger_args = array(
    'post_type'           => $aster_vlogger_slider_content_type,
    'posts_per_page'      => 6,
    'ignore_sticky_posts' => true,
);

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

$aster_vlogger_query = new WP_Query( $aster_vlogger_args );
?>

<section id="aster_vlogger_banner_section" class="banner-section banner-style-1">
    <div class="banner-section-wrapper">
        <?php
        if ( $aster_vlogger_query->have_posts() ) :
            $aster_vlogger_counter = 1; // Initialize counter for unique IDs
            ?>
            <div class="asterthemes-banner-wrapper banner-slider">
                <?php
                while ( $aster_vlogger_query->have_posts() ) :
                    $aster_vlogger_query->the_post();
                    $aster_vlogger_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 $aster_vlogger_counter; ?>">
                        <div class="banner-img">
                            <img src="<?php echo esc_url( $aster_vlogger_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
                    $aster_vlogger_counter++; // Increment the counter for each post
                endwhile;
                wp_reset_postdata();
                ?>
            </div>
            <?php
        endif;
        ?>
    </div>
</section>