<?php

/**
 * Team Member Block Template.
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during AJAX preview.
 * @param   (int|string) $post_id The post ID this block is saved to.
 */

// Create id attribute allowing for custom "anchor" value.
$id = 'team-member-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

// Create class attribute allowing for custom "className" and "align" values.
$className = 'team-member';
if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
}

?>

<section id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">

    <?php 
        if ( get_field( 'block_caption', $block['id'] ) ) : 
            echo '<h2>' . get_field( 'block_caption', $block['id'] ) . '</h2>'; 
        endif; 

        $teams = get_field( 'team', $block['id'] );

        if ( is_array( $teams ) && count( $teams ) > 0 ) {

            $text = '<ul class="teams">';

            foreach( $teams as $key => $item ) {

                $text .= '<li>
                        <img src="' . $item['photo']['sizes']['medium'] . '" class="img-fluid" alt="' . $team['full_name'] . '">
                        <div class="team-info">
                            <h3>' . $item['full_name'] . '</h3>
                            <p>' . $item['position'] . '</p>
                            <p><a href="mailto:' . $item['email'] . '">' . $item['email'] . '</a></p>
                        </div>
                    </li>';

            }

            $text .= '</ul>';

            echo $text;

        }

    ?>

</section>

<?php printf( "<script>window.dispatchEvent( new Event('acf/loaded/team-member'));</script>" );