<?php

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

get_header();

?>

<?php
// Obtener el repeater field
$awards_repeater = get_field('award_repeater');

// Inicializar un array para almacenar los términos seleccionados
$selected_terms = array();

// Verificar si el repeater tiene filas
if ( $awards_repeater ) {
    foreach ( $awards_repeater as $row ) {
        $term_id = $row['award_selection_general']; // Asegúrate de usar el nombre del subcampo correcto
        if ( $term_id ) {
            $selected_terms[] = get_term($term_id, 'award'); // Obtener el término
        }
    }
}

// Ordenar términos por ID
usort($selected_terms, function($a, $b) {
    return $a->term_id - $b->term_id;
});
?>

<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">
	
<?php if ( !empty( $selected_terms ) ) : ?>
    <!-- same-page-nav module-->
    <ul class="same-page-nav">
        <!-- Enlace hardcoded "Jury" al principio -->
        <li class="same-page-nav__item">
            <a href="#" data-soft-scroll="#jury" class="same-page-nav__link"><?php _e( 'Jury', 'far_loop' );?></a>
        </li>

        <?php foreach ( $selected_terms as $term ) : ?>
            <li class="same-page-nav__item">
                <a href="#" data-soft-scroll="#<?php echo esc_attr($term->slug); ?>" class="same-page-nav__link">
                    <?php echo esc_html($term->name); ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>


			<br><br>
			
			
			<div class="col--sm--two--fifth 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 the_title();?></h1>
				</div>
			</div>
			<div class="col--sm--four--seventh col--md--three--fifth u_ph-1p5">
				
				
				
			</div>
		</div>
		
		
		
		
 <div id="jury" class="u_mv-4 awards-section">
    
	 
	 
	 
	 <div class="grid u_mh-min1">
		<div class="col--sm--two--quarter u_ph-1">
			<h2 class="awards-section__title"><?php _e( 'Jury', 'far_loop' );?></h2>
		</div>
		<div class="col--sm--two--quarter u_ph-1 lead-description">
				<div class="lead-description u_mb-2 h1"><?php the_field('descripcion');?></div>
		</div>
		
	</div>
		<br><br>  
		  
<?php if ( !empty( get_field('jury_members') ) ) : ?>
    <div class="grid u_mh-min1p5">
        <?php foreach ( get_field('jury_members') as $member_post ) : ?>
            <?php
                // Si $member_post es un objeto de post
                $member_id = $member_post->ID;

                // Obtén los campos ACF del post relacionado
                $portrait = get_field('field_56c86aeb593d5', $member_id); // Usamos el key field para obtener la imagen
                $name = get_field('name', $member_id);
                $surname = get_field('surname', $member_id);
                $about = get_field('biography', $member_id);

                // Limitamos el contenido de "about" a 100 caracteres
                $about_excerpt = wp_trim_words( $about, 20, '...' ); // 15 words with '...' at the end
                $profile_url = get_permalink( $member_id ); // Obtén el enlace al perfil de la persona
            ?>
            <div class="col--xxs--one--half u_ph-1p5">
                <!-- committee-member-portrait module-->
                <div class="committee-member-portrait">
                    <div class="grid u_mh-min0p5">
                        <div class="col--tb--one--third u_ph-0p5">
                            <div class="committee-member-portrait__image-container">
                                <?php if ( $portrait ): ?>
								<a href="<?php echo esc_url( $profile_url ); ?>"><img src="<?php echo esc_url( $portrait['sizes']['squared'] ); ?>" alt="<?php echo esc_attr( $name ); ?>"></a>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="col--tb--two--third u_ph-0p5">
                            <p class="committee-member-name"><b><?php echo esc_html( $name ); ?> <?php echo esc_html( $surname ); ?></b></p>
                            <p class="committee-member-portrait__bio"><?php echo esc_html( $about_excerpt ); ?></p>
                            <a href="<?php echo esc_url( $profile_url ); ?>" class="read-more-btn same-page-nav__link"><?php _e('Read More', 'far_loop'); ?></a>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

		  
		  
	
      </div>
		
		
		
		
		
		
		
		
		
		
		
		
		
<?php if ( have_rows('award_repeater') ) : ?>
    <?php while ( have_rows('award_repeater') ) : the_row(); ?>
        
        <?php
        // Obtener descripción general y el ID del premio seleccionado
        $description_award = get_sub_field('description_award');
        $description_award = wpautop($description_award);
        $description_award = str_replace(array('<p>', '</p>'), '', $description_award);

        $award_selection_general = get_sub_field('award_selection_general');

        // Verificamos que hay un premio seleccionado
        if ( $award_selection_general ) :
            $term = get_term($award_selection_general, 'award');
        ?>

        <!-- awards-section module -->
        <div id="<?php echo $term->slug; ?>" class="awards-section">
            <div class="grid u_mh-min1">
                <div class="col--sm--two--quarter 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 esc_url($link); ?>" class="awards-section__external-link">
                            <span class="awards-section__external-link-text"><?php echo esc_html(remove_http($link)); ?></span>
                            <span class="icon icon--external-link"></span>
                        </a>
                    <?php endif; ?>
                </div>
                <div class="col--sm--two--quarter u_ph-1">
                    <!-- Mostrar descripción general -->
                    <p class="lead-description u_mb-2 h1"><?php echo $description_award; ?></p>
					
					<div class="current-award">
                    <?php
                    // Obtener videos relacionados con el premio seleccionado
                    $args = array(
                        'post_type' => 'artist-video',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'award',
                                'field'    => 'term_id',
                                'terms'    => $award_selection_general,
                            ),
                        ),
                        'nopaging' => true,
                    );
                    $videos = get_posts($args);

                    // Mostrar el primer video en la sección "current-award"
                    if (!empty($videos)) :
                        $first_video = array_shift($videos); // Extraer el primer video
                        $url = get_permalink($first_video->ID);
                        $cover = get_field('cover_image', $first_video->ID);
                        $src = (empty($cover)) ? get_template_directory_uri() . '/img/horizontal-placeholder.svg' : $cover['url'];
                        $year = get_field('year', $first_video->ID);
                        $authors = get_field('author', $first_video->ID);
                        $authors_string = get_authors_string($authors);
                        ?>
                        <a href="<?php echo $url; ?>" class="awards-past-thumb">
							<div class="awards-thumb__image"><img class="current-award-thumb" src="<?php echo $src; ?>" alt="<?php echo esc_attr($first_video->post_title); ?>"></div>
                           
							    
				 <div class="awards-thumb__info">
			        <h3 class="awards-thumb__title"><em><?php echo $first_video->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>
                    <?php endif; ?>
                </div>
					
					
					
					
					
					
					
                </div>

            </div>
            <br><br>
            <div class="u_mb-3">
                <!-- section-heading module -->
                <h3 class="section-heading delta"><span class="u_text-underline"><?php echo esc_html($term->name) . ' ' . __('past editions', 'far_loop'); ?></span></h3>
                <?php
                // Mostrar el resto de los videos (excepto el primero)
                if (!empty($videos)) :?>
                    <div class="archive-grid">
                        <?php foreach ($videos as $video) :
                            $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');
                            ?>
                            <div class="archive-item">
                                <a href="<?php echo $url; ?>" class="archive.new-thumb">
                                    <div class="archive-thumb__image-container award">
                                        <img src="<?php echo esc_url($src); ?>" alt="<?php echo esc_attr($video->post_title); ?>">
                                    </div>
                                    <div class="archive-thumb__text">

                                        <!-- Título -->
                                        <h3 class="archive-thumb__title">
                                            <?php echo esc_html( get_the_title($video->ID) ); ?><?php
                                            if ( !empty($year) ) echo ' (' . esc_html($year) . ')';
                                            ?>
                                        </h3>

                                        <div class="archive-thumb__label">
                                            <div class="awards-past-thumb__title"><?php echo esc_html($authors_string); ?></div>
                                            <div class="awards-past-thumb__title awards-past-thumb__galery"><?php echo esc_html($gallery_string); ?></div>
                                        </div>
                                     </div>
                                     <div class="archive-thumb__year"><?php echo esc_html($term->name) . ' ' . esc_html($editions[0]->name); ?></div>
                                </a>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?>


	</div>
</div>

<?php get_footer(); ?>
