<?php

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

$content_ids  = array();
$content_type = get_theme_mod( 'corporaze_service_content_type', 'page' );

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

	$args = array(
		'post_type'           => $content_type,
		'posts_per_page'      => absint( 3 ),
		'ignore_sticky_posts' => true,
	);

	if ( ! empty( array_filter( $content_ids ) ) ) {
		$args['post__in'] = array_filter( $content_ids );
		$args['orderby']  = 'post__in';
	} else {
		$args['orderby'] = 'date';
	}

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

	corporaze_render_service_section( $args );

	/**
	 * Render Service Section.
	 */
	function corporaze_render_service_section( $args ) {
		$section_title        = get_theme_mod( 'corporaze_service_title', __( 'Our Services', 'corporaze' ) );
		$section_text         = get_theme_mod( 'corporaze_service_text' );
		$post_button_label    = get_theme_mod( 'corporaze_service_posts_button_label', __( 'View Detail', 'corporaze' ) );
		$service_button_label = get_theme_mod( 'corporaze_service_button_label', __( 'View All Services', 'corporaze' ) );
		$service_button_link  = get_theme_mod( 'corporaze_service_button_link' );
		$service_button_link  = ! empty( $service_button_link ) ? $service_button_link : '#';

		$query = new WP_Query( $args );
		?>
		<section id="corporaze_service_section" class="corporaze-frontpage-section corporaze-our-services-section service-style-1">
			<?php
			if ( is_customize_preview() ) :
				corporaze_section_link( 'corporaze_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="corporaze-section-body">
					<div class="corporaze-our-services-section-wrapper corporaze-navigation">
						<?php
						$i = 1;
						while ( $query->have_posts() ) :
							$query->the_post();
							$service_icon = get_theme_mod( 'corporaze_service_icon_' . $i );
							?>
							<div class="corporaze-service-single wow fadeInUp" data-wow-delay="<?php echo esc_attr( $i * 200 ); ?>ms">
								<?php if ( ! empty( $service_icon ) ) : ?>
									<div class="corporaze-service-img">
										<img src="<?php echo esc_url( $service_icon ); ?>">
									</div>
								<?php endif; ?>
								<div class="corporaze-service-detail">
									<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( $post_button_label ) ) : ?>
									<div class="corporaze-button corporaze-button-noborder-noalternate">
										<a href="<?php the_permalink(); ?>"><?php echo esc_html( $post_button_label ); ?></a>
									</div>
									<?php endif; ?>
								</div>
							</div>
							<?php
							$i++;
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
				<?php endif; ?>
				<?php if ( ! empty( $service_button_label ) ) { ?>
				<div class="bottom-viewall-button corporaze-button">
					<a href="<?php echo esc_url( $service_button_link ); ?>"><?php echo esc_html( $service_button_label ); ?></a>
				</div>
				<?php } ?>
			</div>
		</section>
		<?php
	}
