<?php
/*
 * Template name: Residences
 *
 * @package far_loop
 */

get_header();

// Obtener los campos ACF
$fields = array_fill_keys(array(
    'featured_image_residences',
    'description_residences',
    'sections_residences',
), null);

// Rellenar los campos con valores
foreach ($fields as $field => $value) {
    $fields[$field] = get_field($field);
}

?>


<?php
// Función para mostrar la miniatura de una residencia en formato archive-grid
function residency_thumb($post) {
    // Obtiene campos personalizados si aplica
    $fields = get_custom_post_type_fields($post);
    $cover  = get_field($fields['cover'], $post);

    // Si es un post normal, usa la imagen destacada
    if ($post->post_type == 'post') {
        $post_thumbnail_id  = get_post_thumbnail_id($post);
        $post_thumbnail_src = wp_get_attachment_image_src($post_thumbnail_id, 'large');
        if ($post_thumbnail_src) {
            $cover['url'] = $post_thumbnail_src[0];
        }
    }

    // Fallback a placeholder
    $src = (empty($cover['url'])) 
        ? get_template_directory_uri() . '/img/horizontal-placeholder.svg' 
        : $cover['url'];

    // Fecha del post
    $post_date = get_the_date('F j, Y', $post);

    // Salida HTML en formato archive-grid
    ?>
    <div class="archive-item">
        <a href="<?php echo esc_url(get_permalink($post)); ?>" class="archive-new-thumb">
            <div class="archive-thumb__image-container residency">
                <img src="<?php echo esc_url($src); ?>" alt="<?php echo esc_attr(get_the_title($post)); ?>">
            </div>
            <div class="archive-thumb__text">
                <h3 class="archive-thumb__title">
                    <?php echo esc_html(get_the_title($post)); ?>
                </h3>
                <div class="archive-thumb__year">
                    <?php echo esc_html($post_date); ?>
                </div>
            </div>
        </a>
    </div>
    <?php
}
?>

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

    <!--
        <div class="grid u_mh-min1 u_mb-3 u_mb-md-0">

            <div class="col--md--one--half u_ph-1">

                <?php if (!empty($fields['sections_residences'])) : ?>
                    <ul class="same-page-nav">
                        <?php foreach ($fields['sections_residences'] as $section) : ?>
                            <li class="same-page-nav__item">
                                <a href="#<?php echo sanitize_title($section['title']); ?>" data-soft-scroll="#<?php echo sanitize_title($section['title']); ?>" class="same-page-nav__link">
                                    <?php echo $section['title']; ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>

            </div>
        </div>
-->  

        <figure class="cover-image activity-single-image">
            <img src="<?php echo $fields['featured_image_residences']['url']; ?>" alt="<?php echo $fields['featured_image_residences']['caption']; ?>">
            <figcaption class="cover-image__caption"><?php echo $fields['featured_image_residences']['caption']; ?></figcaption>
        </figure>

        <div class="grid u_mh-min1p5">
            <div data-sticky-sidebar class="col--sm--three--seventh col--md--two--fifth u_ph-1p5">
                <div class="page-title u_mb-3 u_mb-md-4">
                    <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">
                <!-- Módulo WYSIWYG -->
                <div class="wysiwyg">
                    <?php echo $fields['description_residences']; ?>
                </div>
            </div>
        </div>

        <?php if (!empty($fields['sections_residences'])) : ?>
            <?php foreach ($fields['sections_residences'] as $section) : ?>

                <div id="<?php echo sanitize_title($section['title']); ?>" class="u_mv-2 u_pt-2">
        

                   

                    <?php if (!empty($section['content'])) : ?>
                        <div class="grid u_mh-min1p5 u_mb-2">
                           
                            <div data-sticky-sidebar  class="col--sm--three--seventh col--md--two--fifth u_ph-1p5">
                                 <h3 class="section-heading delta"><span class="u_text-underline"><?php echo $section['title']; ?></span></h3>
                              
                                    <!-- Módulo info-card -->
                                    <div class="info-card">
                                        <dl class="info-card__list">

                                            <?php if (!empty($section['dates_and_venues'])) : ?>
                                                <dt class="info-card__key">
                                                    <span class="info-card__key-text"><?php _e('Dates and venues', 'far_loop'); ?></span>
                                                </dt>
                                                <dd class="info-card__value"><?php echo $section['dates_and_venues']; ?></dd>
                                            <?php endif; ?>

                                            <?php if (!empty($section['programme'])) : ?>
                                                <dt class="info-card__key">
                                                    <span class="info-card__key-text"><?php _e('Programme', 'far_loop'); ?></span>
                                                </dt>
                                                <dd class="info-card__value">
                                                    <?php printf(__('Download the complete programme %s', 'far_loop'), '<a href="' . $section['programme']['url'] . '">' . __('here', 'far_loop') . '</a> (pdf)'); ?>
                                                </dd>
                                            <?php endif; ?>

                                        </dl>
                                    </div>
                               
                            </div>

                             <div class="col--sm--four--seventh col--md--three--fifth u_ph-1p5">
                                <!-- Módulo WYSIWYG -->
                                <div class="wysiwyg">
                                    <?php echo $section['content']; ?>
                                </div>
                            </div>


                        </div>
                    <?php endif; ?>

              
                        

						  <div class="activities archive-grid">
							<?php if (!empty($section['activities'])) : ?>
								<?php foreach ($section['activities'] as $activity) : ?>
									<?php residency_thumb($activity); ?>
								<?php endforeach; ?>
							<?php else : ?>
								<p>No activities found for this section.</p>
							<?php endif; ?>
						</div>
                 
                </div>

            <?php endforeach; ?>
        <?php endif; ?>

    </div>
</div>

<?php get_footer(); ?>