<?php
/*
Template Name: Últimos Posts
Template Post Type: page
*/

get_header(); ?>

<main class="main-content">
    <div class="container">
        
        <!-- Archive Header (same style as categories) -->
        <div class="search-results-header">
            <h1><?php echo esc_html(get_the_title()); ?></h1>
            <?php 
            $page_description = get_post_meta(get_the_ID(), '_pulsegrid_page_description', true);
            echo !empty($page_description) ? '<p>' . wp_kses_post($page_description) . '</p>' : '<p>Descubre los últimos posts publicados en nuestro sitio.</p>';
            ?>
        </div>
        
        <!-- Archive Results Grid (same as categories) -->
        <?php
        // Get page number for pagination - custom page templates need special handling
        $paged = 1;
        if (get_query_var('paged')) {
            $paged = get_query_var('paged');
        } elseif (get_query_var('page')) {
            $paged = get_query_var('page');
        }
        
        // Fallback: extract from URL if query vars don't work
        if ($paged == 1 && preg_match('/\/page\/(\d+)/', $_SERVER['REQUEST_URI'], $matches)) {
            $paged = intval($matches[1]);
        }
        
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 14,
            'paged' => $paged,
            'ignore_sticky_posts' => 1,
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key' => '_pulsegrid_exclude_from_latest',
                    'compare' => 'NOT EXISTS'
                ),
                array(
                    'key' => '_pulsegrid_exclude_from_latest',
                    'value' => '1',
                    'compare' => '!='
                )
            )
        );
        
        $latest_posts = new WP_Query($args);
        
        if ($latest_posts->have_posts()) :
        ?>
            <div class="search-results-grid posts-grid-vertical">
                <?php
                while ($latest_posts->have_posts()) : 
                    $latest_posts->the_post();
                ?>
                    <article class="post-item archive-item">
                        <a href="<?php the_permalink(); ?>">
                            <?php pulsegrid_post_thumbnail_with_badges('medium'); ?>
                        </a>
                        <div class="post-content">
                            <h3 class="post-title">
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>
                        </div>
                    </article>
                <?php
                endwhile;
                ?>
            </div>
            
            <!-- Pagination -->
            <div class="pagination">
                <?php
                echo paginate_links(array(
                    'prev_text' => '« Anterior',
                    'next_text' => 'Siguiente »',
                    'type' => 'list',
                    'total' => $latest_posts->max_num_pages,
                    'current' => $paged
                ));
                wp_reset_postdata();
                ?>
            </div>
            
        <?php else : ?>
                <div class="no-results">
                    <h2>No hay posts publicados</h2>
                    <p>Lo sentimos, pero no hay posts disponibles en este momento.</p>
                    
                    <!-- Suggested posts (same as archive) -->
                    <div class="suggested-posts">
                        <h3>Explora otros contenidos:</h3>
                        <div class="posts-grid">
                            <?php
                            $suggested_posts = new WP_Query(array(
                                'posts_per_page' => 7,
                                'orderby' => 'rand'
                            ));
                            
                            if ($suggested_posts->have_posts()) :
                                while ($suggested_posts->have_posts()) : $suggested_posts->the_post();
                            ?>
                                <article class="post-item">
                                    <a href="<?php the_permalink(); ?>">
                                        <?php if (has_post_thumbnail()) : ?>
                                            <img src="<?php the_post_thumbnail_url('medium'); ?>" alt="<?php the_title(); ?>" class="post-thumbnail">
                                        <?php else : ?>
                                            <div class="post-thumbnail no-image">Sin imagen</div>
                                        <?php endif; ?>
                                        <div class="post-content">
                                            <h3 class="post-title"><?php the_title(); ?></h3>
                                        </div>
                                    </a>
                                </article>
                            <?php
                                endwhile;
                                wp_reset_postdata();
                            endif;
                            ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        
    </div>
</main>

<?php get_footer(); ?>
