<?php

/**
 * Supporters 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 = 'supporters-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

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

?>

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

    <?php 
    
        if ( get_field( 'caption_text', $block['id'] ) ) {
            echo '<h2>' . get_field( 'caption_text', $block['id'] ) . '</h2>';
        }

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

        if ( is_array( $supporters ) && count( $supporters ) > 0 ) {
            $text = '<div class="table-wrapper"><div class="table-responsive"><table class="supporters__list">';
            foreach( $supporters as $key => $item ) {
                $text .= '<tr>
                            <td><span>' . $item['supporter_name'] . '</span></td>
                            <td>' . $item['supporter_country'] . '</td>
                            <td>' . $item['year_of_support'] . '</td>
                        </tr>';
            }
            $text .= '</table></div></div>';

            echo $text;
        }

    ?>
    
</section>

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