<?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');

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

<section class="team-authors content-section">
	<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) : ?>
			<div class="comity-type">
				<h3 class="comity-type__title"><?php echo $comity_label; ?></h3>

				<?php $comities_related_authors = get_posts(array(
					'post_type' => 'auteurs',
					'posts_per_page' => -1,
					'orderby' => 'title',
					'order' => 'ASC',
					'meta_query' => array(
						array(
							'key' => 'comity',
							'value' => $comity_value,
							'compare' => '=',
						),
					),
				)); ?>


				<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);
						write_log($author_email);
						?>
						<li class="comity-type__item author-card">
							<div class="author-card__thumbnail">
								<?php if ($author_thumbnail) : ?>
									<img src="<?php echo $author_thumbnail['url']; ?>" alt="<?php echo $author_thumbnail['alt']; ?>">
								<?php else : ?>
									<img src="<?php echo get_template_directory_uri(); ?>/assets/images/placeholder-author.png" alt="Placeholder author">
								<?php endif; ?>
							</div>
							<h4 class="author-card__name"><?php echo $author->post_title; ?></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>