<?php

$headline = mb_get_block_field("posts_selection_headline");
$posts_columns = mb_get_block_field("posts_selection_columns");
$posts_slider = mb_get_block_field("posts_selection_slider_check");
$per_slide = mb_get_block_field("posts_selection_slider_posts");
$spacing = mb_get_block_field("posts_selection_slider_spacing") ?: 30;
$infinite = mb_get_block_field("posts_selection_slider_infinite");
$auto = mb_get_block_field("posts_selection_slider_auto");
$arrows = mb_get_block_field("posts_selection_slider_arrows");
$btn = mb_get_block_field("posts_selection_btn");
$url = mb_get_block_field("posts_selection_url");
$posts = mb_get_block_field("posts_selection");

$slider_id = uniqid("posts-slider-wrapper");

if ($posts) { ?>
<section <?= inoby_block_attrs($attributes, ["class" => "inoby-posts-selection"]) ?>>
  <div class="posts-wrap">
    <div class="container">
      <div class="row">
        <div class="col-12">
          <?php if ($headline) {
            echo '<h2 class="heading">' . $headline . "</h2>";
          } ?>
        </div>
      </div>
      <div class="row">
        <?php if ($posts_slider == 1) { ?>
        <div class="col-12">
          <div
            <?= inoby_block_attrs($attributes, [
              "class" => "inoby-posts-slider",
              "id" => $slider_id,
              "data-infinite" => $infinite,
              "data-slides" => $per_slide,
              "data-spacing" => $spacing,
              "data-automatic" => $auto,
            ]) ?>>
            <div class="posts-slider">
              <div class="posts-slider-wrap">
                <?php foreach ($posts as $post) {
                  inoby_setup_postdata($post["post"]);
                  get_template_part("post-types/post/parts/card", null, ["post" => $post["post"]]);
                  inoby_reset_post_data();
                } ?>
              </div>
              <div class="slider-actions">
                <?php
                if (isset($arrows) && $arrows) {
                  get_template_part("template-parts/keen-arrows", "keen-arrows", ["slider-id" => $slider_id]);
                }
                if ($url) {
                  echo '<a class="button" href="' . $url . '">' . $btn . "</a>";
                }
                ?>
              </div>
            </div>
          </div>
        </div>
        <?php } else {foreach ($posts as $post) { ?>
        <div class="<?= $posts_columns ?? "col-4" ?> col-md-6 col-sm-12">
          <?php
          inoby_setup_postdata($post["post"]);
          get_template_part("post-types/post/parts/card", null, ["post" => $post["post"]]);
          inoby_reset_post_data();
          ?>
        </div>
        <?php }
          if ($url) {
            echo '<div class="col-12 button-wrap"><a class="button" href="' . $url . '">' . $btn . "</a></div>";
          }} ?>
      </div>
    </div>
  </div>
</section>
<?php } ?>
