<?php
/**
 * The template for displaying search results pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package nbcore
 */
$default_product_search_display = 5;
$default_post_search_display    = 6;

$s = get_search_query();
$is_view_only_post_mode = isset( $_GET['view_only_post'] ) && ($_GET['view_only_post'] == 'true' ) ? true : false;

$post_args = array(
    'post_type'             => 'post',
    's'                     => $s,
    'ignore_sticky_posts'   => true,
);

$post_search_results        = new WP_Query($post_args);
$all_posts_count            = $post_search_results->found_posts;

$product_args = array(
    'post_type'             => 'product',
    's'                     => $s,
    'ignore_sticky_posts'   => true,
    'posts_per_page'        => '6'
);

$product_search_results     = new WP_Query($product_args);
$all_products_count         = $product_search_results->found_posts;

$total_count                = 0;

if($is_view_only_post_mode) {
    $total_count = $all_posts_count;
}
else {
    $current_posts_count        = $all_posts_count > $default_post_search_display ? $default_post_search_display : $all_posts_count;
    $current_products_count     = $all_products_count > $default_product_search_display ? $default_product_search_display : $all_products_count;
    $total_count                = $current_posts_count + $current_products_count;
}


get_header(); ?>

    <div class="container">
        <div class="row">
            <section id="primary" class="content-area">
                <main id="main" class="site-main content-search" role="main">

                    <?php
                    if ( $post_search_results->have_posts() || $product_search_results->have_posts() ) : ?>

                        <header class="page-header">
                            <h4>
                                <i class="icon-magnifying-glass"></i><?php echo get_search_query(); ?>
                            </h4>
                            <span><?php printf(esc_html('Showing %s results','aidoo'),'<strong>' . $total_count . '</strong>');
                            ?></span>
                            <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><i class="icon-home"></i><?php echo esc_html('back home', 'aidoo') ?></a>
                        </header><!-- .page-header -->

                        <?php if ( $post_search_results->have_posts() ):?>
                            <div class="blog <?php echo aidoo_blog_classes(); ?> masonry-3-columns" >
                                <div class="masonry">
                                    <?php
                                        $post_counter = 0;
                                        while ( $post_search_results->have_posts()) : $post_search_results->the_post();

                                            if( $post_counter < $default_post_search_display ) {
                                                get_template_part( 'template-parts/blog/masonry');
                                            }
                                            if( ! $is_view_only_post_mode ) {
                                                $post_counter++;
                                            }
                                        endwhile;   
                                                                
                                    ?>
                                </div>
                            </div>
                            <?php if( ! $is_view_only_post_mode ): ?>
                                <div class="view-all-button"><a href="<?php echo esc_url( add_query_arg( array( 'post_type' => 'post', 'view_only_post'=> 'true' ) ) ); ?>"><?php echo esc_html('View all posts result', 'aidoo') ?> </a></div>
                            <?php else:?>
                                <?php the_posts_navigation();?>
                            <?php endif;?>

                        <?php endif;?>
                        
                        
                        <?php if ( $product_search_results->have_posts() && ! $is_view_only_post_mode ) :?>
                            <div class="woocommerce wrap_product five-columns">
                                <div class="product-content products">
                                <?php
                                    $product_counter = 0;
                                    
                                    while ( $product_search_results->have_posts()) : $product_search_results->the_post();
                                    
                                        if( $product_counter < $default_product_search_display ) {
                                            wc_get_template_part( 'content', 'product' ); 
                                        }
                                        $product_counter++;

                                    endwhile;   
                                                            
                                ?>
                                </div>
                            </div>

                            <div class="view-all-button"><a href="<?php echo esc_url( add_query_arg( 'post_type' , 'product' ) ); ?>"><?php echo esc_html('View all products result', 'aidoo') ?> </a></div>
                        <?php endif;?>



                        <?php
                        wp_reset_postdata();
                        // the_posts_navigation();

                    else : ?>
                        <section class="no-results not-found">
                            <header class="page-header">
                                <h1 class="page-title"><?php esc_html_e('Nothing Found', 'aidoo'); ?></h1>
                            </header><!-- .page-header -->

                            <div class="page-content">
                                <?php
                                if (is_home() && current_user_can('publish_posts')) : ?>

                                    <p><?php printf(wp_kses(esc_html__('Ready to publish your first post? <a href="%1$s">Get started here</a>.', 'aidoo'), array('a' => array('href' => array()))), esc_url(admin_url('post-new.php'))); ?></p>

                                <?php elseif (is_search()) : ?>

                                    <p><?php esc_html_e('Sorry, but nothing matched your search terms. Please try again with some different keywords.', 'aidoo'); ?></p>
                                    <?php
                                    get_search_form();

                                else : ?>

                                    <p><?php esc_html_e('It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.', 'aidoo'); ?></p>
                                    <?php
                                    get_search_form();

                                endif; ?>
                            </div><!-- .page-content -->
                        </section><!-- .no-results -->
                        <?php
                    endif; ?>

                </main><!-- #main -->
            </section><!-- #primary -->
        </div>
    </div>


<?php
get_footer();
