<?php

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

$content_id    = $designation = array();
$content_count = get_theme_mod( 'construktly_testimonial_count', 3 );
$content_type  = get_theme_mod( 'construktly_testimonial_content_type', 'page' );

for ( $i = 1; $i <= $content_count; $i++ ) {
	$item_id                 = get_theme_mod( 'construktly_testimonial_content_' . $content_type . '_' . $i );
	$content_id[]            = $item_id;
	$designation[ $item_id ] = get_theme_mod( 'construktly_testimonial_designation_' . $i );
}

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

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

construktly_render_testimonial_section( $args, $designation );

/**
 * Render Testimonial Section
 */
function construktly_render_testimonial_section( $args, $designation ) {
	$section_title     = get_theme_mod( 'construktly_testimonial_section_title', __( 'Clients Testimonials', 'construktly' ) );
	$section_text      = get_theme_mod( 'construktly_testimonial_section_text' );
	?>
	<section id="construktly_testimonial_section" class="construktly-frontpage-section construktly-testimonial-slider-section construktly-grey-background construktly-carousel testimonial-style-2">
		<?php
		if ( is_customize_preview() ) :
			construktly_section_link( 'construktly_testimonial_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<div class="section-header-subtitle section-header-subtitle-left">
				<div class="section-header-subtitle-wrapper">
					<h3 class="section-title"><?php echo esc_html( $section_title ); ?></h3>
					<p class="section-subtitle"><?php echo esc_html( $section_text ); ?></p>
				</div>
				<div class="ascendoor-header-carousel-navigation"></div>
			</div>
			<?php
			$query = new WP_Query( $args );
			if ( $query->have_posts() ) :
				?>
				<div class="construktly-section-body testimonial-body-wrapper">
					<div class="testimonial-body-wrap">
						<div class="construktly-testimonial-slider">
							<?php
							$i = 1;
							while ( $query->have_posts() ) :
								$query->the_post();
								$percentage = get_theme_mod( 'construktly_testimonial_rating_' . $i, 5 );
								$new_width  = ( $percentage / 5 ) * 100;
								?>
								<div class="construktly-testimonial-single">
									<div class="construktly-testimonial-single-inner">
										<div class="construktly-testimonial-user">
											<div class="construktly-testimonial-slider-img">
												<?php the_post_thumbnail(); ?>
											</div>
											<div class="testimonial-user-detail">
												<h4 class="user-name"><?php the_title(); ?></h4>
												<div class="designation"><?php echo esc_html( $designation[ get_the_ID() ] ); ?></div>
											</div>
										</div>
										<div class="construktly-testimonial-comment">
											<?php the_content(); ?>
										</div>
									</div>
								</div>
								<?php
								$i++;
							endwhile;
							wp_reset_postdata();
							?>
						</div>
					</div>
				</div>
				<?php
			endif;
			?>
		</div>
	</section>
	<?php
}
