<?php
/*
Template Name: Bestand
*/

  get_header();

  $categories = get_the_category();
  $args = array(
      'cat'     =>  $categories[0]->cat_ID,
      'orderby' => 'title',
      'order'   => 'ASC',
      'nopaging' => true,
  );
  $query = new WP_Query( $args );
  $available_chars = [];
  if ( $query->have_posts() ) {
    while ( $query->have_posts() ) :
      $query->the_post();
      array_push($available_chars, mb_substr(get_the_title(),0,1,"utf-8"));
    endwhile;
    wp_reset_postdata();
  };

?>

<main>

  <section class="background-color-white category">
    <div class="content-wrap">
      <h2>
        <?php
          if ( ! empty( $categories ) ) {
              echo esc_html( $categories[0]->name );
          }
        ?>
      </h2>

      <?php
        $post = get_page_by_path('bestand');
        $content = apply_filters('the_content', $post->post_content);
        echo $content;
      ?>

      <div class="tx-indexedsearch">

        <div id="c139" class="frame frame-default frame-type-list frame-layout-0">
          <div class="tx-indexedsearch">
            <div class="tx-indexedsearch-searchbox">
              <fieldset>
                <div class="tx-indexedsearch-form">
                  <label for="tx-indexedsearch-searchbox-sword">
                    <?php _e('Ergebnisse filtern', 'kit'); ?>:
                  </label>
                  <div>
                    <ul class="catFilter">
                      <li class='available active'><a href=#>Alle</a></li>
                      <?php
                      $chars = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Ä", "Ü", "Ö");
                      foreach($chars as $item) {
                        if (in_array($item, $available_chars)) {
                          echo "<li class='available'><a href=#>".$item."</a></li>";
                        } else {
                          echo "<li>".$item."</li>";
                        };
                      };
                      ?>
                    </ul>
                  </div>
                </div>
              </fieldset>
            </div>
          </div>
        </div>

      </div>

      <ul class="bestandliste">
        <?php

        $args = array(
            'cat'     =>  $categories[0]->cat_ID,
            'orderby' => 'title',
            'order'   => 'ASC',
            'nopaging' => true,
        );
        $query = new WP_Query( $args );

        if ( $query->have_posts() ) :
          while ( $query->have_posts() ) :

            $query->the_post();
            $first_char = mb_substr( esc_html( get_the_title() ) ,0,1,"utf-8" );

            if ($last_first_char === $first_char) {
              echo '<li>
                <a href="' . esc_url( get_permalink() ) . '">
                  ' . esc_html( get_the_title() ) . '
                </a>
              </li>';
            } else {
              echo '</ul>
              <ul class="bestandliste" id="' . $first_char . '">
                <li>
                  <a href="' . esc_url( get_permalink() ) . '">
                    ' . esc_html( get_the_title() ) . '
                  </a>
                </li>';
              $last_first_char = $first_char;
            };

          endwhile;
          wp_reset_postdata();
        endif;
        ?>
      </ul>

    </div>
  </section>

</main>





<?php

  get_template_part('template-parts/sidebar');
  get_footer();
