<?php
/**
 * Downloads archive page
 *
 */
inoby_enqueue_parted_style("downloads", "post_types");
inoby_enqueue_parted_script("downloads", "post_types");

get_header(); ?>
<main id="primary" class="site-main">

  <?php do_action('post_type_downloads_before_section_downloads')?>
  <section class="downloads-filter">
    <div class="container">
      <div class="row">
        <div class="col-12">
          <div class="downloads-search-bar">
            <form action="/" method="get" autocomplete="off" id="downloads-search">
              <input type="hidden" name="cat" id="cat" onchange="fetch()"></input>
              <div class="switch-buttons-wrap">
                <div class="switch-button active" data-value="0">
                  <img class="image" src="<?= get_theme_file_uri("/assets/icons/file.svg") ?>"
                    alt="<?= __( 'Všetky kategórie', 'inoby' ) ?>">
                  <?php esc_html_e( 'Všetky kategórie', 'inoby' ); ?>
                </div>
                <?php
                $terms = get_terms( array(
                  'taxonomy'   => 'downloads-category', 
                  'hide_empty' => true, 
                ));
                foreach( $terms as $term ) {
                  $thumbs = rwmb_meta( "downloads_category_img", ['object_type' => 'term', "size" => "medium"], $term->term_id );
                  $thumb = reset($thumbs);
                  if (isset($thumb["full_url"]) && !empty($thumb["full_url"])){ ?>
                <div class="switch-button" data-value="<?= $term->term_id ?>"><img class="image"
                    src="<?= $thumb["full_url"] ?>" alt="<?= $term->name ?: "" ?>"><?= $term->name ?></div>
                <?php 
                  } 
                  else{
                    echo '<div class="switch-button" data-value="'. $term->term_id.'">' . $term->name . '</div>';
                  }
                }
              ?>
              </div>
              <input type="text" name="s" placeholder="<?= __('Vyhľadávanie','inoby') ?>" id="keyword"
                class="input-search" onkeyup="fetch()">
            </form>
            <div class="search-result" id="datafetch">
              <?php
                $the_query = new WP_Query( 
                  array( 
                      'posts_per_page' => -1, 
                      'post_type' => array('downloads'),
                  ) 
                );
                if( $the_query->have_posts() ) {
                    echo '<div class="downloads-wrap">';
                    while( $the_query->have_posts() ): 
                      $the_query->the_post(); 
                      get_template_part("post-types/downloads/parts/card");
                    endwhile;
                    echo '</div>';
                  wp_reset_postdata();  
                }
              ?>
            </div>
          </div>

        </div>
      </div>
    </div>
  </section>

</main>

<?php get_footer();