<?php
    $crt_builder_with_elementor_enable_latest = get_theme_mod('crt_manage_enable_latest_section');
    if(!$crt_builder_with_elementor_enable_latest) {
        return;
    }
    $crt_builder_with_elementor_latest_post_per_page = get_theme_mod('crt_manage_latest_post_per_page', 2);
    $crt_builder_with_elementor_latest_post_order = get_theme_mod('crt_manage_latest_post_order', 'DESC');
    $crt_builder_with_elementor_sidebar_position = get_theme_mod('crt_manage_post_latest_sidebar_position', 'sidebar-1');
    $crt_builder_with_elementor_headline = get_theme_mod('crt_manage_latest_headline', __( 'Blog', 'crt-builder-with-elementor' ));
    $tax = get_theme_mod('crt_manage_latest_category_one_list');
    $crt_builder_with_elementor_button_text = get_theme_mod('crt_manage_latest_button_text', __( 'View All', 'crt-builder-with-elementor' ));
    $crt_builder_with_elementor_button_url = get_theme_mod('crt_manage_latest_button_url', '#');
?>

<section id="post-latest" class="post-latest mt-3 mt-md-4">
    <div class="container position-relative">
        <?php crt_manage_section_link( 'Post Latest' ); ?>
        <div class="">
            <div class="row ">
                <?php
                    $crt_builder_with_elementor_args = crt_builder_with_elementor_archive_layout();
                    $crt_builder_with_elementor_col_one = $crt_builder_with_elementor_args['col_one'];
                    $crt_builder_with_elementor_col_two = $crt_builder_with_elementor_args['col_two'];
                    $crt_builder_with_elementor_layout = $crt_builder_with_elementor_args['layout'];
                    $crt_builder_with_elementor_grid = str_contains($crt_builder_with_elementor_layout, 'masonry');
                ?>
                <div class="<?php echo esc_attr($crt_builder_with_elementor_col_one); ?>">
                    <div class="heading-default d-flex justify-content-between align-items-center">
                        <h2 class="heading-char-animation"><?php echo esc_html($crt_builder_with_elementor_headline); ?></h2>
                        <span class="ms-auto"><a href="<?php echo esc_url($crt_builder_with_elementor_button_url); ?>"><?php echo esc_html($crt_builder_with_elementor_button_text); ?></a></span>
                    </div>
                    <div>
                        <div class="<?php echo esc_attr($crt_builder_with_elementor_grid ? 'grid ' . $crt_builder_with_elementor_layout:'row') ?>">
                            <div class="grid__col-sizer"></div>
                            <?php
                                $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
                                $crt_builder_with_elementor_post_latest_args = array(
                                    'paged'          => $paged,
                                    'posts_per_page' => ( $crt_builder_with_elementor_latest_post_per_page ) ? $crt_builder_with_elementor_latest_post_per_page : 2,
                                    'order' => isset($crt_builder_with_elementor_latest_post_order[0]) ? $crt_builder_with_elementor_latest_post_order[0]:'DESC'
                                );
                                $loop = new WP_Query( $crt_builder_with_elementor_post_latest_args );
                                if ( $loop->have_posts() ) :
                                    while ( $loop->have_posts() ) :
                                        $loop->the_post();
                                        get_template_part( 'template-parts/content', $crt_builder_with_elementor_layout, array('sidebar' => $crt_builder_with_elementor_col_two) );
                                    endwhile;
                                endif;
                                wp_reset_postdata();
                            ?>
                        </div>
                    </div>

                    <?php crt_builder_with_elementor_pagination_custom( $paged, $loop->max_num_pages); ?>
                </div>
                <div class="<?php echo esc_attr($crt_builder_with_elementor_col_two); ?>">
                    <?php if($crt_builder_with_elementor_col_two != 'd-none'): ?>
                        <aside id="secondary" class="widget-area">
                            <?php dynamic_sidebar( $crt_builder_with_elementor_sidebar_position ); ?>
                        </aside>
                    <?php endif; ?>
                </div>
                <?php if($crt_builder_with_elementor_col_two != 'd-none'): ?>
                    <div class="br-col br-col66 br-sm-col-none"></div>
                <?php endif; ?>
            </div>
        </div>

    </div>
</section>