<?php
$statuses = get_field_object('comity', get_the_ID());
$comities_field_object = get_field_object('field_6836da6d03ff6');
$comities = $comities_field_object ? $comities_field_object['choices'] : [];

$field_auteurs = get_field_object('auteurs', 'group_auteurs');
$placeholder_thumbnail_counter = 0;



$authors = get_posts(array(
	'post_type' => 'auteurs',
	'posts_per_page' => -1,
	'orderby' => 'title',
	'order' => 'ASC',
));
?>

<section class="team-authors content-section alignwide">
	<h2 class="title-small">Équipe dynamiques</h2>
	<h3 class="subtitle-big">À la croisée des plumes et des luttes</h3>


	<div class="container">
		<?php foreach ($comities as $comity_value => $comity_label) : ?>
			<?php $comities_related_authors = get_posts(array(
				'post_type' => 'auteurs',
				'posts_per_page' => -1,
				'meta_key' => 'last_name',
				'orderby' => 'meta_value',
				'order' => 'ASC',
				'meta_query' => array(
					array(
						'key' => 'comity',
						'value' => $comity_value,
						'compare' => 'LIKE',
					),
				),
			));

			if (count($comities_related_authors) < 1) continue;

			?>
			<div class="comity-type">
				<h3 class="comity-type__title"><?php echo $comity_label; ?></h3>


				<ul class="comity-type__list">
					<?php foreach ($comities_related_authors as $author) : ?>
						<?php

						$author_thumbnail = get_field('profile_thumbnail', $author->ID);
						$author_description = get_field('description', $author->ID);
						$author_email = get_field('email', $author->ID);

						$is_carhop_member = get_field('is_carhop_member', $author->ID);
						$carhop_member_id = get_field('carhop_member', $author->ID);

						if ($is_carhop_member && isset($carhop_member_id)) {
							switch_to_blog(1);
							$author_description = get_field('description', $carhop_member_id);
							$author_email = get_field('email', $carhop_member_id);
							$comity = get_field('comity', $carhop_member_id);
							restore_current_blog();
						}

						$is_director = $author->ID === 476 ? true : false;

						?>
						<li class="comity-type__item author-card <?php echo $is_director ? 'author-card--director' : ''; ?>">
							<div class="author-card__profile-picture">
								<?php if ($author_thumbnail) : ?>
									<a href="<?php echo get_the_permalink($author->ID); ?>">
										<img src="<?php echo $author_thumbnail['url']; ?>" alt="<?php echo $author_thumbnail['alt']; ?>">
									</a>
								<?php else : ?>
									<a href="<?php echo get_the_permalink($author->ID); ?>">
										<div class="author-card__profile-picture-placeholder author-card__profile-picture-placeholder--<?php echo $placeholder_thumbnail_counter % 3 + 1; ?>n">
											<!-- <img class="" src="<?php echo get_template_directory_uri(); ?>/assets/images/placeholder-author.png" alt="Placeholder author"> -->
										</div>
									</a>
									<?php $placeholder_thumbnail_counter++; ?>
								<?php endif; ?>
							</div>
							<h4 class="author-card__name">
								<a href="<?php echo get_the_permalink($author->ID); ?>"> <?php echo $author->post_title; ?></a>
							</h4>

							<p class="author-card__bio"><?php echo $author_description; ?></p>
							<?php if ($author_email) : ?>
								<a href="mailto:<?php echo $author_email; ?>" class="author-card__email cta cta--classic cta--rounded cta--has-icon cta--mailing">
									<div class="cta__icon">
										<?php $mail_svg_path = get_template_directory() . '/resources/img/icons/carhop-mail.svg';
										if (file_exists($mail_svg_path)) {
											echo file_get_contents($mail_svg_path);
										} ?>
									</div>
									Envoyer un email
								</a>
							<?php endif; ?>
						</li>


					<?php endforeach; ?>
				</ul>


			</div>
		<?php endforeach; ?>
	</div>
</section>