<?php
/**
 * The template for displaying all series
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package WordPress
 * @subpackage IZI_THEME
 * @since 1.0.0
 */

get_header();
$cat = get_term_by('name', single_cat_title('',false), 'genre'); 
$catslug = $cat->slug; 
?>
<!-- /* INTERNAL PAGE HEADER IMG -->
<div class="container-fluid p-0 position-relative pagehead">
	<div class="filterdiv internalpage position-absolute top-0 start-0"></div>
	<?php
	if (has_post_thumbnail()) {
		the_post_thumbnail('full', ['class' => 'img-fluid w-100']); 
	} else {
		echo '<img src="' . get_template_directory_uri() . '/assets/img/series/affiche/defaut.jpg" class="img-fluid w-100" alt="Actu Séries" loading="lazy">';
	}
	?>
</div>
<!-- IMG END -->

	<!-- PAGE CONTENT -->
	<section class="container mt-5">
		<h1 class="htitles text-center my-5">Séries <?php echo ucfirst($catslug); ?></h1>
		<?php // Get total number of posts in Taxonomy CPT
		$argtax = array(
			'post_type' => 'series',
			'post_status' => 'publish',
			'genre' => $catslug,
			'numberposts' => -1
			);
		echo '<p>' . count( get_posts( $argtax ) ) . ' séries disponibles.<p>';
		?>

		<div class="row">
			<div class="col-12 mb-4">

				<?php
				$terms = get_terms( array(
					'taxonomy'          => 'genre',
					'order'   			=> 'ASC',
					'hide_empty' 		=> true
				));

				if (!empty($terms) && !is_wp_error($terms)) {
					echo '<a href="' . site_url('/series/') . '" class="btn btn-outline-secondary me-2" role="button" aria-pressed="true">Tout</a>';

					foreach ($terms as $term) {
						echo '<a href="' . esc_url(get_term_link($term)) . '" class="btn btn-outline-secondary me-2" role="button" aria-pressed="true">' . $term->name . '</a>';
					}
				}
				?>

			</div>
		</div>

		<div class="row text-center">
			<?php
			$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
			$argsitem = array(
				'post_type'			=> 'series',
				'posts_per_page' 	=> 8,
				'post_status'		=> 'publish',
				'paged' 			=> max( 1, $paged ),
				'tax_query' => array(
					array(
						'taxonomy' => 'genre',
						'field'    => 'slug',
						'terms'    => $catslug,
					),
				),
			);
			$allitem = new WP_Query($argsitem);

			if ($allitem->have_posts()) : while ($allitem->have_posts()) : $allitem->the_post();
			?>
			<div class="col-12 col-sm-6 col-lg-3 mb-4">
				<div class="card border-0 img-zoom">
					<a class="text-decoration-none" href="<?php the_permalink(); ?>">
					<?php
					if (has_post_thumbnail()) {
						the_post_thumbnail('full', ['class' => 'img-fluid w-100']); 
					} else {
						echo '<img src="' . get_template_directory_uri() . '/assets/img/series/affiche/defaut.jpg" class="img-fluid w-100" alt="Actu Séries" loading="lazy">';
					}
					?>
						<h2><?php the_title(); ?></h2>
					</a>
				</div>
			</div>
			<?php endwhile;
			else : ?>
				<div class="article-content">
					<p>Aucun contenu à afficher</p>
				</div>
			<?php
			endif;
			// Restore original post data.
			wp_reset_postdata();
			?>
		</div>

		<div class="row justify-content-center my-4">
			<nav class="paginate d-flex justify-content-center">
				<?php
				pagination_bar($allitem);
				?>
			</nav>
		</div>

		<div class="row justify-content-center my-4">
			<nav class="col-6" aria-label="Page navigation">
				<ul class="nav justify-content-center pagination">
					<li class="page-item">
						<a class="page-link" href="#" aria-label="Previous">
							<span aria-hidden="true">&laquo;</span>
						</a>
					</li>
					<li class="page-item"><a class="page-link" href="#">1</a></li>
					<li class="page-item"><a class="page-link" href="#">2</a></li>
					<li class="page-item"><a class="page-link" href="#">3</a></li>
					<li class="page-item">
						<a class="page-link" href="#" aria-label="Next">
							<span aria-hidden="true">&raquo;</span>
						</a>
					</li>
				</ul>
			</nav>
		</div>
	</section>
	<!-- PAGE CONTENT END -->
	
<?php
get_template_part('template-parts/content-videobg');
get_template_part('template-parts/content-prefooter');
get_footer();