<?php

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

$doctor_physician_content_ids  = array();
$doctor_physician_content_type = get_theme_mod( 'doctor_physician_service_content_type', 'post' );

for ( $doctor_physician_i = 1; $doctor_physician_i <= 4; $doctor_physician_i++ ) {
	$doctor_physician_content_ids[] = get_theme_mod( 'doctor_physician_service_content_' . $doctor_physician_content_type . '_' . $doctor_physician_i );
}

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

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

$doctor_physician_args = apply_filters( 'doctor_physician_service_section_args', $doctor_physician_args );

doctor_physician_render_service_section( $doctor_physician_args );

/**
 * Render Service Section.
 */
function doctor_physician_render_service_section( $doctor_physician_args ) {
	$doctor_physician_service_main_button_label = get_theme_mod( 'doctor_physician_service_main_button_label');
	$doctor_physician_service_main_button_url = get_theme_mod( 'doctor_physician_service_main_button_url');
	$doctor_physician_service_button_label = get_theme_mod( 'doctor_physician_service_button_label');

	$doctor_physician_about_heading = get_theme_mod( 'doctor_physician_about_heading');
	?>

	<section id="doctor_physician_trending_section" class="asterthemes-frontpage-section service-section service-style-1">
		<?php
		if ( is_customize_preview() ) :
			doctor_physician_section_link( 'doctor_physician_service_section' );
		endif;
		?>
		<div class="asterthemes-wrapper">
			<?php if ( ! empty( $doctor_physician_about_heading ) ) { ?>
				<div class="header-contact-inner">
				  <h3 data-text="heading"><?php echo esc_html( $doctor_physician_about_heading ); ?></h3>
				</div>
			<?php } ?>
			<?php 
			$doctor_physician_query = new WP_Query( $doctor_physician_args );
			if ( $doctor_physician_query->have_posts() ) :
				?>
				<div class="section-body">
					<div class="about-us">
						<?php
						$doctor_physician_i = 1;
						while ( $doctor_physician_query->have_posts() ) :
							$doctor_physician_query->the_post();
							$doctor_physician_service_icon = get_theme_mod( 'doctor_physician_service_icon' .$doctor_physician_i);
							?>
							<div class="service-single">
								<div class="service-detail">
									<div class="gym-icon">
										<i class="<?php echo esc_attr( $doctor_physician_service_icon ); ?>"></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( $doctor_physician_service_button_label ) ) : ?>
										<div class="service-button">
											<a href="<?php the_permalink(); ?>" class="asterthemes-button asterthemes-button-noborder-noalternate"><?php echo esc_html( $doctor_physician_service_button_label ); ?></a>
										</div>
									<?php endif; ?>
								</div>
							</div>
							<?php
							$doctor_physician_i++;
						endwhile;
						wp_reset_postdata();
						?>
					</div>
					<?php if ( ! empty( $doctor_physician_service_main_button_url ) || ! empty( $doctor_physician_service_main_button_label ) ) : ?>
						<div class="service-button-main">
							<a href="<?php echo esc_html( $doctor_physician_service_main_button_url ); ?>" class="button-section"><?php echo esc_html( $doctor_physician_service_main_button_label ); ?></a>
						</div>
					<?php endif; ?>
				</div>
			<?php endif; ?>
		</div>
	</section>
	<?php
}