
<?php
/**
 * Template part for displaying challenges in template-challenges.php.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package bottleup
 */

?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<h2><img class="img-responsive" src="<?php echo get_template_directory_uri ()."/images/bottleup-challenges.png" ?>" alt="BottleUp Challenges"></h2>
		<?php if (is_page('challenges') && (current_user_can( 'manage_options' ))) : ?>
			<button id="add-challenge" type="button" class="btn" data-toggle="modal" data-target="#addChallengeModal">Add Challenge</button>
		<?php endif; ?>
	</header><!-- .entry-header -->

	<div class="entry-content">
		<?php the_content(); ?>
		<?php
		$args = array(
			'post_type'      => 'challenge',
			'order'          => 'ASC',
			'orderby'        => 'menu_order',
			'posts_per_page' => -1,
			'post_status' => 'publish'
		);
		$parent = new WP_Query( $args );
		?>
		<?php if ( $parent->have_posts() ) : $counter = 0;?>
			<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
				<?php
					$post_id = get_the_ID();
					$hashtag = get_field('hashtag');
					$challenge_visual = get_field('challenge_visual');
					$challenge_description = get_field('challenge_description');
					$status = get_field('status');
				?>
				<div class="challenge challenge-<?php echo $hashtag; echo " ".$status; ?>">
					<?php if(!empty($challenge_visual)) : ?>
						<h3>#<?php echo $hashtag; ?></h3>
						<a class="challenge-link-box" href="<?php echo get_permalink(4); ?>?s=<?php echo $hashtag; ?>">
							<img src="<?php echo $challenge_visual['sizes']['large']; ?>" class="img-responsive">
							<div class="challenge-description">
								<?php echo $challenge_description; ?>
							</div>
							<div class="challenge-overlay-background"></div>
						</a>
					<?php else : ?>
						<a class="challenge-link-box" href="<?php echo get_permalink(4); ?>?s=<?php echo $hashtag; ?>">
							<h3>#<?php echo $hashtag; ?></h3>
							<div class="description">
								<?php echo $challenge_description; ?>
							</div>
						</a>
						<?php if (current_user_can( 'manage_options' )) : ?>
							<div class="challenge-admin-close close-<?php echo $hashtag; ?>"><img src="<?php echo get_template_directory_uri ()."/images/challenge-admin-close.png" ?>" alt="X"></div>
							<div class="challenge-admin-panel panel-<?php echo $hashtag; ?>" data-clickSelector=".close-<?php echo $hashtag; ?>" data-containerSelector=".challenge-<?php echo $hashtag; ?>">
								<?php $nonce_hide = wp_create_nonce("hide_challenge_nonce"); ?>
								<a class="challenge-admin-hide" href="#" data-nonce="<?php echo $nonce_hide; ?>" data-id="<?php echo $post_id; ?>">Hide</a>
								<?php $nonce_lock = wp_create_nonce("lock_challenge_nonce"); ?>
								<a class="challenge-admin-lock" href="#" data-nonce="<?php echo $nonce_lock; ?>" data-id="<?php echo $post_id; ?>" data-status="<?php echo $status; ?>"><?php echo ($status === "archive")?"Unlock":"Lock"; ?></a>
							</div>
						<?php endif; ?>
					<?php endif; ?>
				</div>
			<?php endwhile; ?>
		<?php endif; wp_reset_query(); ?>
	</div><!-- .entry-content -->

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