<?php

/**
 * Single post template file.
 *
 * @subpackage TheHoot
 * */

get_header(); 

?>

  <div class="col-12 col-lg-8 pe-lg-5">

    <?php

  // Get primary (Yoast) term if it is set
  $category_display = '';
  $category_slug = '';
  $category = get_the_category();

  if ( class_exists('WPSEO_Primary_Term') ) {

       // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
  	$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
  	$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
  	$term = get_term( $wpseo_primary_term );

       if ( is_wp_error( $term ) ) {

            // Default to first category (not Yoast) if an error is returned
            $category_display = $category[0]->name;
            $category_slug = $category[0]->slug;

       } else {

            // Set variables for category_display & category_slug based on Primary Yoast Term
            $category_id = $term->term_id;
            $category_term = get_category($category_id);
            $category_display = $term->name;
            $category_slug = $term->slug;

       }

  } else {

       // Default, display the first category in WP's list of assigned categories
       $category_display = $category[0]->name;
       $category_slug = $category[0]->slug;

  }

  if (have_posts()) {
      while (have_posts()) {

          the_post();
          get_template_part('template-parts/content', get_post_type());
      }
  }
  ?>

      <div class="row mt-5 more-posts">

              <?php
              $query = new WP_Query( array(
              'post__not_in' => array($id),
              'posts_per_page' => 1,
              'category_name' => $category_slug
            ) );

              while ( $query->have_posts() ) : $query->the_post();

              $excludeID[] = $post->ID;?>

          <div class="col-12" data-aos="fade-up"  data-aos-delay="300">

            <?php if ($category_display): ?>

              <p class="category"><?php echo $category_display; ?></p>

              <h2 class="h1"><?php the_title(); ?></h2>              

                <?php  $author_id  = get_post_field( 'post_author', get_the_ID() ); ?>

                  <a class="mb-4" href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>">
                    <img src="<?php echo get_avatar_url($author_id); ?>" width="40" height="40" class="avatar rounded-circle" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />
                    <span class="ml-2"><?php the_author(); ?></span>
                  </a>

              <?php the_post_thumbnail('full', array( 'class' => 'img-fluid w-100 featured' )) ?>

              <?php echo html_entity_decode(get_the_content()); ?>


            <?php endif; ?>

            </div>

          <?php endwhile; ?>

            <?php  wp_reset_postdata();	?>

          </div>

          <div class="row mt-5 more-posts">

              <?php
              $query = new WP_Query( array(
              'post__not_in' => array($id),
              'offset' => 1,
              'posts_per_page' => 1,
              'category_name' => $category_slug
            ) );

              while ( $query->have_posts() ) : $query->the_post();

              $excludeID[] = $post->ID;?>

          <div class="col-12" data-aos="fade-up"  data-aos-delay="300">

            <?php if ($category_display): ?>

              <p class="category"><?php echo $category_display; ?></p>

            <?php endif; ?>

              <h2 class="h1"><?php the_title(); ?></h1>

                <?php  $author_id  = get_post_field( 'post_author', get_the_ID() ); ?>

                  <a class="mb-4" href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>">
                    <img src="<?php echo get_avatar_url($author_id); ?>" width="40" height="40" class="avatar rounded-circle" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />
                    <span class="ml-2"><?php the_author(); ?></span>
                  </a>

                 <?php echo html_entity_decode(get_the_content()); ?>

            </div>

          <?php endwhile; ?>

            <?php
wp_reset_postdata();	?>

      </div>
  </div>

  <div class="col-12 col-lg-4 sidebar d-flex flex-column">
    <div class="" style="position: sticky;top: 5%;">

    <h2 class="h3 mt-2 mb-4">More <?php echo $category_display ?></h2>

    <section>

      <?php

    							$query = new WP_Query( array(
                  'post__not_in' => array($id),
                  'posts_per_page' => 3,
                  'offset' => 2,
                  'category_name' => $category_slug
    								) );
                    $count = 0;
    							while ( $query->have_posts() ) : $query->the_post();

                  $excludeID[] = $post->ID;
                  $permalink = get_the_permalink();
                  $title = get_the_title();
    							?>

        <div class="col-12 p-0 mb-4">

          <?php 	if ( $count == 0 ) { ?>

            <div class="more-wrap post-<?php echo $count; ?>">
              <a class="col-12 p-0 mb-4 no-transition-link h-100" href="<?php echo $permalink; ?>">
                <div class="row">
                  <div class="col-12 image relative mb-4">

                    <?php
                                if ( has_post_thumbnail() ) {
                                the_post_thumbnail('medium', array( 'class' => 'img-fluid w-100 featured' ));
                                  }
                                  else {
                                  echo '<img class="img-fluid w-100" src="' . get_bloginfo( 'stylesheet_directory' )
                                    . '/src/assets/images/default-image.jpg" />';
                                  } ?>

                  </div>
                  <div class="col-8 absolute roundel grow">
                    <p>
                      <?php
                          $category = get_the_category();
                          echo $category[0]->cat_name;
                          ?>
                    </p>
                    <h3 class="h5">
                              <?php echo $title; ?>
                            </h3>
                  </div>
                </div>
              </a>
            </div>

            <?php	}

                      else { ?>
              <div class="more-wrap post-<?php echo $count; ?>">
                <a class="mb-4" href="<?php echo $permalink; ?>">
                  <div class="row">
                    <div class="col-4 image p-0 <?php	if ( $count == 2 ) { echo 'order-5'; }?>">

                      <?php
                              if ( has_post_thumbnail() ) {
                              the_post_thumbnail('medium', array( 'class' => 'img-fluid w-100' ));
                                }
                                else {
                                echo '<img class="img-fluid w-100" src="' . get_bloginfo( 'stylesheet_directory' )
                                  . '/src/assets/images/default-image.jpg" />';
                                } ?>

                    </div>
                    <div class="col-8 m-auto">
                        <p class="mb-0">
                        <?php
                        $category = get_the_category();
                        echo $category[0]->cat_name;
                        ?>
                      </p>
                      <h3 class="h5">
                            <?php echo $title; ?>
                          </h3>
                    </div>
                  </div>
                </a>
              </div>

              <?php } ?>

        </div>

        <?php

              $count++ ?>

          <?php endwhile;

                  wp_reset_postdata();

									?>

    </section>

  <?php get_template_part('/template-parts/audio-video'); ?>

  </div>
    </div>

  <?php get_footer(); ?>
