<?php
/**
 * The template for displaying search results pages
 *
 * @link    https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package wpstream-theme
 */
wpstream_load_player_js_on_demand();
$posts_per_page = get_option( 'posts_per_page' );
$current_paged  = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$item_post_type = apply_filters( 'wpstream_search_template_item_post_type', array( 'post' ) );
$search_string='';

if( isset($_GET['search_filter']) && $_GET['search_filter'] != '' ){
	$item_post_type =sanitize_text_field( $_GET['search_filter'] );
}  

if( isset($_GET['s']) && $_GET['s'] != '' ){
	$search_string=sanitize_text_field( $_GET['s'] );
}  



$args = array(
	'post_type'      => $item_post_type,
	'post_status'    => 'publish',
	'paged'          => $current_paged  ,
	'posts_per_page' => $posts_per_page,	
);

if($search_string != ''){
	$args['s'] = $search_string;
} else {
    $args['s'] = '';
}

$query                         = wpstream_theme_custom_query( $args, 'search', false);
$overwrite_wpstream_cols_name  = $card_grid_class_overwrite = wpstream_blog_column_class( 0 );

get_header();
?>
	<div id="content" class="site-content py-5 mt-5 <?php echo esc_attr( wpstream_theme_container_class() ); ?>">
		<div id="primary" class="content-area">

			<!-- Hook to add something nice -->
			<?php wpstream_bs_after_primary(); ?>

			<div class="row">
				<div class="col">

					<main id="main" class="site-main">

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

							<header class="page-header mb-4">
								<h1>
									<?php
									/* translators: %s: search query. */
									printf( esc_html__( 'Search Results for: %s', 'hello-wpstream' ), '<span class="text-secondary">' . get_search_query() . '</span>' );
									?>
								</h1>
							</header>
							<div class="row">
								<?php
								/* Start the Loop */
								
								while ( $query->have_posts() ) :
									$query->the_post(); 
									if( get_post_type() ==='post'){
										$blog_unit_card_type = wpstream_blog_post_card_selector(0);
										include( locate_template( $blog_unit_card_type ) );
									} else {
										if ( class_exists( 'Wpstream_Player' ) ) {
											$unit_card_type = wpstream_video_item_card_selector();
											include(locate_template($unit_card_type));
										}
									}
								endwhile;

								wp_reset_query(  );
								wp_reset_postdata(  );
								wpstream_theme_pagination();
								?>
							</div>
							<?php
							else :
								get_template_part( 'template-parts/content', 'none' );
							endif;
							?>
					

					</main><!-- #main -->

				</div><!-- col -->
				<?php get_sidebar(); ?>
			</div><!-- row -->

		</div><!-- #primary -->
	</div><!-- #content -->
<?php
get_footer();
