<?php
$args = array(
    'layout'     => get_theme_mod( 'rebeccafood_blog_layout', 'blog-2cols-grid' ),
    'blog_cols'  => 'col-xl-9 col-lg-8 col-md-7',
    'image_size' => 'large',
    'words'      => 35
);

$sidebar = true;

if ( get_theme_mod( 'rebeccafood_blog_disable_sidebar' ) || ! is_active_sidebar( 'sidebar' ) ) {
    $sidebar = false;
}

if ( isset( $_GET['sidebar'] ) && $_GET['sidebar'] == 'no' ) {
    $sidebar = false;
}

if ( isset( $_GET['layout'] ) ) {
    $args['layout'] = $_GET['layout'];
}

if ( $args['layout'] == 'blog-2cols-grid' ) {
    $post_class = 'col-lg-6 post-small';
    $args['attr'] = array( 'sizes' => '(max-width: 575.98px) calc( 100vw - 24px ), (max-width: 767.98px) 516px, (max-width: 991.98px) 396px, (max-width: 1199.98px) 396px, (max-width: 1399.98px) 404px, 471px' );
}

if ( $args['layout'] == 'blog-standard' ) {
    $post_class = 'col-sm-12 post-large';
    $args['words'] = 50;
    $args['attr'] = array( 'sizes' => '(max-width: 575.98px) calc( 100vw - 24px ), (max-width: 767.98px) 516px, (max-width: 991.98px) 396px, (max-width: 1199.98px) 616px, (max-width: 1399.98px) 831px, 966px' );
}

if ( $args['layout'] == 'blog-3cols-grid' ) {
    $post_class = 'col-xl-4 col-sm-6 post-small';
    $args['words'] = 25;
    $args['attr'] = array( 'sizes' => '(max-width: 575.98px) calc( 100vw - 24px ), (max-width: 767.98px) 246px, (max-width: 991.98px) 336px, (max-width: 1199.98px) 456px, (max-width: 1399.98px) 356px, 416px' );
}

if ( ! $sidebar ) {
    $args['blog_cols'] = 'col-lg-12';
}

?>
<div class="row">
    <div class="<?php echo esc_attr( $args['blog_cols'] ); ?>">
    <?php
        if ( have_posts() ) {
            ?>
            <div class="rebeccafood-blog <?php echo esc_attr( $args['layout'] ); ?>">
                <div class="row">
                <?php
                    while ( have_posts() ) {
                        the_post();
                        ?>
                        <article <?php post_class( $post_class ); ?>>
                            <?php if ( get_the_post_thumbnail() ) { ?>
                            <a class="entry-image" href="<?php the_permalink(); ?>">
                                <?php the_post_thumbnail( $args['image_size'], $args['attr'] ); ?>
                            </a>
                            <?php } ?>
                            <div class="entry-meta">
                                <div class="entry-date">
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_date(); ?>
                                    </a>
                                </div>
                                <div class="entry-categories">
                                    <?php the_category( '<span class="sep">/</span>' ); ?>
                                </div>
                            </div>
                            <h2 class="entry-title">
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_title(); ?>
                                </a>
                            </h2>
                            <div class="entry-excerpt">
                                <?php echo wp_trim_words( get_the_excerpt(), $args['words'], '...' ); ?>
                            </div>
                        </article>
                        <?php
                    }
                ?>
                </div>
            </div>
            <?php
            the_posts_pagination();
        } else {
            get_template_part( 'template-parts/content', 'none' );
        }
    ?>
    </div>
    <?php if ( $sidebar ) { ?>
    <div class="col-xl-3 col-lg-4 col-md-5 sidebar">
        <?php
            get_sidebar();
        ?>
    </div>
    <?php } ?>
</div>
