<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Template Name: Blog Grid
 * 
 * @package Pawfect
 */

get_header();

// Get blog settings from customizer
$posts_per_page = get_theme_mod('pawfect_blog_posts_per_page', 6);
$show_featured_image = get_theme_mod('pawfect_blog_show_featured_image', true);
$show_meta = get_theme_mod('pawfect_blog_show_meta', true);
$show_excerpt = get_theme_mod('pawfect_blog_show_excerpt', true);
$excerpt_length = get_theme_mod('pawfect_blog_excerpt_length', 25);
$show_read_more = get_theme_mod('pawfect_blog_show_read_more', true);
$read_more_text = get_theme_mod('pawfect_blog_read_more_text', __('Read More', 'pawfect'));

// Page Header
if (!get_theme_mod('pawfect_page_header_disabled', false)) {
    get_template_part('template-parts/page-header');
}

// Custom query for blog posts
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$blog_query = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'post_status' => 'publish'
));
?>

<div class="pawfect-container pawfect-mx-auto pawfect-px-4 pawfect-py-12">
    
    <!-- Page Content -->
    <?php while (have_posts()):
    the_post(); ?>
        <?php if (get_the_content()): ?>
            <div class="pawfect-page-content pawfect-mb-12 pawfect-text-center">
                <div class="pawfect-max-w-3xl pawfect-mx-auto">
                    <?php the_content(); ?>
                </div>
            </div>
        <?php
    endif; ?>
    <?php
endwhile; ?>

    <!-- Blog Grid Layout (No Sidebar) -->
    <main id="primary-content" class="pawfect-blog-main" tabindex="-1">
        
        <?php if ($blog_query->have_posts()): ?>
            
            <!-- Posts Grid -->
            <div class="pawfect-posts-container pawfect-posts-grid">
                <div class="pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 lg:pawfect-grid-cols-3 pawfect-gap-8">
                    
                    <?php while ($blog_query->have_posts()):
        $blog_query->the_post(); ?>

                        <?php
        // Use the OOP content.php approach with page template specific settings
        get_template_part('template-parts/post/content', '', array(
            'card_style' => get_theme_mod('pawfect_blog_grid_card_style', 'modern'),
            'layout_style' => 'grid', // Grid template is always grid
            'show_featured_image' => $show_featured_image,
            'show_meta' => $show_meta,
            'show_excerpt' => $show_excerpt,
            'excerpt_length' => $excerpt_length,
            'show_read_more' => $show_read_more,
            'read_more_text' => $read_more_text,
            'use_drag_drop' => true // Enable drag & drop ordering
        )); ?>

                    <?php
    endwhile; ?>
                    
                </div>
            </div>

            <!-- Pagination -->
            <div class="pawfect-pagination pawfect-mt-12 pawfect-text-center">
                <?php
    echo paginate_links(array(
        'total' => $blog_query->max_num_pages,
        'current' => $paged,
        'mid_size' => 2,
        'prev_text' => __('← ', 'pawfect'),
        'next_text' => __(' →', 'pawfect'),
        'class' => 'pawfect-pagination-links'
    ));
?>
            </div>

        <?php
else: ?>
            
            <!-- No Posts Found -->
            <div class="pawfect-no-posts pawfect-text-center pawfect-py-16">
                <div class="pawfect-max-w-md pawfect-mx-auto">
                    <h2 class="pawfect-text-2xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                        <?php esc_html_e('No posts found', 'pawfect'); ?>
                    </h2>
                    <p class="pawfect-text-gray-600 pawfect-mb-6">
                        <?php esc_html_e('Sorry, no posts were found.', 'pawfect'); ?>
                    </p>
                    <a href="<?php echo esc_url(home_url('/')); ?>" class="pawfect-btn pawfect-bg-pawfect-primary pawfect-text-white pawfect-px-6 pawfect-py-3 pawfect-rounded-lg pawfect-font-medium pawfect-transition-all pawfect-duration-200 hover:pawfect-bg-opacity-90">
                        <?php esc_html_e('Back to Home', 'pawfect'); ?>
                    </a>
                </div>
            </div>

        <?php
endif; ?>
        
        <?php wp_reset_postdata(); ?>
    </main>
</div>

<?php get_footer(); ?>
