<?php

/**
 * Simple 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-simple-nav';

$wrapper_attributes = get_block_wrapper_attributes(
  [
    'class' => $class_name
  ]
);
$logo_simple_nav = get_field('logo_simple_nav');
$cta = get_field('cta');

// TODO: validate this block
?>
<div <?php echo $anchor ?><?php echo $wrapper_attributes; ?>>
  <div class="olympus-simple-nav-container">
    <div class="olympus-simple-nav-logo-wrap">
      <a href="<?php echo get_home_url(); ?>" class="olympus-simple-nav-logo">
        <?php if ($logo_simple_nav): ?>
          <img src="<?php echo $logo_simple_nav['url'] ?>" alt="<?php echo $logo_simple_nav['alt'] ?>" />
        <?php endif; ?>
      </a>
    </div>
    <div class="olympus-simple-nav-items-wrap desktop">
      <div class="olympus-simple-nav-items">
        <?php
        if (have_rows('items')):
          while (have_rows('items')) : the_row();
            $item = get_sub_field('item');
            // TODO: add submenu support
        ?>
            <?php if ($item): ?>
              <a href="<?php echo $item['url'] ?>" class="olympus-simple-nav-primary-link"><?php echo $item['title'] ?></a>
            <?php endif; ?>
        <?php
          endwhile;
        endif;
        ?>
      </div>
    </div>
    <div class="olympus-simple-nav-cta-wrap">
      <?php if ($cta): ?>
        <div class="wp-block-button">
          <a href="<?php echo $cta['url'] ?>" class="wp-block-button__link wp-element-button"><?php echo $cta['title'] ?></a>
        </div>
      <?php endif; ?>
    </div>
    <div class="olympus-simple-nav-items-wrap mobile">
      <div class="olympus-simple-nav-top-bar">
      </div>
      <div class="olympus-simple-nav-items">
        <div class="olympus-simple-nav-toggle">
          <div class="line-1"></div>
          <div class="line-2"></div>
          <div class="line-3"></div>
        </div>
      </div>
    </div>
  </div>
  <div class="olympus-simple-nav-mobile" style="display:none;">
    <?php
    if (have_rows('items')):
      while (have_rows('items')) : the_row();
        $item = get_sub_field('item');
        // TODO: add submenu support
    ?>
        <?php if ($item): ?>
          <a href="<?php echo $item['url'] ?>" class="olympus-simple-nav-mobile-link"><?php echo $item['title'] ?></a>
        <?php endif; ?>
    <?php
      endwhile;
    endif;
    ?>
    <?php if ($cta): ?>
      <div class="wp-block-button">
        <a href="<?php echo $cta['url'] ?>" class="wp-block-button__link wp-element-button"><?php echo $cta['title'] ?></a>
      </div>
    <?php endif; ?>
  </div>
</div>