<?php
/**
 * Template Name: Home
 *
 * Page template with wide content area and without the secondary sidebar .
 *
 */

 get_header() ?>

  <?php the_post() ?>

    <?php
      $post_id = get_the_ID();
      $post_type = get_post_type($post_id);
      $post_meta = sandbox_get_post_meta($post_id);

      $section_classes = array();
      $excerpt = '';
      $subheadline = isset($post_meta['_subheadline']) ? apply_filters('the_content', $post_meta['_subheadline']) : '';

      // post thumbnail as background image
      $post_style = '';
      if ( has_post_thumbnail() ) {
        $section_classes[] = 'has-thumbnail';
        $post_style = ' style="background-image:url(' . get_the_post_thumbnail_url($post_id, 'sandbox-hero') . ');"';
      }

      // Post type "page"
      if ( $post_type === 'page' ) {
        $content_meta_args = false;
      }

      if ( empty($excerpt) ) {
        $excerpt = apply_filters('the_content', has_excerpt() ? get_the_excerpt() : get_the_content());
      }
    ?>
  <section id="content" class="articles wrapper container <?php echo implode(' ', $section_classes); ?>">
    <header id="post-<?php the_ID(); ?>" <?php post_class('section-header'); ?><?php echo $post_style; ?>>
      <div class="inner">
        <h1 class="title"><?php the_title() ?></h1>
      <?php if ( !empty($subheadline) ) { ?>
        <div class="subheadline">
          <?php echo $subheadline; ?>
        </div>
      <?php } ?>
      <?php if ( !empty($excerpt) ) { ?>
        <div class="intro">
          <?php echo $excerpt; ?>
        </div>
      <?php } ?>
      </div>
    </header>
    <div class="inner">
      <?php
        /**
         * Custom teaser loop
         * ==================
         * Needs a post type "teaser"
         * Looks for featured images
         * ------------------------
         * Fill out your post query
         *
         */
        $startpagePagesOptions = array(
          'post_type' => 'any',
          'teaser' => 'in-3-schritten-zum-pool',
          'posts_per_page' => 3,
          'orderby' => 'menu_order',
          'order' => 'ASC',
        );
        $customQuery = new WP_Query($startpagePagesOptions);
        // include(locate_template('loop-teaser-linked.php'));
      ?>
    </div>
	</section>
  <?php
    /**
      * Product post type / Type tax / Products
      * =======================================
      */
    $product_post_type_name = 'produkt';
    $product_post_type_object = get_post_type_object($product_post_type_name);
  ?>
<?php if ( $product_post_type_object && $product_post_type_object->has_archive ) { ?>
  <?php
    $post_type_ID = esc_attr($product_post_type_name);
    $post_type_url = esc_url(get_post_type_archive_link($product_post_type_name));
    $post_type_labels = apply_filters('post_type_labels_' . $product_post_type_name, $product_post_type_object->labels);
    $post_type_label = apply_filters('the_title', $product_post_type_object->label);
    $moreText = 'View all ' . $post_type_labels->name;
    if ( $product_post_type_name === 'download_aktion_de' ) {
      $moreText = __('To', 'sandbox') . ' ' . $post_type_labels->name;
    }
    $post_type_description = apply_filters('the_content', apply_filters('get_the_post_type_description', $product_post_type_object->description, $product_post_type_object));
    $post_type_classes = array(
      $product_post_type_name,
      'type-' . $product_post_type_name,
      'status-publish',
      'has-post-thumbnail',
      'hentry',
      'has-thumbnail',
    );
    $post_type_image = esc_url(get_template_directory_uri() . '/img/' . esc_attr($product_post_type_name) . '.jpg');
    $post_type_style = ' style="background-image:url('. esc_url(get_stylesheet_directory_uri()) . '/img/logo-windpool.svg), url(' . $post_type_image . ');"';

    /**
     * types - Produkttypen
     * ====================
     * mit Top 4 Artikel je Bereich sortiert nach Reihenfolge
     */
    $types_taxonomy = get_taxonomy('typ');
    $types_options = array(
      'taxonomy' => $types_taxonomy->name,
      'post_type' => $post_type,
      'orderby' => 'term_order',
      'order' => 'ASC',
    );
    $types = new WP_Term_Query($types_options);
    $types_terms = $types->get_terms();
  ?>
  <section id="<?php echo $post_type_ID; ?>" class="<?php echo esc_attr(implode(' ', $post_type_classes)); ?>">
    <header class="section-header"<?php echo $post_type_style; ?>>
      <div class="inner">
        <h2 class="title"><a href="<?php echo $post_type_url; ?>?mtb=h-l-<?php echo $post_type_ID; ?>"><?php echo $post_type_label; ?></a></h2>
      <?php if ( !empty($post_type_description) ) { ?>
        <div class="intro">
          <?php echo $post_type_description; ?>
        <?php if ( !empty( $types->terms ) ) { ?>
          <ul class="terms-list taxonomy-<?php echo $types_taxonomy->name; ?>">
          <?php foreach( $types_terms as $type ) { ?>
            <?php
              $type_url = esc_url(get_term_link($type));
              $type_classes = array(
                'more',
                $type->taxonomy,
                $type->taxonomy . '-' . $type->slug,
                $type->slug
              );
              $type_meta = get_term_meta($type->term_id);
              $type_image_id = isset($type_meta['_term_image']) && !empty($type_meta['_term_image'][0]) ? intval($type_meta['_term_image'][0]) : 0;
              // post thumbnail as background image
              $type_style = '';
              if ( $type_image_id !== 0 ) {
                $type_classes[] = 'has-post-thumbnail';
                $type_image_src = wp_get_attachment_image_src($type_image_id, 'sandbox-default');
                $type_style = ' style="background-image:url(' . $type_image_src[0] . ');"';
              }
              $type_description = isset($type_meta['_term_short_description']) && !empty($type_meta['_term_short_description'][0]) ? $type_meta['_term_short_description'][0] : wp_trim_words($type->description, 40);
            ?>
            <li class="<?php echo implode(' ', $type_classes); ?>"><a href="<?php echo $type_url; ?>" rel="bookmark"><?php echo apply_filters('the_title', $type->name); ?></a></li>
          <?php } ?>
            <li class="more"><a href="<?php echo $post_type_url; ?>?mtb=h-b-<?php echo $post_type_ID; ?>"><?php printf( $moreText ); ?></a></li>
          </ul>
        <?php } ?>
        </div>
      <?php } ?>
      </div>
    </header>
    <div class="inner">
    <?php if ( !empty( $types->terms ) ) { ?>
      <?php foreach( $types_terms as $type ) { ?>
        <?php
          $type_url = esc_url(get_term_link($type));
          $type_posts_limit = 4;
          $type_posts_options = array(
            'tax_query' => array(array(
              'taxonomy' => $type->taxonomy,
              'field' => 'slug',
              'terms' => $type->slug
            )),
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'posts_per_page' => $type_posts_limit,
          );
        ?>
        <?php $customQuery = new WP_Query($type_posts_options); ?>
        <?php if ( $customQuery->have_posts() ) { ?>
      <div class="products articles <?php echo $type->taxonomy; ?>-<?php echo $type->slug; ?>">
        <h3 class="title"><?php echo __('Top', 'sandbox'), ' ', $customQuery->post_count, ' ', $type->name; ?></h3>
        <div class="inner">
          <?php // include(locate_template('loop-archive.php')); ?>
        </div>
        <div class="content">
          <p class="more">
            <a href="<?php echo $type_url; ?>" rel="bookmark"><?php echo __('All', 'sandbox'); ?> <?php echo apply_filters('the_title', $type->name); ?></a>
          </p>
        </div>
      </div>
        <?php } ?>
      <?php } ?>
    <?php } ?>
    </div>
  </section>
<?php }  ?>

<?php get_footer() ?>
