<?php
/**
 * Template Name: Kontakt
 */

inoby_enqueue_parted_style("contact", "page_templates");
// inoby_enqueue_parted_script("contact", "page_templates");

get_header();

$contact_informations = rwmb_meta("contact_informations");
$label = "contact_information_label";
$text_group = "contact_information_texts";
$text = "contact_information_text";
$url = "contact_information_url";
$icon = "contact_information_icon";
$divider = "contact_information_divider";
$billing_heading = rwmb_meta("contact_billing_heading");
$billing_information = rwmb_meta("contact_billing_information");
$billing_information_l = "contact_billing_information_label";
$billing_information_v = "contact_billing_information_value";
$google_map = rwmb_meta("google_map");

function get_link_protocol($url) {
  return explode(":", $url)[0] ?? "";
}

function is_local_link($url) {
  $local_protocols = ["mailto", "tel", ""];
  $protocol = get_link_protocol($url);
  return in_array($protocol, $local_protocols);
}

$contact_args = apply_filters("inoby_contact_page_args", [
  "info_classes" => "col-6 col-sm-12 contact-info",
  "form_classes" => "col-6 col-sm-12 contact-form",
  "billing_info_classes" => "col-12 billing-info",
  "map_classes" => "col-12 col-sm-12 contact-map",
]);
?>
<main id="primary" class="site-main">
  <section class="contact">
    <div class="container">
      <div class="row">
        <div class="col-12 contact-hero">
          <h1><?= get_the_title() ?></h1>
        </div>
      </div>
      <div class="row">
        <div class="<?= $contact_args["info_classes"] ?>">
          <?php if (!empty($contact_informations)) {
            foreach ($contact_informations as $contact_information) {
              do_action("inoby_contact_page_info_before");
              if (!empty($contact_information[$label])) {
                echo "<h6>" . $contact_information[$label] . "</h6>";
              }
              echo '<div class="info">';
              if (!empty($contact_information[$icon])) {
                $icon_id = $contact_information[$icon][0];
                echo wp_get_attachment_image($icon_id, "icon");
              }
              echo '<div class="info-text">';
              foreach ($contact_information[$text_group] ?? [] as $info_text) {
                $link_text = $info_text[$text];
                if (!empty($info_text[$url])) {
                  $link_url = $info_text[$url];
                  $target = is_local_link($link_url) ? "" : 'target="_blank"';
                  echo "<a href=\"$link_url\" $target>$link_text</a>";
                } else {
                  echo "<p>$link_text</p>";
                }
              }
              echo "</div></div>";
              do_action("inoby_contact_page_info_after");
              $contact_divider = $contact_information[$divider] ?? '';
              if ($contact_divider == 1) {
                echo "<hr>";
              }
            }
          } ?>
        </div>
        <div class="<?= $contact_args["form_classes"] ?>">
          <?php do_action("inoby_contact_page_form_before"); ?>
          <?php inoby_contact_form(["textarea-rows" => 3]); ?>
        </div>
      </div>
      <div class="row">
        <div class="<?= $contact_args["billing_info_classes"] ?>">
          <?php
          if (!empty($billing_heading)) {
            echo "<h2>" . $billing_heading . "</h2>";
          }
          if (!empty($billing_information)) {
            foreach ($billing_information as $billing_info) {
              if (!empty($billing_info["contact_billing_information_label"])) {
                echo '<div class="billing-item">';
                echo "<h6>" . $billing_info[$billing_information_l] . "</h6>";
                echo "<span>" . $billing_info[$billing_information_v] . "</span>";
                echo "</div>";
              }
            }
          }
          ?>
        </div>
      </div>
      <div class="row">
        <?php if (!empty($google_map)) {
          echo '<div class="' . $contact_args["map_classes"] . '">';
          echo '<iframe src="' . $google_map . '" width="100%" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>';
          echo "</div>";
        } ?>
      </div>
    </div>
  </section>
</main>

<?php get_footer();
