<?php
/**
 * Template part for displaying the blog section on the homepage
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

// Check if section is enabled
$blog_enable = get_theme_mod('paws_and_care_blog_enable', true);
if (!$blog_enable) {
    return;
}

// Get customizer values
$blog_title = get_theme_mod('paws_and_care_blog_title', __('Latest Pet Care Tips', 'paws-and-care'));
$blog_subtitle = get_theme_mod('paws_and_care_blog_subtitle', __('Stay informed with our expert advice and pet care articles.', 'paws-and-care'));
$blog_posts_count = get_theme_mod('paws_and_care_blog_posts_count', 3);
$blog_category = get_theme_mod('paws_and_care_blog_category', '');
$blog_show_category = get_theme_mod('paws_and_care_blog_show_category', true);
$blog_show_date = get_theme_mod('paws_and_care_blog_show_date', true);
$blog_excerpt_length = get_theme_mod('paws_and_care_blog_excerpt_length', 20);
$blog_read_more = get_theme_mod('paws_and_care_blog_read_more', __('Read More', 'paws-and-care'));

// Build query args
$query_args = array(
    'posts_per_page' => $blog_posts_count,
    'post_status' => 'publish',
    'post_type' => 'post',
);

if (!empty($blog_category)) {
    $query_args['cat'] = $blog_category;
}

$recent_posts = new WP_Query($query_args);
?>

<section class="py-5 bg-light blog-section">
    <?php paws_and_care_section_shortcut('paws_and_care_blog', __('Edit Blog', 'paws-and-care')); ?>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-8 text-center">
                <h2 class="section-title"><?php echo esc_html($blog_title); ?></h2>
                <p class="lead mb-5"><?php echo esc_html($blog_subtitle); ?></p>
            </div>
        </div>
        <div class="row g-4">
            <?php
            if ($recent_posts->have_posts()):
                while ($recent_posts->have_posts()):
                    $recent_posts->the_post();
                    ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="blog-card card h-100">
                            <?php if (has_post_thumbnail()): ?>
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail('medium_large', array('class' => 'card-img-top')); ?>
                                </a>
                            <?php endif; ?>
                            <div class="card-body">
                                <?php if ($blog_show_category || $blog_show_date): ?>
                                    <div class="d-flex align-items-center mb-3">
                                        <?php
                                        if ($blog_show_category):
                                            $categories = get_the_category();
                                            if (!empty($categories)):
                                                ?>
                                                <span class="badge bg-primary me-2"><?php echo esc_html($categories[0]->name); ?></span>
                                            <?php endif;
                                        endif;
                                        ?>
                                        <?php if ($blog_show_date): ?>
                                            <small class="text-muted"><i class="fa-regular fa-calendar me-1"></i>
                                                <?php echo esc_html( get_the_date() ); ?></small>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                                <h5 class="card-title">
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </h5>
                                <p class="card-text"><?php echo wp_trim_words(get_the_excerpt(), $blog_excerpt_length); ?></p>
                                <a href="<?php the_permalink(); ?>"
                                    class="btn btn-link text-primary p-0"><?php echo esc_html($blog_read_more); ?> <i
                                        class="fa-solid fa-arrow-right ms-1"></i></a>
                            </div>
                        </div>
                    </div>
                    <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
        </div>
    </div>
</section>