<?php
// pri registrácii taxonómií nezabudnúť na "show_in_rest" => true
$finticky = array_reduce(
  get_terms(["taxonomy" => "finticka"]),
  function ($cary, $item) {
    $cary[$item->term_taxonomy_id] = $item->name;
    return $cary;
  },
  ["0" => "All"],
);
$picovniky = array_reduce(
  get_terms(["taxonomy" => "picovinka"]),
  function ($cary, $item) {
    $cary[$item->term_taxonomy_id] = $item->name;
    return $cary;
  },
  ["0" => "All"],
);

function render_select_options($options, $selected) {
  $html = "";
  foreach ($options as $value => $label) {
    if ($selected == null) {
      $selected = $value;
    }
    $sel = $selected == $value ? "selected" : "";
    $html .= "<option value=\"{$value}\" {$sel}>{$label}</option>";
  }
  return $html;
}

get_header();
?>
<main id="primary" class="site-main">
  <section class="header">
    <div class="container">
      <?php do_action("blog_archive_header_before_heading"); ?>
      <h1><?= post_type_archive_title(null, false) ?? ($active_category->name ?? "") ?></h1>
      <?php if (Inoby_Config::latest_posts() > 0) {
        // get_template_part("post-types/post/parts/featured-posts-list");
      } ?>
    </div>
  </section>

  <section class="filter">
    <div class="container">
      <input type="search" placeholder="search" name="search" value="<?= $_GET["search"] ?? "" ?>"
        onblur="inoby_post_filter.onSearchChange(event)" oninput="inoby_post_filter.onSearchChange(event)" />
      <select name="finticka" onchange="inoby_post_filter.onTaxChange(event)">
        <?= render_select_options($finticky, $_GET["tax_finticka"] ?? 0) ?>
      </select>
      <select name="picovinka" onchange="inoby_post_filter.onTaxChange(event)">
        <?= render_select_options($picovniky, $_GET["tax_picovinka"] ?? 0) ?>
      </select>
      <select name="order_by" onchange="inoby_post_filter.onOrderByChange(event)">
        <?= render_select_options(["date" => "Datum", "title" => "Nazov", "modified" => "Zmena"], $_GET["orderby"] ?? "date") ?>
      </select>
      <select name="order" onchange="inoby_post_filter.onOrderChange(event)">
        <?= render_select_options(["asc" => "Vzostupne", "desc" => "Zostupne"], $_GET["order"] ?? "asc") ?>
      </select>
      <p>Pocet vysledkov<span post-total-count></span></p>
      <p>Stranka<span post-current-page>1</span> / <span post-total-pages>1</span></p>
    </div>
  </section>

  <section class="list">
    <div class="container" post-container post-page-size="7" post-order-by="title" post-order-ascdesc="desc">
    </div>
  </section>

  <section class="pagination">
    <div class="container">
      <?php get_template_part("template-parts/archive/pagination"); ?>
    </div>
  </section>
</main>

<?php get_footer();