<?php

/**
 * Full Width Image 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 = 'full-width-image-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

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

$media_type = get_field( 'choose_media_type', $block['id'] );
$choose_image = get_field( 'choose_image', $block['id'] );
$choose_video_option = get_field( 'choose_video_option', $block['id'] );
$video_url = get_field( 'video_url', $block['id'] );
$choose_video = get_field( 'choose_video', $block['id'] );

$has_caption = get_field( 'has_caption', $block['id'] )[0];
$image_caption = get_field( 'image_caption', $block['id'] );
$media_title = get_field( 'media_title', $block['id'] );
$media_link_text = get_field( 'media_link_text', $block['id'] );
$media_link_url = get_field( 'media_link_url', $block['id'] );

$has_texts = get_field( 'has_texts', $block['id'] )[0];
$texts_location = get_field( 'texts_location', $block['id'] );

if ( $has_texts == 'yes' ) {

    if ( have_rows( 'texts_block' ) ) {
        $text = '';
        $link_img = '';
        while ( have_rows( 'texts_block' ) ) {
            the_row();
            $text .= '<div class="text-block ' . ( is_front_page() ? '' : 'short-line' ) . '">';
            $text .= get_sub_field( 'enter_texts', $block['id'] );

            $custom_url = get_sub_field( 'custom_url', $block['id'] );
            $select_a_post = get_sub_field( 'select_a_post', $block['id'] );

            if ( get_sub_field( 'has_cta', $block['id'] )[0] == 'yes' ) {
                if ( get_sub_field( 'link_options', $block['id'] ) == 'custom_url' ) {
                    $text .= '<a href="' . $custom_url . '" class="cta">' . get_sub_field( 'link_text' ) . '</a>';
                    $link_img .= '<a href="' . $custom_url . '">';
                } else {
                    $text .= '<a href="' . get_permalink( $select_a_post->ID) . '" class="cta">' . get_sub_field( 'link_text', $block['id'] ) . '</a>';
                    $link_img .= '<a href="' . get_permalink( $select_a_post->ID) . '">';
                }
                
            }
            $text .= '</div>';
            
        }
    }

}

?>

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

    <?php 
        if ( $texts_location == 'top' ) {
            echo '<div class="text-block-wrapper">' . $text . '</div>';
        }
    ?>
        
    <div class="mpwbpk-image full-width media">
        <?php 

            if ( $media_type == 'image' ) {

                if ( $link_img !== '' ) {
                    echo $link_img . '<img src="' . $choose_image['sizes']['large'] . '" style="background-image: url(' . $choose_image['sizes']['thumbnail'] . ');" class="img-fluid" width="1084" height="600" alt="">' . '</a>';
                } else {
                    echo '<img src="' . $choose_image['sizes']['full-width-img'] . '" class="img-fluid" alt="">';
                }

            } else {

                if ( $choose_video_option == 'url') {
                    
                    echo '<div class="video video__external" data-youtube-id="'. $video_url .'" data-show-controls="' . ( get_field( 'show_controls', $block['id'] )[0] == 'yes' ? 'yes' : '' ) . '"></div>';
                    
                } else {
                    
                    echo '<video ' . ( get_field( 'show_controls', $block['id'] )[0] == 'yes' ? 'controls' : '' ) . ' autoplay muted loop id="myVideo">
                            <source src="' . $choose_video['url'] . '" type="video/mp4">
                        </video>';
                    
                }

            }
        
        ?>
            
            
        <?php 
            if ( $has_caption == 'yes' ) {
                echo '<div class="caption">' . $image_caption . '</div>';
            }
        ?>
    </div>

    <?php 
        if ( $texts_location == 'bottom' ) {
            echo '<div class="text-block-wrapper bottom">' . $text . '</div>';
        }
    ?>
    
</section>

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