<?php

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

get_header();

// Get blog settings from customizer
$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');
}

// 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 Layout with Left Sidebar -->
    <div class="pawfect-grid lg:pawfect-grid-cols-[300px_1fr] pawfect-gap-8">
        
        <!-- Left Sidebar -->
        <aside class="pawfect-blog-sidebar">
            <?php get_sidebar(); ?>
        </aside>

        <!-- Main Content -->
        <main id="primary-content" class="pawfect-blog-main" tabindex="-1">
            
            <?php if ($blog_query->have_posts()): ?>
                
                <!-- Posts Grid/List -->
                <div class="pawfect-posts-container <?php echo $blog_style === 'grid' ? 'pawfect-posts-grid' : 'pawfect-posts-list'; ?>">
                    <?php if ($blog_style === 'grid'): ?>
                        <div class="pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 pawfect-gap-8">
                    <?php
    endif; ?>

                    <?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_left_sidebar_card_style', 'classic'),
            'layout_style' => get_theme_mod('pawfect_blog_left_sidebar_layout_style', '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; ?>

                    <?php if ($blog_style === 'grid'): ?>
                        </div>
                    <?php
    endif; ?>
                </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>
</div>

<?php get_footer(); ?>
