<?php

/**
 * Main nav Block Template.
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during backend preview render.
 * @param   int $post_id The post ID the block is rendering content against.
 *          This is either the post ID currently being displayed inside a query loop,
 *          or the post ID of the post hosting this block.
 * @param   array $context The context provided to the block by the post or it's parent block.
 */

// Support custom "anchor" values.
$anchor = '';
if (! empty($block['anchor'])) {
  $anchor = 'id="' . esc_attr($block['anchor']) . '" ';
}

// Load values and assign defaults.
$class_name = 'olympus-main-nav';

$theme = get_field('theme', 'option');
if (get_field('use_a_different_theme_variation') && get_field('theme')) $theme = get_field('theme');

if ($theme) {
  $class_name .= ' ' . $theme;
}

$wrapper_attributes = get_block_wrapper_attributes(
  [
    'class' => $class_name
  ]
);
$logo_main_nav = get_field('logo_main_nav', 'option');
$menu_call_to_action = get_field('menu_call_to_action', 'option');
$menu_call_to_action_2 = get_field('menu_call_to_action_2', 'option');
$cta_has_submenu = get_field('cta_has_submenu', 'option');
$cta_has_submenu_2 = get_field('cta_has_submenu_2', 'option');
$hide_top_nav = get_field('hide_top_nav');

$menu_items_color = (get_field('menu_items_color') && $theme == 'accelerator') ? get_field('menu_items_color') : '#000000';

// TODO: add override color options in block
?>
<div <?php echo $anchor ?><?php echo $wrapper_attributes; ?>>
  <?php if (!$hide_top_nav): ?>
    <div class="olympus-top-nav">
      <div class="olympus-top-nav-wrap">
        <div class="olympus-top-nav-spacer"></div>
        <div class="olympus-top-nav-contact">
          <?php
          if (have_rows('general_contact_info', 'option')):
            while (have_rows('general_contact_info', 'option')) : the_row();
              $icon = get_sub_field('icon');
              if ($icon) {
                $icon = file_get_contents(get_template_directory() . '/admin/img/acf/' . $icon . '.svg');
              }
              $link = get_sub_field('link');
              if ($link):
          ?>
                <a href="<?php echo $link['url'] ?>" target="<?php echo $link['target'] ?>" class="olympus-top-nav-link">
                  <div class="olympus-top-nav-link-icon"><?php echo $icon ?></div>
                  <span><?php echo $link['title'] ?></span>
                </a>
          <?php
              endif;
            endwhile;
          endif;
          ?>
        </div>
        <div class="olympus-top-nav-socials">
          <?php
          if (have_rows('social_icons', 'option')):
            while (have_rows('social_icons', 'option')) : the_row();
              $icon = get_sub_field('icon');
              if ($icon) {
                $icon = file_get_contents(get_template_directory() . '/admin/img/acf/' . $icon . '.svg');
              }
              $link = get_sub_field('link');
              if ($link):
          ?>
                <a href="<?php echo $link['url'] ?>" target="<?php echo $link['target'] ?>" class="olympus-top-nav-link">
                  <div class="olympus-top-nav-link-icon"><?php echo $icon ?></div>
                </a>
          <?php
              endif;
            endwhile;
          endif;
          ?>
        </div>
      </div>
    </div>
  <?php endif; ?>
  <div class="olympus-main-nav-wrap">
    <div class="olympus-main-nav-items">
      <a href="<?php echo get_site_url(); ?>" class="olympus-main-nav-logo">
        <?php if ($logo_main_nav): ?>
          <img src="<?php echo $logo_main_nav['url'] ?>" alt="<?php echo $logo_main_nav['alt'] ?>" />
        <?php endif; ?>
      </a>
      <div class="olympus-main-nav-items hide-on-mobile hide-on-tablet">
        <?php
        $counter = 0;
        if (have_rows('menu_items', 'option')):
          while (have_rows('menu_items', 'option')) : the_row();
            $item = get_sub_field('item');
            $has_submenu = get_sub_field('has_submenu');
            $counter++;
        ?>

            <?php if ($has_submenu): ?>

              <div class="olympus-main-nav-item-submenu-wrap">
                <?php if ($item): ?>
                  <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-item has-submenu olympus-main-nav-item-submenu-trigger" style="color:<?php echo $menu_items_color ?>;">
                    <?php echo $item['title'] ?>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
                      <path d="M238 429.3l22.6-22.6 192-192L475.3 192 430 146.7l-22.6 22.6L238 338.7 68.6 169.4 46 146.7 .7 192l22.6 22.6 192 192L238 429.3z"></path>
                    </svg>
                  </a>
                <?php endif; ?>

                <div class="olympus-main-nav-item-submenu submenu-<?php echo $counter ?>" style="display:none;">
                  <div class="olympus-main-nav-item-submenu-content">
                    <?php
                    $oneHasSubmenuSubmenu = false;
                    if (have_rows('submenu_items', 'option')):
                      while (have_rows('submenu_items', 'option')) : the_row();
                        $item = get_sub_field('item');
                        $has_submenu_submenu = get_sub_field('has_submenu_submenu');
                    ?>

                        <?php if ($has_submenu_submenu):
                          $oneHasSubmenuSubmenu = true;
                        ?>

                          <div class="olympus-main-nav-submenu-submenu">
                            <?php if ($item): ?>
                              <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-title main-nav-popup-inside-link">
                                <?php echo $item['title'] ?>
                              </a>
                            <?php endif; ?>

                            <?php
                            if (have_rows('submenu_submenu_items', 'option')):
                              while (have_rows('submenu_submenu_items', 'option')) : the_row();
                                $item = get_sub_field('item');
                            ?>
                                <?php if ($item): ?>
                                  <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-submenu-item main-nav-popup-inside-link">
                                    <?php echo $item['title'] ?>
                                  </a>
                                <?php endif; ?>
                            <?php
                              endwhile;
                            endif;
                            ?>
                          </div>

                        <?php else: ?>

                          <?php if ($item): ?>
                            <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-item main-nav-popup-inside-link">
                              <?php echo $item['title'] ?>
                            </a>
                          <?php endif; ?>


                        <?php endif; ?>

                    <?php
                      endwhile;
                    endif;
                    ?>
                    <?php if ($oneHasSubmenuSubmenu) {
                      echo '<style>.olympus-main-nav-item-submenu.submenu-' . $counter . '{width:580px;left:-290px;}</style>';
                    } ?>
                  </div>
                </div>
              </div>

            <?php else: ?>

              <?php if ($item): ?>
                <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-item" style="color:<?php echo $menu_items_color ?>;" target="<?php echo $item['target'] ?>">
                  <?php echo $item['title'] ?>
                </a>
              <?php endif; ?>

            <?php endif; ?>

        <?php
          endwhile;
        endif;
        ?>

        <?php if ($menu_call_to_action): ?>

          <?php if ($cta_has_submenu): ?>
            <div class="olympus-main-nav-item-submenu-wrap">
              <div class="wp-block-button is-style-no-arrow-button olympus-main-nav-item-submenu-trigger nav-btn-1">
                <a href="<?php echo $menu_call_to_action['url'] ?>" class="wp-block-button__link wp-element-button"><?php echo $menu_call_to_action['title'] ?></a>
              </div>

              <div class="olympus-main-nav-item-submenu submenu-cta" style="display:none;">
                <div class="olympus-main-nav-item-submenu-content">
                  <?php
                  $hasSubmenuSubmenu = false;
                  if (have_rows('cta_submenu', 'option')):
                    while (have_rows('cta_submenu', 'option')) : the_row();
                      $item = get_sub_field('item');
                      $has_submenu = get_sub_field('has_submenu');
                  ?>

                      <?php if ($has_submenu):
                        $hasSubmenuSubmenu = true;
                      ?>

                        <div class="olympus-main-nav-submenu-submenu">
                          <?php if ($item): ?>
                            <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-title main-nav-popup-inside-link">
                              <?php echo $item['title'] ?>
                            </a>
                          <?php endif; ?>

                          <?php
                          if (have_rows('submenu_items', 'option')):
                            while (have_rows('submenu_items', 'option')) : the_row();
                              $item = get_sub_field('item');
                          ?>
                              <?php if ($item): ?>
                                <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-submenu-item main-nav-popup-inside-link">
                                  <?php echo $item['title'] ?>
                                </a>
                              <?php endif; ?>
                          <?php
                            endwhile;
                          endif;
                          ?>
                        </div>

                      <?php else: ?>

                        <?php if ($item): ?>
                          <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-item main-nav-popup-inside-link">
                            <?php echo $item['title'] ?>
                          </a>
                        <?php endif; ?>

                      <?php endif; ?>

                  <?php
                    endwhile;
                  endif;
                  ?>
                  <?php if ($hasSubmenuSubmenu) {
                    echo '<style>.olympus-main-nav-item-submenu.submenu-cta{width:580px;}</style>';
                  } ?>
                </div>
              </div>
            </div>
          <?php else: ?>
            <div class="wp-block-button is-style-no-arrow-button nav-btn-1">
              <a href="<?php echo $menu_call_to_action['url'] ?>" class="wp-block-button__link wp-element-button"><?php echo $menu_call_to_action['title'] ?></a>
            </div>
          <?php endif; ?>

        <?php endif; ?>

        <?php if ($menu_call_to_action_2): ?>

          <?php if ($cta_has_submenu_2): ?>
            <div class="olympus-main-nav-item-submenu-wrap">
              <div class="wp-block-button is-style-no-arrow-button olympus-main-nav-item-submenu-trigger nav-btn-2">
                <a href="<?php echo $menu_call_to_action_2['url'] ?>" class="wp-block-button__link wp-element-button"><?php echo $menu_call_to_action_2['title'] ?></a>
              </div>

              <div class="olympus-main-nav-item-submenu submenu-cta" style="display:none;">
                <div class="olympus-main-nav-item-submenu-content">
                  <?php
                  $hasSubmenuSubmenu = false;
                  if (have_rows('cta_submenu_2', 'option')):
                    while (have_rows('cta_submenu_2', 'option')) : the_row();
                      $item = get_sub_field('item');
                      $has_submenu = get_sub_field('has_submenu');
                  ?>

                      <?php if ($has_submenu):
                        $hasSubmenuSubmenu = true;
                      ?>

                        <div class="olympus-main-nav-submenu-submenu">
                          <?php if ($item): ?>
                            <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-title main-nav-popup-inside-link">
                              <?php echo $item['title'] ?>
                            </a>
                          <?php endif; ?>

                          <?php
                          if (have_rows('submenu_items', 'option')):
                            while (have_rows('submenu_items', 'option')) : the_row();
                              $item = get_sub_field('item');
                          ?>
                              <?php if ($item): ?>
                                <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-submenu-item main-nav-popup-inside-link">
                                  <?php echo $item['title'] ?>
                                </a>
                              <?php endif; ?>
                          <?php
                            endwhile;
                          endif;
                          ?>
                        </div>

                      <?php else: ?>

                        <?php if ($item): ?>
                          <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-item main-nav-popup-inside-link">
                            <?php echo $item['title'] ?>
                          </a>
                        <?php endif; ?>

                      <?php endif; ?>

                  <?php
                    endwhile;
                  endif;
                  ?>
                  <?php if ($hasSubmenuSubmenu) {
                    echo '<style>.olympus-main-nav-item-submenu.submenu-cta{width:580px;}</style>';
                  } ?>
                </div>
              </div>
            </div>
          <?php else: ?>
            <div class="wp-block-button is-style-no-arrow-button nav-btn-2">
              <a href="<?php echo $menu_call_to_action_2['url'] ?>" class="wp-block-button__link wp-element-button"><?php echo $menu_call_to_action_2['title'] ?></a>
            </div>
          <?php endif; ?>

        <?php endif; ?>
      </div>
      <div class="hide-on-desktop">
        <div class="olympus-mobile-nav-toggle">
          <div class="line-1"></div>
          <div class="line-2"></div>
          <div class="line-3"></div>
        </div>
      </div>
    </div>
  </div>
  <div class="olympus-main-nav-mobile hide-on-desktop" style="display:none;">
    <?php
    $counter = 0;
    if (have_rows('menu_items', 'option')):
      while (have_rows('menu_items', 'option')) : the_row();
        $item = get_sub_field('item');
        $has_submenu = get_sub_field('has_submenu');
        $counter++;
    ?>

        <?php if ($has_submenu): ?>


          <div class="main-nav-primary-link-mobile dropdown" tabindex="0">
            <a href="<?php echo $item['url'] ?>"><?php echo $item['title'] ?></a>
            <div class="main-nav-mobile-dropdown-toggle">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
                <path d="M238 429.3l22.6-22.6 192-192L475.3 192 430 146.7l-22.6 22.6L238 338.7 68.6 169.4 46 146.7 .7 192l22.6 22.6 192 192L238 429.3z" />
              </svg>
            </div>
          </div>

          <div class="main-nav-mobile-dropdown-content-container" style="display:none;">
            <?php
            if (have_rows('submenu_items', 'option')):
              while (have_rows('submenu_items', 'option')) : the_row();
                $item = get_sub_field('item');
                $has_submenu_submenu = get_sub_field('has_submenu_submenu');
            ?>
                <?php if ($has_submenu_submenu): ?>

                  <div class="olympus-main-nav-submenu-submenu">
                    <?php if ($item): ?>
                      <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-title main-nav-popup-inside-link">
                        <?php echo $item['title'] ?>
                      </a>
                    <?php endif; ?>
                    <?php
                    if (have_rows('submenu_submenu_items', 'option')):
                      while (have_rows('submenu_submenu_items', 'option')) : the_row();
                        $item = get_sub_field('item');
                    ?>
                        <?php if ($item): ?>
                          <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-submenu-item main-nav-popup-inside-link">
                            <?php echo $item['title'] ?>
                          </a>
                        <?php endif; ?>
                    <?php
                      endwhile;
                    endif;
                    ?>
                  </div>

                <?php else: ?>
                  <?php if ($item): ?>
                    <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-submenu-item main-nav-popup-inside-link">
                      <?php echo $item['title'] ?>
                    </a>
                  <?php endif; ?>
                <?php endif; ?>
            <?php
              endwhile;
            endif;
            ?>
          </div>

        <?php else: ?>

          <?php if ($item): ?>
            <a href="<?php echo $item['url'] ?>" class="olympus-main-nav-item">
              <?php echo $item['title'] ?>
            </a>
          <?php endif; ?>

        <?php endif; ?>

    <?php
      endwhile;
    endif;
    ?>
    <?php if ($menu_call_to_action): ?>
      <a href="<?php echo $menu_call_to_action['url'] ?>" class="olympus-main-nav-item">
        <?php echo $menu_call_to_action['title'] ?>
      </a>
    <?php endif; ?>
    <?php if ($menu_call_to_action_2): ?>
      <a href="<?php echo $menu_call_to_action_2['url'] ?>" class="olympus-main-nav-item">
        <?php echo $menu_call_to_action_2['title'] ?>
      </a>
    <?php endif; ?>
  </div>
</div>