<?php
/*
Template Name: Contacto
*/
get_header();
?>

<main class="fjord-main fjord-contact">
  <?php
  if (have_posts()) {
    the_post();
  }
  $post_content = get_post_field('post_content', get_the_ID());
  $content_blocks = $post_content ? parse_blocks($post_content) : [];
  $hero_attrs = [];
  $filtered_blocks = [];
  foreach ($content_blocks as $block) {
    if (!empty($block['blockName']) && $block['blockName'] === 'fjord/contact-hero') {
      $hero_attrs = isset($block['attrs']) ? (array) $block['attrs'] : [];
      continue;
    }
    $filtered_blocks[] = $block;
  }
  $hero_defaults = [
    'title' => get_theme_mod('fjord_contact_title', "Let's Talk"),
    'formShortcode' => get_theme_mod('fjord_contact_form_shortcode', ''),
    'imageUrl' => 'https://framerusercontent.com/images/tTnxI9bEGHuPLga5HlUAYCJjneY.webp',
    'imageAlt' => 'Contact',
    'cardBackground' => '',
    'textColor' => '',
    'inputBackground' => '',
    'inputText' => '',
    'inputBorder' => '',
    'buttonBackground' => '',
    'buttonText' => '',
    'city1' => 'Prague',
    'address1' => "Vinohradská 121\n130 00 Praha 3\nCzech Republic",
    'cta1' => 'Get Direction',
    'link1' => 'https://maps.google.com/?q=Vinohradská+121+Praha+3',
    'backgroundColor1' => '#f6f6f6',
    'textColor1' => '#0e1011',
    'buttonColor1' => '#0e1011',
    'buttonTextColor1' => '#ffffff',
    'city2' => 'Hamburg',
    'address2' => "Gänsemarkt 2\n20354 Hamburg\nGermany",
    'cta2' => 'Get Direction',
    'link2' => 'https://maps.google.com/?q=Gänsemarkt+2+Hamburg',
    'backgroundColor2' => '#f6f6f6',
    'textColor2' => '#0e1011',
    'buttonColor2' => '#0e1011',
    'buttonTextColor2' => '#ffffff',
  ];
  $values = wp_parse_args($hero_attrs, $hero_defaults);
  $style_vars = [];
  if ($values['cardBackground'] !== '') {
    $style_vars[] = '--nordie-contact-card-bg:' . $values['cardBackground'];
  }
  if ($values['textColor'] !== '') {
    $style_vars[] = '--nordie-contact-text:' . $values['textColor'];
  }
  if ($values['inputBackground'] !== '') {
    $style_vars[] = '--nordie-contact-input-bg:' . $values['inputBackground'];
  }
  if ($values['inputText'] !== '') {
    $style_vars[] = '--nordie-contact-input-text:' . $values['inputText'];
  }
  if ($values['inputBorder'] !== '') {
    $style_vars[] = '--nordie-contact-input-border:' . $values['inputBorder'];
  }
  if ($values['buttonBackground'] !== '') {
    $style_vars[] = '--nordie-contact-button-bg:' . $values['buttonBackground'];
  }
  if ($values['buttonText'] !== '') {
    $style_vars[] = '--nordie-contact-button-text:' . $values['buttonText'];
  }
  $style_attr = $style_vars ? ' style="' . esc_attr(implode(';', $style_vars)) . '"' : '';
  ?>

  <section class="fjord-contact__hero" aria-labelledby="fjord-contact-title">
    <div class="fjord-contact__grid">
      <div class="fjord-contact__image">
        <?php if (!empty($values['imageUrl'])) : ?>
          <img src="<?php echo esc_url($values['imageUrl']); ?>" alt="<?php echo esc_attr($values['imageAlt']); ?>">
        <?php endif; ?>
      </div>
      <div class="fjord-contact__right">
        <div class="fjord-contact__card"<?php echo $style_attr; ?>>
          <h1 id="fjord-contact-title"><?php echo esc_html($values['title']); ?></h1>
          <div class="fjord-contact__form">
            <?php
            if (!empty($values['formShortcode'])) {
              echo do_shortcode($values['formShortcode']);
            }
            ?>
          </div>
        </div>
        <div class="fjord-contact__locations-grid">
          <?php
          $cards = [
            [
              'city' => $values['city1'],
              'address' => $values['address1'],
              'cta' => $values['cta1'],
              'link' => $values['link1'],
              'background' => $values['backgroundColor1'],
              'textColor' => $values['textColor1'],
              'buttonColor' => $values['buttonColor1'],
              'buttonTextColor' => $values['buttonTextColor1'],
            ],
            [
              'city' => $values['city2'],
              'address' => $values['address2'],
              'cta' => $values['cta2'],
              'link' => $values['link2'],
              'background' => $values['backgroundColor2'],
              'textColor' => $values['textColor2'],
              'buttonColor' => $values['buttonColor2'],
              'buttonTextColor' => $values['buttonTextColor2'],
            ],
          ];
          foreach ($cards as $card) :
            if (empty($card['city']) && empty($card['address'])) {
              continue;
            }
            $card_style = sprintf(
              'style="background:%s;color:%s;"',
              esc_attr($card['background']),
              esc_attr($card['textColor'])
            );
            $btn_style = sprintf(
              'style="background:%s;color:%s;"',
              esc_attr($card['buttonColor']),
              esc_attr($card['buttonTextColor'])
            );
          ?>
            <article class="fjord-contact__location-card"<?php echo $card_style; ?>>
              <?php if (!empty($card['city'])) : ?>
                <h2><?php echo esc_html($card['city']); ?></h2>
              <?php endif; ?>
              <?php if (!empty($card['address'])) : ?>
                <p><?php echo nl2br(esc_html($card['address'])); ?></p>
              <?php endif; ?>
              <a class="fjord-contact__direction" href="<?php echo esc_url($card['link']); ?>"<?php echo $btn_style; ?>>
                <?php echo esc_html($card['cta']); ?>
              </a>
            </article>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
  </section>

  <?php
  if (!empty($filtered_blocks)) {
    foreach ($filtered_blocks as $block) {
      echo render_block($block);
    }
  }
  ?>
</main>

<?php get_footer(); ?>
