<?php

/*
 * Template name: Loop Collection
 *
 * @package far_loop
 */

get_header();

?>



<div class="u_pt-3 u_pt-md-6">
	
	
	<div id="mainContainer" class="l_container">

		
		<div class="grid">
	
			<div class="cover-image activity-single-image">
				<?php 
				if ( has_post_thumbnail() ) {
					the_post_thumbnail( 'full', [ 'alt' => get_the_title() ] );
				}
				?>
			</div>
			
			
			<div class="col--sm--two--fifth">
				<!-- page-title module-->
				<div class="page-title u_mb-2 u_mb-md-2">
					<!-- Page title-->
					<h1 class="page-title__apex-title"><?php the_title();?></h1>
				</div>
			</div>
			<div class="col--sm--four--seventh col--md--three--fifth">
				
				<?php if ( have_rows('award_repeater') ) : ?>
					<?php while ( have_rows('award_repeater') ) : the_row(); ?>
							<div class="wysiwyg"><?php echo get_sub_field('description_award');?></div>
					<?php endwhile; ?>
				<?php endif; ?>
			</div>
		</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
				// Obtener descripción general sin las etiquetas <p>
				$description_award = get_sub_field('description_award');
				$description_award = wpautop($description_award); // Asegura el formato de párrafos
				$description_award = str_replace(array('<p>', '</p>'), '', $description_award); // Elimina etiquetas <p>

				$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'); // Obtener el término del premio
				?>

						<!-- awards-section module (archive-grid) -->
						<div id="<?php echo esc_attr( $term->slug ); ?>" class="awards-section">
						<div class="u_mb-3">
							<?php
							// Videos del award seleccionado
							$videos = get_posts( array(
							'post_type'      => 'artist-video',
							'tax_query'      => array(
								array(
								'taxonomy' => 'award',
								'field'    => 'term_id',
								'terms'    => (int) $award_selection_general,
								),
							),
							'nopaging'       => true,
							'post_status'    => 'publish',
							'suppress_filters' => false, // respeta idioma si usas WPML/Polylang
							) );

							if ( ! empty( $videos ) ) :
							echo '<div class="archive-grid">';

							foreach ( $videos as $video ) :
								$vid_id  = (int) $video->ID;
								$url     = get_permalink( $vid_id );

								// Imagen
								$cover   = get_field( 'cover_image', $vid_id );
								$src     = ( ! empty( $cover['url'] ) ) ? $cover['url'] : get_template_directory_uri() . '/img/horizontal-placeholder.svg';

								// Datos
								$title   = get_the_title( $vid_id );
								$year    = get_field( 'year', $vid_id );

								$authors = get_field( 'author', $vid_id );
								$authors_string = function_exists('get_authors_string') ? get_authors_string( $authors ) : '';

								// Gallery (nombre + ciudad)
								$gallery = get_field( 'gallery', $vid_id );
								$gallery_string = '';
								if ( $gallery ) {
								$gid     = is_object( $gallery ) ? $gallery->ID : (int) $gallery;
								$g_title = get_the_title( $gid );
								$addr    = get_field( 'address', $gid );
								$g_city  = ( ! empty( $addr[0]['city'] ) ) ? $addr[0]['city'] : '';
								$gallery_string = trim( $g_title . ( $g_city ? " ($g_city)" : '' ) );
								}

								// Render tarjeta archive-grid
								?>
								<div class="archive-item">
								<a href="<?php echo esc_url( $url ); ?>" class="archive-new-thumb">
									<div class="archive-thumb__image-container award">
									<img src="<?php echo esc_url( $src ); ?>" alt="<?php echo esc_attr( $title ); ?>">
									</div>
									<div class="archive-thumb__text">
									<h3 class="archive-thumb__title">
										<?php echo esc_html( $title ); ?>
										<?php if ( ! empty( $year ) ) echo ' (' . esc_html( $year ) . ')'; ?>
									</h3>

									<div class="archive-thumb__label">
										<?php if ( $authors_string ) : ?>
										<div class="awards-past-thumb__title"><?php echo esc_html( $authors_string ); ?></div>
										<?php endif; ?>
									</div>

									<?php if ( $gallery_string ) : ?>
										<div class="archive-thumb__year"><?php echo esc_html( $gallery_string ); ?></div>
									<?php endif; ?>

									</div>
								</a>
								</div>
								<?php
							endforeach;

							echo '</div>'; // .archive-grid
							endif;
							?>
						</div>
						</div>

				<?php endif; // Fin de comprobación de premio seleccionado ?>

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

	</div>
</div>

<?php get_footer(); ?>
