<?php
global $anps_page_data, $row_inner; 
$meta = get_post_meta(get_the_ID());
$num_of_sidebars = 0;
$left_sidebar = 0;
if (isset($meta['sbg_selected_sidebar'])) {
    $left_sidebar = $meta['sbg_selected_sidebar'];
    if($left_sidebar[0] != "0") {
        $num_of_sidebars++;   
    }
}
$right_sidebar = 0;
if (isset($meta['sbg_selected_sidebar_replacement'])) {
    $right_sidebar = $meta['sbg_selected_sidebar_replacement'];
    if($right_sidebar[0] != "0") {
        $num_of_sidebars++;   
    }
}
$page_value = get_post_meta(get_the_ID(), $key ='anps_page_type', $single = true ); 
$page_cat = get_post_meta(get_the_ID(), $key ='anps_page_cat', $single = true );
$page_blog = get_post_meta(get_the_ID(), $key ='anps_page_blog', $single = true );
if($page_blog=="on") {
    global $blog_columns;
    switch($page_value) {
        case "1": 
            $blog_type = "content"; 
            $class_blog = "blog-loop style-2";
            break;
        case "2": 
            $blog_type = "content-blog-grid"; 
            $blog_columns = "col-md-4";
            $class_blog = "style-2";
            break;
        case "3": 
            $blog_type = "content-blog-grid"; 
            $blog_columns = "col-md-3";
            $class_blog = "style-2";
            break;
        case "4": 
            $blog_type = "content-blog-masonry"; 
            $blog_columns = " col-md-4";
            $class_blog = "style-2";
            break;
        case "5": 
            $blog_type = "content-blog-masonry"; 
            $blog_columns = " col-md-3"; 
            $class_blog = "style-2";
            break;
        default :
            $class_blog = "style-2";
            $blog_type = "content";
    }
    $categories = "";
    if($page_cat) {
        $categories = implode(",", $page_cat);
    }
    get_query_var('paged') > 1 ? $current = get_query_var('paged') : $current = 1;
    $args = array(
    	'cat' => $categories,
    	'post_type' => 'post',
    	'post_status' => 'publish',
        'paged' => $current 
   );
    $pre = "";
    $after = "";
    $prev_text = __('Prev', ANPS_TEMPLATE_LANG);
    $next_text = __('Next', ANPS_TEMPLATE_LANG);
    /* Pagination style */
    if(isset($page_data) && $page_data['pagination_style']=="style-2") {
        $pre = "<div class='style-2'>";
        $after = "</div>";
        $prev_text = "";
        $next_text = "";
    }
    
    $posts = new WP_Query( $args );

    $pagination = array(
    	'base' => @esc_url(add_query_arg('page','%#%')),
    	'format' => '',
    	'total' => $posts->max_num_pages,
    	'current' => $current,
    	'show_all' => false,
        'prev_next' => true,
        'prev_text'    => $prev_text,
        'next_text'    => $next_text,
    	'type' => 'list',
	);
    
    $content = "";   
    if($posts->have_posts()) : 
        global $counter_blog;
        $counter_blog = 1;
        while($posts->have_posts()) : 
            $posts->the_post(); 
            ob_start();
            get_template_part( $blog_type, get_post_format() );
            $counter_blog++;
            $content .= ob_get_clean(); 
        endwhile;   
        if( $wp_rewrite->using_permalinks() ) {
            $pagination['base'] = user_trailingslashit( trailingslashit( esc_url(remove_query_arg('s',get_pagenum_link(1)) ) ) . 'page/%#%/', 'paged');
        }
        if( !empty($wp_query->query_vars['s']) ) {
            $pagination['add_args'] = array('s'=>get_query_var('s'));
        }
        $content .= $pre;
        $content .= paginate_links( $pagination );
        $content .= $after;
        wp_reset_postdata();    
    else :
        $content .= "<h2>".__('Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', ANPS_TEMPLATE_LANG)."</h2>";
    endif;
} else {
    $class_blog = "";
}
?>

<?php if ($num_of_sidebars > 0): $row_inner = true; ?>
<section class="container<?php echo $class_blog; ?>">
    <div class="row">
<?php endif; ?>
        <?php      
            if($page_blog!="on") : 
                while (have_posts()) : the_post(); 
                    //if( ! strpos('pre' . get_the_content(), 'vc_row') ) {
                        echo '<section class="container"><div class="row">';
                    //}

                    if ($left_sidebar[0] != "0" && $left_sidebar[0] != null): ?>
                        <aside class="sidebar col-md-<?php if($num_of_sidebars == 1) { echo "3"; } else if($num_of_sidebars == 2) { echo "3"; } ?>">
                            <ul class="widgets">
                                <?php dynamic_sidebar($left_sidebar[0]); wp_reset_query(); ?>
                            </ul>
                        </aside>   
                    <?php endif; ?>

                    <?php if($num_of_sidebars == 0 && strpos('pre' . get_the_content(), 'vc_row')): ?>
                        <?php the_content(); ?>
                    <?php else: ?>
                        <div class='col-md-<?php echo 12-$num_of_sidebars*3; ?>'><?php the_content(); ?></div>
                    <?php endif; ?>

                    <?php if (isset($right_sidebar[0]) && $right_sidebar[0] != "0"): ?>
                        <aside class="sidebar col-md-<?php if($num_of_sidebars == 1) { echo "3"; } else if($num_of_sidebars == 2) { echo "3"; } ?>">
                            <ul class="widgets">
                                <?php dynamic_sidebar($right_sidebar[0]); ?>
                            </ul>
                        </aside>   
                    <?php endif;

                 //   if( ! strpos('pre' . get_the_content(), 'vc_row') ) {
                        echo '</div></section>';
                 //   }
                endwhile; 
           else :
               //if( ! strpos('pre' . get_the_content(), 'vc_row') ) {
                    echo "<section class='$class_blog'><div class='container'><div class='row'>";
               // }
                if($page_value=="2" || $page_value=="3") {
                    echo "<div class='blog-loop columns'>";
                }
                if($page_value=="4" || $page_value=="5") {
                    echo "<div class='blog-loop columns masonry'>";
                }
                if ($left_sidebar[0] != "0" && $left_sidebar[0] != null): ?>
                    <aside class="sidebar col-md-<?php if($num_of_sidebars == 1) { echo "3"; } else if($num_of_sidebars == 2) { echo "3"; } ?>">
                        <ul class="widgets">
                            <?php dynamic_sidebar($left_sidebar[0]); wp_reset_query(); ?>
                        </ul>
                    </aside>   
                <?php endif; ?>

                <?php if($num_of_sidebars == 0 && strpos('pre' . get_the_content(), 'vc_row')): ?>
                    <?php echo $content; ?>
                <?php else: ?>
                    <div class='col-md-<?php echo 12-$num_of_sidebars*3; ?>'><?php echo $content; ?></div>
                <?php endif; ?>

                <?php if (isset($right_sidebar[0]) && $right_sidebar[0] != "0"): ?>
                    <aside class="sidebar col-md-<?php if($num_of_sidebars == 1) { echo "3"; } else if($num_of_sidebars == 2) { echo "3"; } ?>">
                        <ul class="widgets">
                            <?php dynamic_sidebar($right_sidebar[0]); ?>
                        </ul>
                    </aside>   
                <?php endif;
                if($page_value=="2" || $page_value=="3" || $page_value=="4" || $page_value=="5") {
                    echo "</div>";
                }
              //  if( ! strpos('pre' . get_the_content(), 'vc_row') ) {
                    echo '</div></div></section>';
               // }
               
            endif; // end of the loop. 
        ?>
<?php if ($num_of_sidebars > 0): ?>
    </div>
</section>
<?php endif; ?>