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

$social_blog_content_ids  = array();
$social_blog_content_type = get_theme_mod( 'social_blog_service_content_type', 'post' );

for ( $social_blog_i = 1; $social_blog_i <= 5; $social_blog_i++ ) {
	$social_blog_content_ids[] = get_theme_mod( 'social_blog_service_content_' . $social_blog_content_type . '_' . $social_blog_i );
}

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

// Modify query to fetch posts from a specific category
$social_blog_args = array(
    'post_type'           => $social_blog_content_type,
    'post__in'            => array_filter( $social_blog_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' === $social_blog_content_type && ! empty( $social_blog_services_category ) ) {
    $social_blog_args['category_name'] = $social_blog_services_category;
}

$social_blog_args = apply_filters( 'social_blog_service_section_args', $social_blog_args );

social_blog_render_service_section( $social_blog_args );

/**
 * Render Service Section.
 */
function social_blog_render_service_section( $social_blog_args ) { ?>

	<section id="social_blog_service_section" class="asterthemes-frontpage-section service-section service-style-1">
		<?php
		if ( is_customize_preview() ) :
			social_blog_section_link( 'social_blog_service_section' );
		endif;
		?>
		<div class="asterthemes-wrapper">
			<div class="video-title">
				<?php $social_blog_video_title = get_theme_mod( 'social_blog_video_section_title');
				$social_blog_video_text = get_theme_mod( 'social_blog_video_section_text'); ?>

				<?php if ( ! empty( $social_blog_video_title ) ) { ?>
					<h2><?php echo esc_html( $social_blog_video_title ); ?></h2>
				<?php } ?>
				<?php if ( ! empty( $social_blog_video_text ) ) { ?>
					<p><?php echo esc_html( $social_blog_video_text ); ?></p>
				<?php } ?>
			</div>
			<div class="video-main-box">				
				<?php 
				$social_blog_query = new WP_Query( $social_blog_args );
				if ( $social_blog_query->have_posts() ) :
					?>
					<div class="section-body">
						<div class="service-section-wrapper">
							<?php
							$social_blog_i = 1;
							while ( $social_blog_query->have_posts() ) :
								$social_blog_query->the_post();
								?>
								<div class="service-single">
									<div class="service-img">
										<?php 
										if ( has_post_thumbnail() ) {
											the_post_thumbnail( 'full' );
										}
										?>
										<?php if ( get_theme_mod( 'social_blog_enable_service_video_section', true ) === true ) { ?>
										<div class="video-icon">
											<i class="fas fa-play"></i>
										</div>
										<?php } ?>
									</div>
									<div class="service-title">
										<span class="entry-comments"><i class="fa fa-comments" aria-hidden="true"></i> <?php comments_number( __('0 Comment', 'social-blog'), __('0 Comments', 'social-blog'), __('% Comments', 'social-blog') ); ?></span>
										<span class="mag-post-meta">
											<?php social_blog_posted_on(); ?>
										</span>
										<h3>
											<a href="<?php the_permalink(); ?>">
												<?php the_title(); ?>
											</a>
										</h3>
									</div>
								</div>
								<?php
								$social_blog_i++;
							endwhile;
							wp_reset_postdata();
							?>
						</div>
					</div>
				<?php endif; ?>
			</div>
		</div>
	</section>

	<?php
}