<?php

$per_slide = 1;
$infinite = mb_get_block_field("hero_slider_infinite");
$auto = mb_get_block_field("hero_slider_auto");
$arrows = mb_get_block_field("hero_slider_arrows");
$slides = mb_get_block_field("hero_slider_slides");
$num_of_slides = count($slides);

$id = uniqid("hero-slider-wrapper");
$show_arrows = $arrows == 1 && $num_of_slides >= 2 ? " show-arrows" : null;

if ($slides) { ?>
<section <?= inoby_block_attrs($attributes, ["class" => "component-hero-slider{$show_arrows}"]) ?>>
  <div
    <?= inoby_block_attrs($attributes, [
      "class" => "inoby-hero-slider",
      "id" => $id,
      "data-infinite" => $infinite,
      "data-slides" => $per_slide,
      "data-numslides" => $num_of_slides,
      "data-automatic" => $auto,
    ]) ?>>
    <div class="hero-slider">
      <?php foreach ($slides as $slide) {
        $bg_id = $slide["bg"][0] ?? null;
        $bg_url = wp_get_attachment_image_src($bg_id, "h-12")[0] ?? null;

        echo '<div class="hero-slider-wrap" style="background: center/cover no-repeat url(' . $bg_url . ');">';
        echo '<div class="slide-content">';
        if ($slide["heading"] ?? false) {
          echo "<h2>" . $slide["heading"] . "</h2>";
        }
        if ($slide["text"] ?? false) {
          echo "<p>" . $slide["text"] . "</p>";
        }
        foreach ($slide["buttons"] ?? [] as $button) {
          $btn_text = $button["text"] ?? "Text tlačidla";
          $btn_href = $button["url"] ?? "#0";
          $btn_target = empty($button["target"]) ? "_self" : "_blank";
          $btn_class = $button["type"] ?? "button";
          echo '<a href="' . $btn_href . '" target="' . $btn_target . '" class="' . $btn_class . '">' . $btn_text . "</a>";
        }
        echo "</div>";
        echo "</div>";
      } ?>
    </div>
    <?php do_action("component_hero_slider_static"); ?>
  </div>
</section>
<?php } ?>
