<?php

/*
 * Template name: Awards
 *
 * @package far_loop
 *
 */

get_header();

$terms = get_terms( array(
	'taxonomy' => 'award',
	'orderby' => 'term_id',
	'order' => 'ASC',
	'hide_empty' => true,
));

?>

<div class="u_pt-3 u_pt-md-6">
	<div id="mainContainer" class="l_container u_mv-5">
		<div class="grid u_mh-min1 u_mb-1">
			<div class="col--md--one--third u_ph-1">
				<!-- page-title module-->
				<div class="page-title u_mb-3 u_mb-md-4">
					<!-- Page title-->
					<h1 class="page-title__apex-title u_mb-0p5"><?php _e( 'Awards', 'far_loop' );?></h1>
				</div>
			</div>
			<div class="col--md--two--third u_ph-1">

				<?php if ( !empty( $terms ) ) : ?>

				<!-- same-page-nav module-->
				<ul class="same-page-nav">

				<?php foreach ( $terms as $term ) : ?>

					<li class="same-page-nav__item">
						<a href="#" data-soft-scroll="#<?php echo $term->slug;?>" class="same-page-nav__link"><?php echo $term->name;?></a>
					</li>

				<?php endforeach;?>

				</ul>

				<?php endif; ?>

	    	</div>
		</div>

		<?php

		if ( !empty( $terms ) ) :

		foreach ( $terms as $term ) :

		$args = array(
			'post_type' => 'artist-video',
			'tax_query' => array(
				array(
					'taxonomy' => 'award',
					'field'    => 'slug',
					'terms'    => $term->slug,
				),
			),
			'nopaging' => true,
		);

		$videos = get_posts( $args );

		$editions = array();

		foreach ($videos as $key => $video) {

		    $edition = get_the_terms( $video->ID, 'edition');

		    $editions[$key] = $edition[0]->slug;

		}

		array_multisort($editions, SORT_DESC, $videos);

		?>

		<!-- awards-section module-->
		<div id="<?php echo $term->slug;?>" class="awards-section">
			<div class="grid u_mh-min1">
			  <div data-sticky-sidebar="data-sticky-sidebar" class="col--sm--two--fifth u_ph-1">
			    <h2 class="awards-section__title"><?php echo $term->name;?></h2>

			    <?php

				$link = get_field( 'link', 'award_' . $term->term_id );

				if ( $link ) : ?>

			    <a href="<?php echo $link;?>" class="awards-section__external-link"><span class="awards-section__external-link-text"><?php echo remove_http( $link );?></span><span class="icon icon--external-link"></span></a>

			    <?php endif;?>


			    <!-- share-module-->
			    <?php share_module();?>
			  </div>
			  <div class="col--sm--three--fifth u_ph-1">
			    <div class="u_mb-5">
			      <!-- lead-description module-->
			      <p class="lead-description u_mb-2 h1"><?php echo $term->description;?></p>
			    </div>
			    <?php

				$current_award = array_shift( $videos );

				$editions = get_the_terms( $current_award->ID, 'edition' );

				$url = get_permalink( $current_award->ID );
				$cover = get_field( 'cover_image', $current_award->ID );
				$src = ( empty( $cover ) ) ? get_template_directory_uri() . '/img/horizontal-placeholder.svg' : $cover['url'];
				$year = get_field( 'year', $current_award->ID );
				$authors = get_field( 'author', $current_award->ID );
				$authors_string = get_authors_string( $authors );
				$gallery = get_field( 'gallery', $current_award->ID );
				$gallery_title = $city = '';
				if ( !empty( $gallery ) ) {
					$gallery_address = get_field( 'address', $gallery->ID );
					$gallery_title = get_the_title( $gallery->ID );
					$city = $gallery_address[0]['city'];
				}

				?>
			    <!-- section-heading module-->
			    <h3 class="section-heading delta"><span class="u_text-underline"><?php echo $term->name . ' ' . $editions[0]->name; ?></span></h3>
			    <!-- awards-thumb module--><a href="<?php echo $url;?>" class="awards-thumb">
			      <div class="awards-thumb__image"><img src="<?php echo $src;?>" alt="<?php echo $term->name . ' ' . $editions[0]->name; ?>">
			      </div>
			      <div class="awards-thumb__info">
			        <h3 class="awards-thumb__title"><em><?php echo $current_award->post_title; ?></em><?php if ( !empty( $year ) ) echo " ($year)";?></h3>
			        <h4 class="awards-thumb__author"><?php echo $authors_string;?></h4>
			        <h5 class="awards-thumb__gallery"><?php echo $gallery_title;?></h5>
			        <h6 class="awards-thumb__city"><?php echo $city;?></h6>
			        <div class="awards-thumb__cta"><span class="awards-thumb__cta-text"><?php _e('Read more', 'far_loop' );?></span></div>
			      </div></a>
			    <div class="u_mb-3">
			      <?php

			        $jury = get_field( 'jury', 'award_' . $term->term_id );

			        if ( !empty( $jury ) ) :

			        $html = ''; ?>

			        <!-- section-heading module-->
			        <h3 class="section-heading delta"><span class="u_text-underline"><?php echo $term->name . ' ' . __( 'jury', 'far_loop' );?></span></h3>
			        <div class="grid u_mh-min1">

				    <?php

				    foreach ( $jury as $member ) :

				    // Duplicate from speakers.php with modifications
				    // FUTURE UNIFICATION

				    $portrait = get_field( 'portait', $member );

					if ( !empty( $portrait ) ) {
						$src = $portrait['sizes']['squared'];
					} else {
						$src = get_template_directory_uri() . '/img/square-placeholder.svg';
					}

					$html .= '<div class="col--xxs--one--half col--xs--one--third col--sm--one--quarter col--tb--one--fifth u_ph-1"><a href="' . get_permalink($member) . '" class="speaker-portrait">';
					$html .= '<div class="u_mb-0p5"><img src="' . $src . '" alt="' . $member->post_title .'"></div>';
					$html .= '<h3 class="speaker-portrait__name">' . $member->post_title .'</h3></a>';
					$html .= '</div>';

				    endforeach;

				    echo $html; ?>

					</div>

			        <?php endif; ?>

			    </div>
			  </div>
			</div>
			<div class="u_mb-3">

				<?php if ( !empty( $videos ) ) :?>

				<!-- section-heading module-->
				<h3 class="section-heading delta"><span class="u_text-underline"><?php echo $term->name . ' ' . __( 'past editions', 'far_loop' );?></span></h3>

				<?php foreach ( $videos as $video ) :

					// Duplicate code video_thumb()
					// Future Unification

					$url = get_permalink( $video->ID );
					$cover = get_field( 'cover_image', $video->ID );
					$src = ( empty( $cover ) ) ? get_template_directory_uri() . '/img/horizontal-placeholder.svg' : $cover['url'];
					$year = get_field( 'year', $video->ID );
					$authors = get_field( 'author', $video->ID );
					$authors_string = get_authors_string( $authors );
					$gallery = get_field( 'gallery', $video->ID );
					$gallery_string = '';
					if ( !empty( $gallery ) ) {
						$gallery_address = get_field( 'address', $gallery->ID );
						$gallery_title = get_the_title( $gallery->ID );
						$city = $gallery_address[0]['city'];
						$gallery_string = "$gallery_title ($city)";
					}
					$editions = get_the_terms( $video->ID, 'edition' );
				?>

				<!-- awards-past-thumb module-->
				<a href="<?php echo $url;?>" class="awards-past-thumb"><img src="<?php echo $src;?>" alt="<?php echo $video->post_title; ?>">
					<h3 class="awards-past-thumb__title"><em><?php echo $video->post_title; ?></em><?php if ( !empty( $year ) ) echo " ($year)";?></h3>
					<h4 class="awards-past-thumb__author"><?php echo $authors_string;?></h4>
					<h5 class="awards-past-thumb__gallery"><?php echo $gallery_string;?></h5>
					<h6 class="awards-past-thumb__award"><?php echo $term->name . ' ' . $editions[0]->slug;?></h6>
				</a>

				<?php endforeach; endif;?>

			</div>
		</div>

		<?php endforeach; endif; ?>


	</div>
</div>

<?php

get_footer();
