<?php 
global $post; 
global $wp_query;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = get_option( 'posts_per_page' );
$offset = $paged * $posts_per_page;

//get all taxonomies associated with this post type 
$taxonomies; 

//get taxonomies available from post type
$taxonomy_objects = get_object_taxonomies( get_post_type(), 'objects' );
$taxonomies = array();

//get all possible taxonomies, loop through into array
foreach($taxonomy_objects as $taxonomy_object) {
	array_push($taxonomies, $taxonomy_object); 
} 
//find out which taxonomies are active --------------------
$activeFilters = array(); 

//on initial page load, find out queried taxonomies
foreach ($taxonomies as $taxonomy): 

		//check if this taxonomy appears in the URL or has a selected option in the drop down 
		$taxonomyArray = array($taxonomy->name, array());
		$termsSelected = false;

		$terms = get_terms( $taxonomy->name, array(
			'hide_empty' => false,
		) );

		foreach($terms as $term) {
			if (in_array($term->slug, $wp_query->query_vars, true)) {
				array_push($taxonomyArray[1], $term->slug); 
				$termsSelected = true;
			}
		}
		//only push to array if has terms
		if ($termsSelected) {
			array_push($activeFilters, $taxonomyArray); 
		}
		//if term is currently active, add to array 
		//base selected on either current URL terms or current query object
endforeach; 
//make a json object so it can be passed to the js
$jsonFilters = json_encode(  $activeFilters  );

?>
<div class="container">
	<div class="indicator" data-offset="<?php echo $offset; ?>" data-ppp="<?php echo $posts_per_page?>"  data-post-type="<?php echo get_post_type(); ?>" data-taxonomy='<?php echo $jsonFilters; ?>'>
	</div>


	<div class="archive-filters">

		<?php 
		// foreach taxonomy, display a filter tool
		foreach ($taxonomies as $taxonomy): 			
			//get all terms 
			$terms = get_terms( $taxonomy->name, array(
				'hide_empty' => true,
			) );
			$activestate = " "; ?>

			<?php if ($terms): ?>
				<!-- <h3><?php echo $taxonomy->labels->name; ?></h3> -->

				<?php if (get_post_type() == 'media'): ?>


					<div class="button-filters hide-on-small-only">
						<?php if (is_tax()): ?>
							<a class="filter btn btn-outline-gray" data-taxonomy-name="<?php echo $taxonomy->name; ?>" href="<?php echo get_post_type_archive_link(get_post_type()); ?>"><span>All</span></a>
						<?php endif; ?>

						<?php foreach($terms as $term) {
							// ensure filter is set to current active term
								if (in_array($term->slug, $wp_query->query_vars, true)) {
									$activestate = 'active-btn';
								} else {
									$activestate = " ";
								}
							if ($category->name != 'Uncategorized') {
								echo '<a href="' . get_term_link($term) . '" class="btn btn-outline-gray filter   ' . $activestate . '"   data-taxonomy-name="' . $term->taxonomy . '" data-post-type="' . get_post_type() . '" data-term-name="' . $term->slug .'"><span>' . $term->name . "</span></a>";
							}
						} 
						?>
					</div>

					<div class="select-filter hide-on-med-and-up">
						<!-- <span class="filter-label hide-on-small-only"><?php echo $taxonomy->labels->name?>:</span> -->

						<select class="filter-select" data-taxonomy-name="<?php echo $taxonomy->name; ?>" onchange="if (this.value) window.location.href=this.value">
							<option class="filter" <?php if (!is_category() && !is_tax()){ echo '"selected"'; } ?> data-post-type="<?php echo get_post_type(); ?>" value="<?php echo get_post_type_archive_link( get_post_type() ); ?>">All Categories </option>

							<?php foreach($terms as $term) {
								// ensure filter is set to current active term
								if (in_array($term->slug, $wp_query->query_vars, true)) {
									$activestate = 'selected';
								} else {
									$activestate = " ";
								}
								if ($category->name != 'Uncategorized') {
									echo '<option class="filter "  ' . $activestate . '  data-taxonomy-name="' . $term->taxonomy . '" data-post-type="' . get_post_type() . '" value="' . get_term_link($term) .'">' . $term->name . "</option>";
								}
							} 
							?>
						</select>

				<?php else: ?>
					<div class="select-filter">
						<!-- <span class="filter-label hide-on-small-only"><?php echo $taxonomy->labels->name?>:</span> -->

						<select class="filter-select" data-taxonomy-name="<?php echo $taxonomy->name; ?>" onchange="if (this.value) window.location.href=this.value">
							<option class="filter" <?php if (!is_category() && !is_tax()){ echo '"selected"'; } ?> data-post-type="<?php echo get_post_type(); ?>" value="<?php echo get_post_type_archive_link( get_post_type() ); ?>">All Categories </option>

							<?php foreach($terms as $term) {
								// ensure filter is set to current active term
								if (in_array($term->slug, $wp_query->query_vars, true)) {
									$activestate = 'selected';
								} else {
									$activestate = " ";
								}
								if ($category->name != 'Uncategorized') {
									echo '<option class="filter "  ' . $activestate . '  data-taxonomy-name="' . $term->taxonomy . '" data-post-type="' . get_post_type() . '" value="' . get_term_link($term) .'">' . $term->name . "</option>";
								}
							} 
							?>
						</select>
					<?php endif; ?>
				</div>
			<?php endif; ?>
		<?php endforeach; ?>
	</div>
</div>


<!-- news slider -->


<?php if (get_post_type() == 'news' AND !is_tax() AND !is_paged()): ?>

<?php 

$args = array(
    'post_type' => 'news',
	'posts_per_page'	=> 5,
);
$featuredQuery = new WP_Query( $args );

?>

<?php if ($featuredQuery->have_posts() && !is_paged() ): ?>
	<section class="featured-blog-posts">
		<div class="container">
			<div class="featured-article-wrapper nospace-mobile">
			<div class="featured-article-slider">
				<?php
				while ( $featuredQuery->have_posts() ) {
				    $featuredQuery->the_post();
				    $thumbnail = get_the_post_thumbnail_url(); 
				    $author = get_the_author();

					$user_id =  get_post_field( 'post_author' );

					//query for info of posts change to suit below
					$userDetail = new WP_Query(array(
					    'post_type' => 'fit-teammember' , 
					    'meta_key' => 'linked_user', 
					    'meta_value' => $user_id ,
					    'posts_per_page' => 1)
					  );

				    ?>

				    <div class="featured-article-slide">
				    	<div class="featured-article-slide-inner clearfix">
					    	<div class="feature-image" style="background-image:url(<?php echo $thumbnail; ?>);">
					    	</div>
					    	<div class="featured-article-inner">
							<span class="date"><?php echo get_the_date(); ?></span>
					    		<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
					    		<?php echo get_the_category_list(); ?>

							<?php get_template_part('partials/template', 'post-meta'); ?>
					    	
							</div>
				    	</div>
				    </div>

				<?php } //endwhile; ?>
			</div>
			<div class="feature-article-slider-nav"> 
				<div class="slider-nav">
					<div class="control-group-pagination">
						<a class="control control-prev inverse" data-custom-slick-prev="#news"><i class="icon-arrow-thin-left"></i><span>Prev</span></a><a  class="control control-next inverse" data-custom-slick-next="#news"><span>Next</span><i class="icon-arrow-thin-right"></i></a>
					</div>
				</div>
			</div>
		</div>
		</div>
	</section>
<?php endif; ?>


<?php endif; ?>


<section class="page-section archive-content">
	<div class="section-inner">
		<div class="container">
					<div class="posts-grid">
						<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
							<?php get_template_part('partials/template', 'post-card'); ?>
						<?php endwhile; else : ?>
							<p><?php _e( 'Sorry, nothing was found.' ); ?></p>
						<?php endif; ?>
					</div>
					<div class="pagination">
						<?php m2_pagination(); ?>
					</div> 
				</div>
		</div>
	</section>
</section>