<?php
/**
 * The template for displaying the gallery post format on archives.
 *
 * @package chained
 * @since Chained 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

?>

<div class="masonry-panel">

	<div class="masonry-panel__content">

		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

			<?php chained_card_categories(); ?>

			<div class="entry-gallery entry-image entry-image--wide">

				<?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
					<span class="sticky-post accent-color"></span>
				<?php endif; ?>

				<?php global $post;
				// Get Selected Images ID's from Post Gallery Meta Box
				$images_ids = get_post_meta( get_the_ID(), 'chained_post_gallery_meta_key', true );

				// Create an array with them
				if (!empty($images_ids)) {
					$images_ids = explode(',',$images_ids);
				}

				// Check if there are any and create query
				if ( ! empty( $images_ids ) )  : ?>
					<?php
					$attachments = get_posts( array(
						'post_type' => 'attachment',
						'posts_per_page' => -1,
						'orderby' => "post__in",
						'post__in' => $images_ids
					) );?>

				<ul class="post_gallery_slider">
					<?php
					// IF however there are images, iterate through them and show them in a list that will become slider
					foreach ( $attachments as $attachment ) :
						$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
						$gallery_image = wp_get_attachment_image_src( $attachment->ID, 'chained-masonry-image' );

						?>
						<li class="post_gallery_item" >
							<a href="<?php echo get_the_permalink();?>">
								<img itemprop="image" class="attachment-medium-size" src="<?php echo $gallery_image[0]; ?>">
							</a>
						</li>
						<?php
					endforeach;
					//Create a variable to see how many images the user has selected - this will be useful later
					$num_of_images = count($attachments); 
					?>
				</ul>
					<?php if ($num_of_images > 1 ) : ?>
						<div class="entry-gallery-navigation">	
							<button id="gallery_prev_button" class="gallery-navigation-button reset">
								<?php echo get_template_part('assets/images/svg/arrow-left');?>
							</button>
							<button id="gallery_next_button" class="gallery-navigation-button reset">
								<?php echo get_template_part('assets/images/svg/arrow-left');?>
							</button>
						</div>
					<?php endif; ?>
				<!-- If there are no images selected, inform the user that he must add them -->
				<?php else : ?>
					<span class="no-images-in-gallery">
						<?php echo esc_html('Please add gallery images!', 'chained'); ?>
					</span>
				<?php endif; ?>

			</div><!-- .entry-gallery -->

			<div class="entry-content-inner">

				<header <?php chained_post_title_class(); ?>>
					<?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>
				</header><!-- .entry-header -->

				<div <?php chained_post_excerpt_class(); ?>>

					<?php if ( get_theme_mod('chained_masonry_blog_excerpt_visibility', true) ) {
						the_excerpt();
					}

					wp_link_pages( array(
						'before' => '<div class="page-links"><span class="pagination-title">' . __( 'Pages:', 'chained' ),
						'after'  => '</span></div>',
						'link_before' => '<span>',
						'link_after'  => '</span>',
						'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'chained' ) . ' </span>%',
						'separator'   => '<span class="screen-reader-text">, </span>',
					) ); ?>

				</div><!-- .entry-content -->

				<div class="entry-meta"><?php chained_card_meta(); ?></div><!-- .entry-meta -->

				<footer class="entry-footer">

					<?php chained_entry_footer(); ?>

				</footer><!-- .entry-footer -->

			</div>
		
		</article><!-- #post-## -->
		
	</div>

</div><!-- .grid__item -->