<?php

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

$charity_elite_content_ids  = array();
$charity_elite_content_type = get_theme_mod( 'charity_elite_service_content_type', 'page' );

for ( $charity_elite_i = 1; $charity_elite_i <= 3; $charity_elite_i++ ) {
	$charity_elite_content_ids[] = get_theme_mod( 'charity_elite_service_content_' . $charity_elite_content_type . '_' . $charity_elite_i );
}

$charity_elite_args = array(
	'post_type'           => $charity_elite_content_type,
	'posts_per_page'      => absint( 3 ),
	'ignore_sticky_posts' => true,
);

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

$charity_elite_args = apply_filters( 'charity_elite_service_section_args', $charity_elite_args );

charity_elite_render_service_section( $charity_elite_args );

/**
 * Render Service Section.
 */
function charity_elite_render_service_section( $charity_elite_args ) {
	$charity_elite_section_title        = get_theme_mod( 'charity_elite_service_title', __( 'What We Do For You', 'charity-elite' ) );
	$charity_elite_section_subtitle     = get_theme_mod( 'charity_elite_service_subtitle', '' );
	$charity_elite_service_button_label = get_theme_mod( 'charity_elite_service_button_label', __( 'Read More', 'charity-elite' ) );
	?>

	<section id="charity_elite_service_section" class="ascendoor-frontpage-section service-section service-style-1">
		<?php
		if ( is_customize_preview() ) :
			charity_elite_section_link( 'charity_elite_service_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<?php if ( ! empty( $charity_elite_section_subtitle || $charity_elite_section_title ) ) { ?>
				<div class="section-header-subtitle">
					<p class="section-subtitle"><?php echo esc_html( $charity_elite_section_subtitle ); ?></p>
					<h3 class="section-title"><?php echo esc_html( $charity_elite_section_title ); ?></h3>
				</div>
				<?php
			}
			$charity_elite_query = new WP_Query( $charity_elite_args );
			if ( $charity_elite_query->have_posts() ) :
				?>
				<div class="section-body">
					<div class="service-section-wrapper">
						<?php
						$charity_elite_i = 1;
						while ( $charity_elite_query->have_posts() ) :
							$charity_elite_query->the_post();
							$charity_elite_service_icon = get_theme_mod( 'charity_elite_service_icon_' . $charity_elite_i, '' );
							?>
							<div class="service-single">
								<?php if ( ! empty( $charity_elite_service_icon ) ) { ?>
									<div class="service-img">
										<img src="<?php echo esc_url( $charity_elite_service_icon ); ?>" alt="<?php the_title_attribute(); ?>">
									</div>
								<?php } ?>
								<div class="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( $charity_elite_service_button_label ) ) : ?>
										<div class="service-button">
											<a href="<?php the_permalink(); ?>" class="ascendoor-button ascendoor-button-noborder-noalternate"><?php echo esc_html( $charity_elite_service_button_label ); ?></a>
										</div>
									<?php endif; ?>
								</div>
							</div>
							<?php
							$charity_elite_i++;
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</section>
	<?php
}
