<?php

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

$content_id   = $designation = $social_links = $section_content = array();
$content_type = get_theme_mod( 'corporaze_team_content_type', 'page' );

for ( $i = 1; $i <= 3; $i++ ) {
	$team_id                 = get_theme_mod( 'corporaze_team_content_' . $content_type . '_' . $i );
	$content_id[]            = $team_id;
	$designation[ $team_id ] = get_theme_mod( 'corporaze_team_designation_' . $i );
	$social_links_str        = get_theme_mod( 'corporaze_team_social_links_' . $i );
	if ( ! empty( $social_links_str ) ) {
		$social_links[ $team_id ] = explode( ',', get_theme_mod( 'corporaze_team_social_links_' . $i ) );
	}
}

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

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

$query = new WP_Query( $args );
if ( $query->have_posts() ) :
	while ( $query->have_posts() ) :
		$query->the_post();
		$section_content[] = array(
			'id'            => get_the_ID(),
			'title'         => get_the_title(),
			'permalink'     => get_the_permalink(),
			'thumbnail_url' => get_the_post_thumbnail_url( get_the_ID(), 'full' ),
		);
	endwhile;
	wp_reset_postdata();
endif;

$section_content = apply_filters( 'corporaze_team_section_content', $section_content );

corporaze_render_team_section( $section_content, $designation, $social_links );

/**
 * Render Team Section.
 */
function corporaze_render_team_section( $section_content, $designation, $social_links ) {
	$section_title     = get_theme_mod( 'corporaze_team_section_title', __( 'Our Team', 'corporaze' ) );
	$section_text      = get_theme_mod( 'corporaze_team_section_text' );
	$team_button_label = get_theme_mod( 'corporaze_team_button_label', __( 'View All', 'corporaze' ) );
	$team_button_link  = get_theme_mod( 'corporaze_team_button_link' );
	$team_button_link  = ! empty( $team_button_link ) ? $team_button_link : '#';

	?>
	<section id="corporaze_team_section" class="corporaze-frontpage-section corporaze-teams-section team-style-2">
		<?php
		if ( is_customize_preview() ) :
			corporaze_section_link( 'corporaze_team_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<?php if ( ! empty( $section_title || $section_text ) ) : ?>
				<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 endif; ?>
			<div class="corporaze-section-body">
				<div class="corporaze-teams-section-wrapper corporaze-navigation">
					<?php
					$i = 1;
					foreach ( $section_content as $content ) {
						$team_id = $content['id'];
						?>
						<div class="teams-single wow fadeInUp" data-wow-delay="<?php echo esc_attr( $i * 200 ); ?>ms">
							<?php if ( ! empty( $content['thumbnail_url'] ) ) { ?>
								<div class="teams-img">
									<img src="<?php echo esc_html( $content['thumbnail_url'] ); ?>" class="trending_item_img" alt="<?php echo esc_attr( $content['title'] ); ?>">
								</div>
							<?php } ?>
							<div class="teams-detial">
								<h3 class="team-name">
									<a href="<?php echo esc_url( $content['permalink'] ); ?>"><?php echo esc_html( $content['title'] ); ?></a>
								</h3>
								<?php if ( ! empty( $designation[ $team_id ] ) ) : ?>
									<p class="team-designation"><?php echo esc_html( $designation[ $team_id ] ); ?></p>
									<?php
								endif;
								if ( ! empty( $social_links[ $team_id ] ) ) {
									?>
									<div class="teams-social">
										<?php foreach ( $social_links[ $team_id ] as $link ) { ?>
											<a href="<?php echo esc_url( $link ); ?>" target="_blank"></a>
										<?php } ?>
									</div>
								<?php } ?>
							</div>
						</div>
						<?php
						$i++;
					}
					?>
				</div>
				<?php if ( ! empty( $team_button_label ) ) { ?>
					<div class="corporaze-teams-view-all corporaze-button">
						<a href="<?php echo esc_url( $team_button_link ); ?>"><?php echo esc_html( $team_button_label ); ?></a>
					</div>
				<?php } ?>
			</div>
		</div>
	</section>
	<?php
}
