<?php


add_action('wp_enqueue_scripts', function() {
    wp_enqueue_script('jquery');
    wp_enqueue_script('isotope', 'https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js', [], null, true);
    wp_enqueue_script('team-bio-slide', get_stylesheet_directory_uri() . '/js/team-bio.js', ['jquery', 'isotope'], null, true);
});
add_action('genesis_after_header', 'team_top_section');
function team_top_section(){
	?>
<div class="team-top-section">
	<div class="sep">
		<svg viewBox="0 0 100 63" fill="none" xmlns="http://www.w3.org/2000/svg">
		<path d="M25.3611 16.5856L20.845 20.932H9.05859V9.85059L25.3611 16.5856Z" fill="#5b2c83"></path>
		<path d="M90.9431 9.85059V20.932H79.1567L74.6406 16.5856L90.9431 9.85059Z" fill="#5b2c83"></path>
		<path d="M92.1805 55.4067H7.81445V62.7943H92.1805V55.4067Z" fill="#5b2c83"></path>
		<path d="M67.3661 20.9315H72.0187L49.9966 0L27.9746 20.9315H32.6271C40.096 20.9315 46.6096 25.0562 49.9966 31.114C53.3837 25.0562 59.8972 20.9315 67.3661 20.9315Z" fill="#5b2c83"></path>
		<path d="M47.5179 40.6311V50.4812H7.54332L0 25.856H32.6298C40.8431 25.856 47.5179 32.4802 47.5179 40.6311Z" fill="#5b2c83"></path>
		<path d="M100 25.856L92.457 50.4812H52.4824V40.6311C52.4824 32.4802 59.1573 25.856 67.3705 25.856H100Z" fill="#5b2c83"></path>
		</svg>
	</div>
	<h2 class="h2">Meet the Team</h2>
</div>
<?php
}
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', function() {
	
    $taxonomy = 'teamtax';  // your custom taxonomy
    $post_type = 'team';

    // Get all taxonomy terms for filters
    /*$terms = get_terms([
        'taxonomy' => $taxonomy,
        'hide_empty' => true,
    ]);*/
   /* if (!$terms || is_wp_error($terms)) $terms = [];?>
    <div class="team-category-filters">
		<button class="team-category-filter-btn active" data-filter="*">Show All</button>
		<?php foreach ($terms as $term) {
			echo '<button class="team-category-filter-btn" data-filter=".' . esc_attr($term->slug) . '">' . esc_html($term->name) . '</button>';
		} ?>
    </div>
	<?php*/
    // Get all team posts once
    $teams = get_posts([
        /*'post_type' => $post_type,
        'posts_per_page' => -1,
		'meta_key' => 'team_order',           // ACF field key
		'orderby' => 'meta_value_num',        // Order numerically
		'order' => 'ASC',
        'tax_query' => [
            [
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => wp_list_pluck($terms, 'slug'),
                'operator' => 'IN',
            ],
        ],*/
		'post_type' => $post_type,
        'posts_per_page' => -1,
        'meta_key' => 'team_order',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_query' => [
            [
                'key' => 'team_info_active_bio_modal',
                'value' => 'Yes',
                'compare' => '=',
            ],
        ],
    ]);

    if (empty($teams)) {
        echo '<p>No team members found.</p>';
        return;
    }

    // Team grid container for Isotope
    echo '<div class="team-grid">';
    foreach ($teams as $team) {
        $id = $team->ID;
		
		$team_member_info = get_field('team_info', $id);
		$team_member_image = $team_member_info['image']['url'];
		$job_position = $team_member_info['job_position'];
		$bio = $team_member_info['bio'];
        $title = get_the_title($id);

        // Get team taxonomy slugs for filtering classes
        $team_terms = get_the_terms($id, $taxonomy);
        $class_list = '';
        if ($team_terms && !is_wp_error($team_terms)) {
            $class_list = implode(' ', wp_list_pluck($team_terms, 'slug'));
        }?>
		
		<div class="team-member <?php echo esc_attr($class_list); ?>" data-member="<?php echo esc_attr($id); ?>">
			<img src="<?php echo $team_member_image; ?>">
			<h3 class="p"><?php echo $title; ?></h3>
			<p class="p-s"><?php echo $job_position; ?></p>
		</div>
    <?php }
    echo '</div>';

    // Output all bio panels hidden offscreen
   foreach ($teams as $team) {
        $id = $team->ID;
		$team_member_info = get_field('team_info', $id);
		$bio = $team_member_info['bio'];
		$job_position = $team_member_info['job_position'];
        $title = get_the_title($id);?>
		<div class="bio-panel " id="bio-<?php echo esc_attr($id);?>">
			<div class="bio-pannel-wrapper">
				<div class="bio-top">
					<h3><?php echo esc_html($title) ?></h3>
					<button class="close-btn" aria-label="Close bio"></button>
				</div>
				<p class="job-position"><?php echo $job_position; ?></p>
				<div class="bio-text">
					<?php echo $bio;?>
				</div>
			</div>
		</div>
		<?php
		
        /*echo '<div class="bio-panel" id="bio-' . esc_attr($id) . '">';
			echo '<button class="close-btn" aria-label="Close bio">&times;</button>';
			echo '<h3>' . esc_html($title) . '</h3>';
			echo '<div class="bio-text">' . $bio . '</div>';
        echo '</div>';*/
    }

    // Overlay div
    echo '<div class="overlay"></div>';
});

genesis();
