<?php
$section_title = rt_get_option('frontpage_post_archive_title', 'Latest Posts');
$section_desc = rt_get_option('frontpage_post_archive_desc');

$archive_slug = basename(get_permalink(get_option('page_for_posts')));
$pagination_style = rt_get_option('frontpage_post_archive_pagination_style', 'number');

$args = array(
    'post_type' => 'post',
    'posts_per_page' => get_option('posts_per_page'),
    'post_status' => 'publish',
);

$the_query = new WP_Query($args);
?>

<section id="frontpage-archive" class="frontpage-section has-global-padding">
    <div class="site-container">

        <?php if ($section_title || $section_desc) : ?>
            <div class="rt-block-heading is-layout-constrained text-center">
                <?php if ($section_title) : ?>
                    <h2 class="rt-block-heading__title"><?php echo esc_html($section_title); ?></h2>
                <?php endif; ?>

                <?php if ($section_desc) : ?>
                    <div class="rt-block-heading__desc"><?php echo esc_html($section_desc); ?></div>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <?php if ($the_query->have_posts()) : ?>

            <div class="rt-posts">
                <div id="post_archive" class="rt-post-archive grids grids-1 md:grids-3">
                    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
                        <?php rt_get_template_part('post/post'); ?>
                    <?php endwhile ?>
                </div>

                <?php echo rt_get_pagination(array_merge([
                    'target' => 'post_archive',
                    'template_part' => 'template-parts/post/post',
                    'pagination_style' => $pagination_style,
                    'total' => $the_query->max_num_pages,
                    'total_page' => $the_query->max_num_pages,
                    'post_total' => $the_query->found_posts,
                    'format' => "/{$archive_slug}/page/%#%",
                    'current' => max(1, get_query_var('paged')),
                ], $args)); ?>

                <?php wp_reset_postdata(); ?>

            </div>
        <?php else : ?>
            <p><?php _e('No posts found', 'rt_theme'); ?></p>
        <?php endif ?>


    </div>
</section>