<?php
if (!defined('ABSPATH')) { exit; }
get_header();

$parents = backpro_news_parent_categories();
?>

<?php if (!empty($parents)): ?>
    <div class="bp-grid">
        <?php foreach ($parents as $cat): ?>
            <?php
            $query = new WP_Query([
                'post_type' => 'post',
                'posts_per_page' => 5,
                'cat' => (int) $cat->term_id,
                'ignore_sticky_posts' => true,
            ]);
            if (!$query->have_posts()) {
                wp_reset_postdata();
                continue;
            }
            $query->the_post();
            $lead_id = get_the_ID();
            $leadCats = get_the_category();
            $leadLabel = !empty($leadCats) ? $leadCats[0]->name : $cat->name;
            ?>
            <section class="bp-section">
                <div class="bp-section-head">
                    <h2><?php echo esc_html($cat->name); ?></h2>
                    <a href="<?php echo esc_url(get_category_link($cat)); ?>">Wiecej</a>
                </div>
                <div class="bp-section-body">
                    <article>
                        <?php if (has_post_thumbnail()): ?>
                            <a class="bp-lead-media" href="<?php the_permalink(); ?>" aria-label="<?php the_title_attribute(); ?>">
                                <?php the_post_thumbnail('large', ['class' => 'bp-lead-image']); ?>
                                <span class="bp-mini-label"><?php echo esc_html($leadLabel); ?></span>
                            </a>
                        <?php endif; ?>
                        <h3 class="bp-lead-title">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h3>
                        <p class="bp-lead-excerpt">
                            <?php echo esc_html(wp_trim_words(get_the_excerpt(), 28)); ?>
                        </p>
                    </article>
                    <ul class="bp-mini-grid">
                        <?php while ($query->have_posts()): $query->the_post(); ?>
                            <?php if ((int) get_the_ID() === (int) $lead_id) { continue; } ?>
                            <?php
                                $cardCats = get_the_category();
                                $cardLabel = !empty($cardCats) ? $cardCats[0]->name : $cat->name;
                            ?>
                            <li class="bp-mini-card">
                                <a class="bp-mini-media" href="<?php the_permalink(); ?>" aria-label="<?php the_title_attribute(); ?>">
                                    <?php if (has_post_thumbnail()): ?>
                                        <?php the_post_thumbnail('medium_large', ['class' => 'bp-mini-image']); ?>
                                    <?php else: ?>
                                        <span class="bp-mini-image bp-mini-placeholder"></span>
                                    <?php endif; ?>
                                    <span class="bp-mini-label"><?php echo esc_html($cardLabel); ?></span>
                                </a>
                                <a class="bp-mini-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </li>
                        <?php endwhile; ?>
                    </ul>
                </div>
            </section>
            <?php wp_reset_postdata(); ?>
        <?php endforeach; ?>
    </div>
<?php else: ?>
    <div class="bp-section">
        <div class="bp-section-head">
            <h2>Najnowsze wpisy</h2>
        </div>
        <div class="bp-section-body" style="grid-template-columns:1fr;">
            <ul class="bp-list">
                <?php
                $fallback = new WP_Query([
                    'post_type' => 'post',
                    'posts_per_page' => 10,
                    'ignore_sticky_posts' => true,
                ]);
                while ($fallback->have_posts()): $fallback->the_post();
                ?>
                    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endwhile; wp_reset_postdata(); ?>
            </ul>
        </div>
    </div>
<?php endif; ?>

<?php get_footer(); ?>
