<?php

if ( ! get_theme_mod( 'aster_vlogger_enable_service_section', false ) ) {
	return;
}

$aster_vlogger_content_ids  = array();
$aster_vlogger_content_type = get_theme_mod( 'aster_vlogger_service_content_type', 'post' );

for ( $aster_vlogger_i = 1; $aster_vlogger_i <= 3; $aster_vlogger_i++ ) {
	$aster_vlogger_content_ids[] = get_theme_mod( 'aster_vlogger_service_content_' . $aster_vlogger_content_type . '_' . $aster_vlogger_i );
}

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

// Modify query to fetch posts from a specific category
$aster_vlogger_args = array(
    'post_type'           => $aster_vlogger_content_type,
    'post__in'            => array_filter( $aster_vlogger_content_ids ),
    'orderby'             => 'post__in',
    'posts_per_page'      => absint(4),
    'ignore_sticky_posts' => true,
);

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

$aster_vlogger_args = apply_filters( 'aster_vlogger_service_section_args', $aster_vlogger_args );

aster_vlogger_render_service_section( $aster_vlogger_args );

/**
 * Render Service Section.
 */
function aster_vlogger_render_service_section( $aster_vlogger_args ) { ?>

	<section id="aster_vlogger_service_section" class="asterthemes-frontpage-section service-section service-style-1">
		<?php
		if ( is_customize_preview() ) :
			aster_vlogger_section_link( 'aster_vlogger_service_section' );
		endif;
		?>
		<div class="asterthemes-wrapper">
			<div class="video-main-box">
				<div class="video-title">
					<?php $aster_vlogger_video_title = get_theme_mod( 'aster_vlogger_video_section_title');
					$aster_vlogger_video_text = get_theme_mod( 'aster_vlogger_video_section_text'); ?>

					<?php if ( ! empty( $aster_vlogger_video_title ) ) { ?>
						<h2><?php echo esc_html( $aster_vlogger_video_title ); ?></h2>
					<?php } ?>
					<?php if ( ! empty( $aster_vlogger_video_text ) ) { ?>
						<p><?php echo esc_html( $aster_vlogger_video_text ); ?></p>
					<?php } ?>
				</div>
				<?php 
				$aster_vlogger_query = new WP_Query( $aster_vlogger_args );
				if ( $aster_vlogger_query->have_posts() ) :
					?>
					<div class="section-body">
						<div class="service-section-wrapper">
							<?php
							$aster_vlogger_i = 1;
							while ( $aster_vlogger_query->have_posts() ) :
								$aster_vlogger_query->the_post();
								?>
								<div class="service-single">
									<div class="service-img">
										<?php
                                    if ( has_post_thumbnail() ) {
                                        the_post_thumbnail( 'full' );
                                    } else {
                                        // Use default image if no featured image is set
                                        echo '<img src="' . get_template_directory_uri() . '/resource/image/video-image.png" alt="Default Image">';
                                    }
                                    ?>
										<div class="video-icon">
											<i class="fas fa-play"></i>
										</div>
									</div>
									<div class="service-title">
										<span class="entry-comments"><i class="fa fa-comments" aria-hidden="true"></i> <?php comments_number( __('0 Comment', 'aster-vlogger'), __('0 Comments', 'aster-vlogger'), __('% Comments', 'aster-vlogger') ); ?></span>
										<h3>
											<a href="<?php the_permalink(); ?>">
												<?php the_title(); ?>
											</a>
										</h3>
									</div>
								</div>
								<?php
								$aster_vlogger_i++;
							endwhile;
							wp_reset_postdata();
							?>
						</div>
					</div>
				<?php endif; ?>
			</div>
		</div>
	</section>
	<?php
}