<?php

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

$resort_motel_left_content_ids  = array();
$resort_motel_left_content_type = get_theme_mod( 'resort_motel_service_left_content_type', 'post' );

for ( $resort_motel_i = 1; $resort_motel_i <= 6; $resort_motel_i++ ) {
	$resort_motel_left_content_ids[] = get_theme_mod( 'resort_motel_service_left_content_' . $resort_motel_left_content_type . '_' . $resort_motel_i );
}

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

// Modify query to fetch posts from a specific category
$resort_motel_left_args = array(
    'post_type'           => $resort_motel_left_content_type,
    'post__in'            => array_filter( $resort_motel_left_content_ids ),
    'orderby'             => 'post__in',
    'posts_per_page'      => absint(6),
    'ignore_sticky_posts' => true,
);

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

$resort_motel_left_args = apply_filters( 'resort_motel_service_section_args', $resort_motel_left_args );

resort_motel_render_service_section( $resort_motel_left_args );

/**
 * Render Service Section.
 */
function resort_motel_render_service_section( $resort_motel_left_args ) { 

    ?>

	<section id="resort_motel_service_section" class="asterthemes-frontpage-section service-section service-style-1">
		<?php
		if ( is_customize_preview() ) :
			resort_motel_section_link( 'resort_motel_service_section' );
		endif;
		?>
		<div class="asterthemes-wrapper">
			<div class="video-main-box">
				<?php 
				$resort_motel_query = new WP_Query( $resort_motel_left_args );
				if ( $resort_motel_query->have_posts() ) :

					$resort_motel_main_services_heading = get_theme_mod( 'resort_motel_main_services_heading');

					$resort_motel_main_services_text = get_theme_mod( 'resort_motel_main_services_text');
					?>
					<div class="section-body">
						<?php if ( ! empty( $resort_motel_main_services_text ) ) { ?>
							<p><?php echo esc_html( $resort_motel_main_services_text ); ?></p>
						<?php } ?>
						<?php if ( ! empty( $resort_motel_main_services_heading ) ) { ?>
							<h3><?php echo esc_html( $resort_motel_main_services_heading ); ?></h3>
						<?php } ?>
						<div class="service-section-wrapper">
							<?php
							$resort_motel_i = 1;
							while ( $resort_motel_query->have_posts() ) :
								$resort_motel_query->the_post();
								?>
								<div class="service-single">
									<div class="service-image">
										<?php 
                                        if ( has_post_thumbnail() ) {
                                            the_post_thumbnail( 'full' ); 
                                        }
                                        ?>
									</div>
									<div class="service-title">
										<p>
											<a href="<?php the_permalink(); ?>">
												<?php the_title(); ?>
											</a>
										</p>
									</div>
								</div>
								<?php
								$resort_motel_i++;
							endwhile;
							wp_reset_postdata();
							?>
						</div>
					</div>
				<?php endif; ?>
				<div class="video-title">
					<?php
						$resort_motel_center_services_images = get_theme_mod( 'resort_motel_center_services_images');
				 		if ( ! empty( $resort_motel_center_services_images ) ) { ?>
							<img src="<?php echo esc_url( $resort_motel_center_services_images ); ?>" >
						<?php } else { ?>
						<?php } ?>
				</div>				
			</div>
		</div>
	</section>
	<?php
}
?>