<?php
$cta_grid = mb_get_block_field("cta-grid");
$cta_grid_inner = mb_get_block_field("cta-grid-inner");

if ($cta_grid) { ?>
<section <?= inoby_block_attrs($attributes, ["class" => "cta-grid"]) ?>>
  <div class="container">
    <div class="row">
      <div class="col-12">
        <?php if ($cta_grid_inner == 1) {
            echo '<div class="cta-grid-wrap-inner">';

            foreach ($cta_grid as $cta) {
              $cta_url = $cta["url"] ?? "";
              $bg_url = "";

              if (!empty($cta["bg"]) && is_array($cta["bg"])) {
                $bg_data = wp_get_attachment_image_src(reset($cta["bg"]), "s-6");
                $bg_url = $bg_data ? $bg_data[0] : "";
              }

              if ($bg_url) {
                echo '<a href="' . esc_url($cta_url) . '" class="cta-section" style="background: center/cover no-repeat url(' . esc_url($bg_url) . ');">';
              } else {
                echo '<a href="' . esc_url($cta_url) . '" class="cta-section cta-section-placeholder">';
              }

              if (!empty($cta["headline"])) {
                echo "<h3>" . esc_html($cta["headline"]) . "</h3>";
              }
              if (!empty($cta["text"])) {
                echo "<p>" . esc_html($cta["text"]) . "</p>";
              }
              echo "</a>";
            }
            echo "</div>";
          } else {
            echo '<div class="cta-grid-wrap">';
            foreach ($cta_grid as $cta) {
              $cta_headline = $cta["headline"] ?? "";
              $cta_text = $cta["text"] ?? "";
              $cta_url = $cta["url"] ?? "";

              echo '<div class="cta-grid-box">';

              $img_src = "";
              if (!empty($cta["bg"]) && is_array($cta["bg"])) {
                $img_data = wp_get_attachment_image_src(reset($cta["bg"]), "s-6");
                $img_src = $img_data ? $img_data[0] : "";
              }

              if ($img_src) {
                echo '<div class="img-wrap">';
                echo '<img src="' . esc_url($img_src) . '" alt="' . esc_attr($cta_headline) . '">';
                echo "</div>";
              }

              echo '<div class="content-wrap">';
              if (!empty($cta_headline)) {
                echo "<h5>" . esc_html($cta_headline) . "</h5>";
              }

              /**
               * Action to render stuff after cta grid headline
               * @package inobydoc
               */
              do_action("inoby_cta_grid_headline_after", $cta);

              if (!empty($cta_text)) {
                echo "<p>" . esc_html($cta_text) . "</p>";
              }

              if (!empty($cta_url)) {
                echo '<a class="button" href="' . esc_url($cta_url) . '">' . __("Zisti viac", "inoby") . "</a>";
              }

              echo "</div>"; // .content-wrap
              echo "</div>"; // .cta-grid-box
            }
            echo "</div>"; // .cta-grid-wrap
          } ?>
      </div>
    </div>
  </div>
</section>
<?php }
?>