<?php

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

$dark_fitness_content_ids  = array();
$dark_fitness_content_type = get_theme_mod( 'dark_fitness_service_content_type', 'post' );

for ( $i = 1; $i <= 3; $i++ ) {
    $dark_fitness_content_ids[] = get_theme_mod( 'dark_fitness_services_content_' . $dark_fitness_content_type . '_' . $i );
}

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

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

$dark_fitness_services_args = apply_filters('dark_fitness_services_section_args', $dark_fitness_services_args);

dark_fitness_render_service_section($dark_fitness_services_args);


/**
 * Render Service Section.
 */
function dark_fitness_render_service_section( $dark_fitness_services_args ) {
	$dark_fitness_service_button_label = get_theme_mod( 'dark_fitness_service_button_label');
	$default_service_icon = get_template_directory_uri() . '/assets/img/default.png'; 
	?>

	<section id="dark_fitness_service_section" class="asterthemes-frontpage-section service-section service-style-1">
		<?php
		if ( is_customize_preview() ) :
			dark_fitness_section_link( 'dark_fitness_service_section' );
		endif;
		?>
		<div class="asterthemes-wrapper">
			<?php 
			$dark_fitness_query = new WP_Query( $dark_fitness_services_args );
			if ( $dark_fitness_query->have_posts() ) :
				?>
				<div class="section-body">
					<div class="service-section-wrapper">
						<?php
						$i = 1;
						while ( $dark_fitness_query->have_posts() ) :
							$dark_fitness_query->the_post();
							$dark_fitness_service_icon = get_theme_mod( 'dark_fitness_service_icon_' . $i, '' );
							?>
							<div class="service-single">
								<div class="service-img">
									<img src="<?php echo esc_url( ! empty( $dark_fitness_service_icon ) ? $dark_fitness_service_icon : $default_service_icon ); ?>" alt="<?php the_title_attribute(); ?>">
								</div>
								<div class="service-detail">
									<div class="gym-icon">
										<i class="<?php echo esc_attr(get_theme_mod('dark_fitness_service_icon', 'fa fa-dumbbell')); ?>"></i>
									</div>
									<h3 class="service-title">
										<a href="<?php the_permalink(); ?>">
											<?php the_title(); ?>
										</a>
									</h3>
									<p>
										<?php echo wp_kses_post( wp_trim_words( get_the_content(), 20 ) ); ?>
									</p>
									<?php if ( ! empty( $dark_fitness_service_button_label ) ) : ?>
										<div class="service-button">
											<a href="<?php the_permalink(); ?>" class="asterthemes-button asterthemes-button-noborder-noalternate"><?php echo esc_html( $dark_fitness_service_button_label ); ?></a>
										</div>
									<?php endif; ?>
								</div>
							</div>
							<?php
							$i++;
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</section>
	<?php
}