<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 21/07/18
 * Time: 11.08
 */

function sidebar($model_array)
{
    $acf_string = '<div class="blok sidebar">';
    $wp_post_type = $model_array[o66_model_fields_keys::wp_post_type];
    if ($wp_post_type === 'page') {
        $acf_post_id = get_field('sidebar', $model_array[o66_model_fields_keys::wp_id]);
    } else {
        $acf_post_id = get_field('sidebar', get_field('index_page_' . $wp_post_type, 'option')->ID);

    }
    $field_name = 'sb_content_block';

    //cammpi dinamici tour elicottero
    if ($wp_post_type === 'tour_elicottero') {
        $te_title = '<h2>' . get_label('Dettagli') . '</h2>';
        $list = '';
        $list_elements = '';
        $te_ul_start = '<ul>';
        $giro_content = get_field('giro_in_elicottero', $model_array[o66_model_fields_keys::wp_id]);
        if (!empty($giro_content)) {
            $list_elements .= '<li>' . get_label('Giro in elicottero') . '</li><li class="alternative-list-row"><i class="fa fa-clock-o" aria-hidden="true"></i> ' . $giro_content . '</li>';
        };
        $partenza_content = get_field('partenza', $model_array[o66_model_fields_keys::wp_id]);
        if (!empty($partenza_content)) {
            $list_elements .= '<li>' . get_label('Partenza') . '</li><li class="alternative-list-row"><i class="fa fa-clock-o" aria-hidden="true"></i> ' . $partenza_content . '</li>';
        }
        $prezzo_content = get_field('prezzo', $model_array[o66_model_fields_keys::wp_id]);
        if (!empty($prezzo_content)) {
            $list_elements .= '<li>' . get_label('Prezzo') . '</li><li class="alternative-list-row">' . $prezzo_content . '</li>';
        }
        $te_ul_end = '</ul>';
        $list = $te_ul_start . $list_elements . $te_ul_end;


        $acf_string .= '<div class="content">' . $te_title . $list . '</div>';

    }


    while (have_rows($field_name, $acf_post_id)) :
        the_row(); ?>
        <?php if (get_row_layout() == 'text_editor_block') : ?>
        <?php $acf_string .= '<div class="content">' . get_sub_field('text_editor') . '</div>'; ?>
        <?php // endif; ?>

    <?php elseif (get_row_layout() == 'banner') :
        $banner_image = '<img src="' . get_sub_field('banner_image')['sizes']['rect'] . '" alt="' . get_sub_field('banner_title') . '" class ="img-responsive"/>';
        $banner_title = get_sub_field('banner_title');
        $banner_title_content = '';
        if (!empty($banner_title)) {
            $banner_title_content = html5_div('<h3>' . get_sub_field('banner_title') . '</h3>', null, 'banner_title');
        }
        $banner_link = get_sub_field('banner_link');

        $return_content = $banner_image . $banner_title_content;

        if (!empty($banner_link)) {
            $return_content = '<a href="' . $banner_link . '">' . $banner_image . $banner_title_content . '</a>';
        }


        $acf_string .= html5_div($return_content, null, 'banner __is-video');


        ?>

    <?php elseif (get_row_layout() == 'image') :
        $banner_image = '<img src="' . get_sub_field('banner_image')['sizes']['rect'] . '" alt="' . get_sub_field('banner_title') . '" class ="img-responsive"/>';
//        $banner_title = get_sub_field('banner_title');
//        $banner_title_content = '';
//        if (!empty($banner_title)) {
//            $banner_title_content = html5_div('<h3>' . get_sub_field('banner_title') . '</h3>', null, 'banner_title');
//        }
        $banner_link = get_sub_field('banner_link');

        $return_content = $banner_image ;

        if (!empty($banner_link)) {
            $return_content = '<a href="' . $banner_link . '">' . $banner_image . '</a>';
        }


        $acf_string .= html5_div($return_content, null, 'banner');


        ?>

    <?php elseif (get_row_layout() == 'mariuccia') :
        $banner_image = '<img src="' . get_sub_field('banner_image')['sizes']['rect'] . '" alt="' . get_sub_field('banner_title') . '" class ="img-responsive"/>';
        $banner_title = get_sub_field('banner_title');
        $banner_title_content = '';
        if (!empty($banner_title)) {
            $banner_title_content = html5_div('<h3>' . get_sub_field('banner_title') . '</h3>', null, 'banner_title');
        }
        $banner_link = get_sub_field('banner_link');

        $return_content = $banner_image . $banner_title_content;

        if (!empty($banner_link)) {
            $return_content = '<a href="' . $banner_link . '">' . $banner_image . $banner_title_content . '</a>';
        }


        $acf_string .= html5_div($return_content, null, 'banner __is-video');


        ?>

    <?php

    elseif (get_row_layout() == 'mappe_e_luoghi') :
        $banner_image = '<img src="' . get_sub_field('mel_image')['sizes'][o66_image_sizes::nocrop_side] . '" alt="' . get_sub_field('banner_title') . '" class ="img-responsive"/>';
        $banner_title = get_sub_field('mel_title');
        $banner_text = get_sub_field('mel_text');
        $scopri_label = get_sub_field('scopri_adesso_label');
        $scopri_link = get_sub_field('scopri_adesso_link');
        $prenota_label = get_sub_field('prenota_il_tuo_tuor_label');
        $prenota_link = get_sub_field('prenota_il_tuo_tuor_link');


        $banner_title_content = '';
        if (!empty($banner_title)) {
            $scopri_content = '';
            $prenota_content = '';
            if (!empty($scopri_label)) {
                $scopri_content = '<div class="button_container scopri"><a href="' . $scopri_link . '"><i class="fa fa-calendar"></i> ' . $scopri_label . '</a></div>';
            }
            if (!empty($prenota_label)) {
                $prenota_content = '<div class="button_container prenota"><a href="' . $prenota_link . '"><i class="fa fa-calendar"></i> ' . $prenota_label . '</a></div>';
            }
            $banner_title_content = html5_div('<h3>' . $banner_title . '</h3><p>' . $banner_text . '</p>' . $scopri_content . $prenota_content, null, 'banner_title');
        }
        $banner_link = get_sub_field('banner_link');

        $return_content = $banner_image . $banner_title_content;

        if (!empty($banner_link)) {
            $return_content = '<a href="#">' . $banner_image . '</a>';
        }


        $acf_string .= html5_div($return_content, null, 'mappe_e_luoghi');


        ?>

    <?php


    elseif (get_row_layout() == 'transfer') :
        $return_content = '';
        $style_background = "background-image: url('" . get_sub_field('transfer_image')['sizes'][o66_image_sizes::nocrop_side] . "')";
        //  $banner_image = '<img src="' . get_sub_field('transfer_image')['sizes'][o66_image_sizes::nocrop_side] . '" alt="' . get_sub_field('transfer_title') . '" class ="img-responsive"/>';
        $banner_image = '';
        $banner_title = get_sub_field('transfer_title');
        $scopri_label = get_sub_field('transfer_label');
        $scopri_link = get_sub_field('transfer_link');


        $banner_title_content = '';
        if (!empty($banner_title)) {
            $scopri_content = '';

            if (!empty($scopri_label)) {
                $scopri_content = '<div class="transfer-content"><a href="' . $scopri_link . '"><i class="fa fa-calendar"></i> ' . $scopri_label . '</a></div>';
            }

            $banner_title_content = html5_div('<h3>' . $banner_title . '</h3>' . $scopri_content, null, 'banner_title');
        }
        $banner_link = get_sub_field('banner_link');

        $return_content = $banner_image . $banner_title_content;

        if (!empty($banner_link)) {
            $return_content = '<a href="#">' . $banner_image . $banner_title_content . '</a>';
        }

        //   $acf_string .= '<a href="#i">';
        $acf_string .= '<div class= "blok transfer_background" style="' . $style_background . '"><span  class="blok transfer_span" onClick="
(function(){
    window.location.href = `'.$scopri_link.'`;
    return false;
})();return false;
">' . html5_div($return_content, null, 'transfer') . '</span></div>';
        //   $acf_string .= '</a>'

        ?>

    <?php


    elseif (get_row_layout() == 'gallery_block') : ?>
        <?php $gallery_images = get_sub_field('gallery'); ?>
        <?php if ($gallery_images) :
            $acf_string .= '<div class="entry-content__gallery"><div class="row">';
            foreach ($gallery_images as $gallery_image):
                $acf_string .= "<div class='col-xs-6'>
                                <div class='entry-content__gallery__image'><a href='{$gallery_image["url"]}'><img src='{$gallery_image["sizes"]["quad"]}'
                                             alt='{$gallery_image["alt"]}' class='img-responsive'/>
                                    </a>
                                </div>
                            </div>"; ?>
            <?php endforeach;
            $acf_string .= '
                    </div>
                </div>';
        endif; ?>
    <?php endif; ?>
    <?php endwhile;

    //  endwhile;

    return $acf_string . '</div>';
}