<?php
/**
 * Posts archive page
 *
 */
inoby_enqueue_parted_style("aktuality", "post_types");
inoby_enqueue_parted_script("aktuality", "post_types");

$kategorie = array_reduce(
  get_terms(["taxonomy" => "kategoria"]),
  function ($cary, $item) {
    $cary[$item->term_taxonomy_id] = $item->name;
    return $cary;
  },
  ["0" => "Kategória"],
);

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">
      <div class="row row-head">
        <div class="col-12">
          <h1 class="underlined"><?= post_type_archive_title(null, false) ?? ($active_category->name ?? "") ?></h1>
        </div>
      </div>
    </div>
  </section>

  <section class="filter">
    <div class="container">
      <div class="row">
        <div class="col-12">
          <select name="order" onchange="inoby_post_filter.onOrderChange(event)">
            <?= render_select_options(["asc" => "Najstaršie", "desc" => "Najnovšie"], $_GET["order"] ?? "desc") ?>
          </select>
          <select name="kategoria" onchange="inoby_post_filter.onTaxChange(event)">
            <?= render_select_options($kategorie, $_GET["tax_kategoria"] ?? 0) ?>
          </select>
          <div class="search_bar">
            <input type="search" placeholder="<?= __('Vyhľadať','podoblazom') ?>" name="search"
              value="<?= $_GET["search"] ?? "" ?>" oninput="inoby_post_filter.onSearchChange(event)" />
            <span class="search-icon"></span>
          </div>
        </div>
      </div>
    </div>
  </section>

  <section class="list">
    <div class="container" post-container post-page-size="9" post-order-by="date" post-order-ascdesc="desc">
    </div>
    <div class="no-posts-response">
      <div class="container">
        <div class="row">
          <div class="col-12">
            <h3><?= __('Nenašli sa žiadne aktuality','podoblazom') ?></h3>
          </div>
        </div>
      </div>
    </div>
  </section>

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

<?php get_footer();