<?php

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

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

$section_heading = get_field( 'section_heading' );

?>

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

    <h2><?php echo $section_heading ?></h2>

    <?php 

        $choose_events = get_field( 'choose_events' );
        $event_ids = [];
        
        foreach ( $choose_events as $event ) {
           $event_ids[] = $event->ID;
        }
    
        $event_blocks = new WP_Query(
            array(
                'post_type' => 'events',
                'post__in' => $event_ids,
                'orderby'   => 'post__in',
                'order'     => 'ASC'
            )
        );

        if ( $event_blocks->have_posts() ) {
            $i = 1;
            echo '<div class="events-wrapper">';
                while ( $event_blocks->have_posts() ) {
                    $event_blocks->the_post(); ?>

                    <div class="event <?php echo ($i == 1 ? 'featured' : '' ); ?>">
                        <div class="event-meta">
                            <p class="event-date"><?php echo get_field( 'event_date', get_the_ID() ); ?></p>
                            <div class="event-heading">
                                <?php 

                                    $url = get_field( 'custom_url' );
                                    $select_a_post = get_field( 'select_a_post' );

                                    if ( get_field( 'cta_link_option' ) == 'select_a_post' ) {
                                        $url = get_permalink( $select_a_post->ID );
                                    }

                                    if ( $i == 1 ) {
                                        echo '<h3>' . get_the_title() . '</h3>';
                                    } else {
                                        echo '<a href="' . get_the_permalink() . '" class="cta">' . get_the_title() . '</a>';
                                    }
                                
                                    if ( ( $i == 1 ) && ( get_field( 'has_cta_above_the_first_event' )[0] == 'yes' ) ) {
                                        $text = get_field( 'cta_text_above_the_first_event' );
                                        echo '<a href="' . $url . '" class="cta">' . $text . '</a>';
                                    }
                                ?>
                            </div>
                        </div>
                        <div class="event-image">
                            <?php 
                                if ( get_field( 'has_cta_above_the_first_event' )[0] == 'yes' ) {
                                    echo '<a href="' . $url . '"><img src="' . get_thumb_url( get_the_ID(), 'medium_large' ) . '" style="backround-image: url(' . get_thumb_url( get_the_ID(), 'thumbnail' ) . ');" class="img-fluid" width="534" height="352" alt=""></a>';
                                } else {
                                    echo '<img src="' . get_thumb_url( get_the_ID(), 'medium_large' ) . '" style="backround-image: url(' . get_thumb_url( get_the_ID(), 'thumbnail' ) . ');" class="img-fluid" width="534" height="352" alt="">';
                                }
                            ?>
                        </div>
                    </div>

                   <?php $i++;
                }
                wp_reset_postdata();
            echo '</div>';
        }
    
    ?>
    
</section>

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