<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Template Name: Blog with Sidebar
 * The template for displaying blog posts with sidebar
 *
 * @package Pawfect
 */

get_header();

// Get blog settings
$blog_layout = get_theme_mod('pawfect_blog_layout', 'right-sidebar');
$blog_style = get_theme_mod('pawfect_blog_style', 'grid');
$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');
}
?>

<div class="pawfect-container pawfect-mx-auto pawfect-px-4 pawfect-py-12">
    <div class="pawfect-grid <?php echo $blog_layout === 'left-sidebar' ? 'lg:pawfect-grid-cols-[300px_1fr]' : ($blog_layout === 'right-sidebar' ? 'lg:pawfect-grid-cols-[1fr_300px]' : 'lg:pawfect-grid-cols-1'); ?> pawfect-gap-8">
        
        <?php if ($blog_layout === 'left-sidebar'): ?>
            <!-- Left Sidebar -->
            <aside class="pawfect-blog-sidebar">
                <?php get_sidebar(); ?>
            </aside>
        <?php
endif; ?>

        <!-- Main Content -->
        <main id="primary-content" class="pawfect-blog-main" tabindex="-1">
            <?php
// 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'
));

if ($blog_query->have_posts()): ?>
                
                <div class="pawfect-blog-posts <?php echo $blog_style === 'grid' ? 'pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 pawfect-gap-8' : 'pawfect-space-y-8'; ?>">
                    
                    <?php while ($blog_query->have_posts()):
        $blog_query->the_post(); ?>
                        
                        <article id="post-<?php the_ID(); ?>" <?php post_class('pawfect-blog-post pawfect-bg-white pawfect-rounded-xl  pawfect-overflow-hidden pawfect-transition-all pawfect-duration-300 '); ?>>
                            
                            <?php if ($show_featured_image && has_post_thumbnail()): ?>
                                <div class="pawfect-post-thumbnail pawfect-relative pawfect-overflow-hidden">
                                    <a href="<?php the_permalink(); ?>" class="pawfect-block">
                                        <?php the_post_thumbnail('large', array('class' => 'pawfect-w-full pawfect-h-64 pawfect-object-cover pawfect-transition-transform pawfect-duration-300 hover:pawfect-scale-105')); ?>
                                    </a>
                                </div>
                            <?php
        endif; ?>

                            <div class="pawfect-post-content pawfect-p-6">
                                
                                <?php if ($show_meta): ?>
                                    <div class="pawfect-post-meta pawfect-flex pawfect-items-center pawfect-gap-4 pawfect-text-sm pawfect-text-gray-500 pawfect-mb-3">
                                        <span class="pawfect-post-date">
                                            <i class="pawfect-icon-calendar"></i>
                                            <?php echo esc_html(get_the_date()); ?>
                                        </span>
                                        <span class="pawfect-post-author">
                                            <i class="pawfect-icon-user"></i>
                                            <?php echo esc_html(get_the_author()); ?>
                                        </span>
                                        <?php if (get_the_category()): ?>
                                            <span class="pawfect-post-category">
                                                <i class="pawfect-icon-folder"></i>
                                                <?php the_category(', '); ?>
                                            </span>
                                        <?php
            endif; ?>
                                    </div>
                                <?php
        endif; ?>

                                <h2 class="pawfect-post-title pawfect-text-xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-3 pawfect-leading-tight">
                                    <a href="<?php the_permalink(); ?>" class="pawfect-text-gray-900 hover:pawfect-text-pawfect-primary pawfect-transition-colors pawfect-duration-300">
                                        <?php the_title(); ?>
                                    </a>
                                </h2>

                                <?php if ($show_excerpt): ?>
                                    <div class="pawfect-post-excerpt pawfect-text-gray-600 pawfect-mb-4 pawfect-leading-relaxed">
                                        <?php
            if (has_excerpt()) {
                echo wp_trim_words(get_the_excerpt(), $excerpt_length, '...');
            }
            else {
                echo wp_trim_words(get_the_content(), $excerpt_length, '...');
            }
?>
                                    </div>
                                <?php
        endif; ?>

                                <?php if ($show_read_more): ?>
                                    <div class="pawfect-post-read-more">
                                        <a href="<?php the_permalink(); ?>" class="pawfect-inline-flex pawfect-items-center pawfect-text-pawfect-primary pawfect-font-medium pawfect-text-sm hover:pawfect-text-pawfect-secondary pawfect-transition-colors pawfect-duration-300">
                                            <?php echo esc_html($read_more_text); ?>
                                            <svg class="pawfect-w-4 pawfect-h-4 pawfect-ml-2 pawfect-transition-transform pawfect-duration-300 group-hover:pawfect-translate-x-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3"></path>
                                            </svg>
                                        </a>
                                    </div>
                                <?php
        endif; ?>

                            </div>
                        </article>

                    <?php
    endwhile; ?>
                    
                </div>

                <!-- Pagination -->
                <?php
    // Custom Pagination (same as shop)
    $pages = paginate_links(array(
        'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
        'format' => '?paged=%#%',
        'current' => max(1, $paged),
        'total' => $blog_query->max_num_pages,
        'type' => 'array',
        'prev_text' => '←',
        'next_text' => '→',
    ));
    if ($pages): ?>
                    <nav class="pawfect-flex pawfect-justify-center pawfect-items-center pawfect-space-x-4 pawfect-mt-12">
                        <?php foreach ($pages as $page):
            // Detect current page
            if (strpos($page, 'current') !== false) {
                // Extract page number
                preg_match('/>([0-9]+)</', $page, $matches);
                $num = isset($matches[1]) ? $matches[1] : '';
                echo '<span class="pawfect-px-4 pawfect-py-2 pawfect-rounded-md pawfect-bg-accent pawfect-text-white">' . esc_html($num) . '</span>';
            }
            else {
                // Output link
                echo str_replace('<a', '<a class="pawfect-px-4 pawfect-py-2 pawfect-rounded-md pawfect-text-text-light hover:pawfect-bg-white"', $page);
            }
        endforeach; ?>
                    </nav>
                <?php
    endif; ?>

            <?php
else: ?>
                
                <div class="pawfect-no-posts pawfect-text-center pawfect-py-12">
                    <h2 class="pawfect-text-2xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4"><?php echo esc_html__( 'No Posts Found', 'pawfect' ); ?></h2>
                    <p class="pawfect-text-gray-600"><?php esc_html_e( 'Sorry, no blog posts were found. Please check back later.', 'pawfect' ); ?></p>
                </div>

            <?php
endif; ?>

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

        <?php if ($blog_layout === 'right-sidebar'): ?>
            <!-- Right Sidebar -->
            <aside class="pawfect-blog-sidebar">
                <?php get_sidebar(); ?>
            </aside>
        <?php
endif; ?>

    </div>
</div>

<?php get_footer(); ?>
