<?php

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

$content_ids   = array();
$content_count = get_theme_mod( 'construktly_service_count', 6 );
$content_type  = get_theme_mod( 'construktly_service_content_type', 'page' );

if ( in_array( $content_type, array( 'post', 'page' ) ) ) {

	for ( $i = 1; $i <= $content_count; $i++ ) {
		$content_ids[] = get_theme_mod( 'construktly_service_content_' . $content_type . '_' . $i );
	}

	$args = array(
		'post_type'           => $content_type,
		'post__in'            => $content_ids,
		'orderby'             => 'post__in',
		'posts_per_page'      => absint( $content_count ),
		'ignore_sticky_posts' => true,
	);

}

$args = apply_filters( 'construktly_service_section_args', $args );

construktly_render_service_section( $args );

/**
 * Render Service Section.
 */
function construktly_render_service_section( $args ) {
	$section_title        = get_theme_mod( 'construktly_service_title', __( 'Our Services', 'construktly' ) );
	$section_text         = get_theme_mod( 'construktly_service_text' );
	$service_button_label = get_theme_mod( 'construktly_service_button_label', __( 'View All Services', 'construktly' ) );
	$service_button_link  = get_theme_mod( 'construktly_service_button_link' );
	$service_button_link  = ! empty( $service_button_link ) ? $service_button_link : '#';

	$query = new WP_Query( $args );
	?>
	<section id="construktly_service_section" class="construktly-frontpage-section construktly-our-services-section service-style-3">
		<?php
		if ( is_customize_preview() ) :
			construktly_section_link( 'construktly_service_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<div class="section-header-subtitle">
				<h3 class="section-title"><?php echo esc_html( $section_title ); ?></h3>
				<p class="section-subtitle"><?php echo esc_html( $section_text ); ?></p>
			</div>
			<?php if ( $query->have_posts() ) : ?>
				<div class="construktly-section-body">
					<div class="construktly-our-services-section-wrapper">
						<?php
						while ( $query->have_posts() ) :
							$query->the_post();
							?>
							<div class="construktly-service-single">
								<div class="construktly-service-img">
									<a href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail(); ?>
									</a>
								</div>
								<div class="construktly-service-detail">
									<h3 class="service-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<?php the_excerpt(); ?>
									<div class="construktly-button construktly-button-noborder-noalternate">
										<a href="<?php the_permalink(); ?>"><?php echo esc_html( get_theme_mod( 'construktly_excerpt_more_text', __( 'Read More', 'construktly' ) ) ); ?></a>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			<?php endif; ?>
			<?php if ( ! empty( $service_button_label ) ) { ?>
				<div class="bottom-viewall-button construktly-button">
					<a href="<?php echo esc_url( $service_button_link ); ?>"><?php echo esc_html( $service_button_label ); ?></a>
				</div>
			<?php } ?>
		</div>
	</section>
	<?php
}
