<?php get_header(); ?>
	<?php 
				
	$args = array(
	    'type' => 'listing',
	    'parent' => 0,          // Gets only top level categories
	    'orderby' => 'count',   // Orders the list by post count
	    'order' => 'desc',
	    'hide_empty' => 1,      // Hides categories with no posts
	    'number' => 6,         // No of categories to return
	    'taxonomy' => 'listing-category',
	    'no_found_rows' => true, // Skip SQL_CALC_FOUND_ROWS for performance (no pagination).

	);
	$top_categories = get_categories( $args ); // do not show any markup if we do not have any category at all.
	if (!empty($top_categories)){

	?>
		<!-- category-list block
			================================================== -->
		<section class="category-list">
			<div class="container">
				<h1><?php esc_html_e('Find listings by category', 'triptip'); ?></h1>
			</div>
			<div class="category-list__box owl-wrapper">
				<div class="owl-carousel" data-num="5">
					<?php foreach ( $top_categories as $cat ) { ?>
						<div class="item">

							<!-- category-post module -->
							<div class="category-post">
								<?php $banner = get_term_meta($cat->term_id, 'qkl_region_banner', true); ?>
								<?php if($banner!=''){ ?>
									<img class="category-post__image" src="<?php echo esc_url($banner); ?>" alt="<?php echo esc_attr($cat->name); ?>">
								<?php } ?>
								<div class="category-post__content">
									<h2 class="category-post__title">
										<a href="<?php echo get_term_link($cat->term_id); ?>"><?php echo esc_html($cat->name); ?></a>
									</h2>
									<a class="category-post__list-num" href="#">
										<?php echo esc_html($cat->count); ?> <?php if($cat->count>1){ esc_html_e('Listings', 'triptip'); }else{ esc_html_e('Listing', 'triptip'); }  ?>
									</a>
								</div>
							</div>
							<!-- end category-post module -->

						</div>
					<?php } ?>

					
				</div>
			</div>
		</section>
		<!-- End category-list block -->
	<?php } ?>

	<!-- explore-module
		================================================== -->
	<section class="explore">
		<div class="container">
			<div class="row">
				<div class="col-lg-8">
					<div class="explore__box">
						<h2 class="explore__filter-title">
							<?php
							if ( is_post_type_archive() ) {
							    ?>
							    <span><?php esc_html_e('Results For','triptip'); ?>:</span> <?php post_type_archive_title(); ?>
							    <?php
							}else{
							?>
								<span><?php esc_html_e('Results For', 'triptip'); ?>:</span> <?php esc_html_e('Trending Listings', 'triptip'); ?>
							<?php } ?>
							<a href="#" class="active">
								<i class="fa fa-list-ul" aria-hidden="true"></i>
							</a>
							<a href="#">
								<i class="fa fa-th" aria-hidden="true"></i>
							</a>
						</h2>
						<?php if(is_search()){ ?>
								<?php 
									$q = sanitize_text_field( $_GET['s'] );// get the searched query
							        $cat = !empty($_GET['in_cat']) ? sanitize_text_field($_GET['in_cat']) : 'all';
							        $loc = !empty($_GET['in_loc']) ? sanitize_text_field($_GET['in_loc']) : 'all';
							        $type = !empty($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : 'event';
							        $args = array(
									    'post_type'     => $type,
									    'post_status'   => 'publish',
									    's'             => $q,
									);
							        $tax_queries=array();

									if( $cat!='all') {
									      /*@todo; add option to the settings panel to let user choose whether to include result from children or not*/
									      $tax_queries[] = array(
									          'taxonomy'         => 'event-category',
									          'field'            => 'slug',
									          'terms'            => $cat,
									          'include_children' => true, /*@todo; Add option to include children or exclude it*/
									      );

									}

									if( $loc!='all' ) {
								        /*@todo; add option to the settings panel to let user choose whether to include result from children or not*/
								        $tax_queries[] = array(
								          'taxonomy'         => 'event-region',
								          'field'            => 'slug',
								          'terms'            => $loc,
								          'include_children' => true, /*@todo; Add option to include children or exclude it*/
								        );

									}

									if (!is_empty_v($tax_queries)){
									   $args['tax_query'] = $tax_queries;
									}
									  
									 $listings = new WP_Query($args);
								?>
								<div class="explore__wrap iso-call">
									<?php 
										$locations = array();

				                        if($listings->have_posts()) :
				                            while($listings->have_posts()) : $listings->the_post();

				                            $locations[] = array(get_the_permalink(),get_the_post_thumbnail_url('thumbnail'),get_the_title(), 'Coffee', 'Closed', '457 Court St, Brooklyn', '2', '7.9');
				                    ?>

											<div class="item">

												<div class="event-post">
													<div class="event-post__gal-box">
														<?php the_post_thumbnail('', ['class' => 'event-post__image']); ?>
														<?php if(get_post_meta(get_the_ID(), 'event_date', true)!=''){ ?>
															<span class="event-post__date"><?php echo esc_html(get_post_meta(get_the_ID(), 'event_date', true)); ?></span>
														<?php } ?>
														<?php if( function_exists('get_simple_likes_button') ) echo get_simple_likes_button(get_the_ID());?>
														
													</div>
													<div class="event-post__content">
														<p class="event-post__info">
															<i class="fa fa-clock-o" aria-hidden="true"></i>
															<span class="open"><?php echo esc_html(get_post_meta(get_the_ID(), 'event_open', true)); ?></span>
														</p>
														<h2 class="event-post__title">
															<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
														</h2>
														<p class="event-post__description">
															<?php  if( function_exists('qkl_event_category') ){ qkl_event_category(get_the_ID()); } ?>
															<span class="event-post__description-review">
																<i class="fa fa-comment-o" aria-hidden="true"></i>
																<?php comments_number( esc_html__('0 Reviews','triptip'), esc_html__('1 Review','triptip'), esc_html__('% Reviews','triptip') ); ?>
															</span>
														</p>

														<?php if(get_post_meta(get_the_ID(), '_triptip_address',true)!=''){ ?>
														<p class="event-post__address">
															<i class="fa fa-map-marker" aria-hidden="true"></i>
															<?php echo esc_html(get_post_meta(get_the_ID(), '_triptip_address',true)) ?>
														</p>
														<?php } ?>
													</div>
												</div>
												
											</div>	

									<?php endwhile; ?>
									<?php else : ?>
									    <div class="blog-post not-found">
									      <div class="alert alert-danger" role="alert">
									        <strong><?php esc_html_e('Nothing Found Here!','triptip'); ?></strong> <?php esc_html_e('Search with other keyword:', 'triptip') ?>
									      </div>
									    </div>
									<?php endif; ?>
								</div>

								<?php if($listings->found_posts>0){ ?>
									<div class="center-button">
										<a class="btn-default btn-default-red load-more" 
											data-cat="<?php echo esc_attr('all'); ?>" 
											data-loc="<?php echo esc_attr('all'); ?>" 
											data-load="<?php echo get_option( 'posts_per_page' ); ?>" 
											data-found="<?php echo esc_attr($listings->found_posts); ?>"
											href="#">
											<i class="fa fa-refresh"></i>
											<?php esc_html_e('Load More Listing', 'triptip'); ?>
										</a>
									</div>
								<?php } ?>

							<?php }else{ ?>

							<div class="explore__wrap iso-call list-version">
								<?php
			                        $i=1; if(have_posts()) :
			                          		while(have_posts()) : the_post(); 
			                    ?>

									<div class="item">

										<!-- place-post module -->
										<div class="place-post list-style">
											<div class="place-post__gal-box">
												<?php the_post_thumbnail('', ['class' => 'place-post__image']); ?>
												<?php triptip_post__ratings_get_average(get_the_ID()); ?>
												<?php if( function_exists('get_simple_likes_button') ) echo get_simple_likes_button(get_the_ID());?>
											</div>
											<div class="place-post__content">
												<?php if( function_exists('qkl_working_status') ){ qkl_working_status(); } ?>
												<h2 class="place-post__title">
													<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
												</h2>
												<p class="place-post__description">
													<?php 
														if( function_exists('qkl_event_category') ){ qkl_event_category(get_the_ID()); }
													?>

													<span class="place-post__description-review">
														<i class="fa fa-comment-o" aria-hidden="true"></i>
														<?php comments_number( esc_html__('0 Reviews','triptip'), esc_html__('1 Review','triptip'), esc_html__('% Reviews','triptip') ); ?>
													</span>
												</p>
												<p class="place-post__text">
													<?php echo triptip_excerpt($limit = 20); ?>
												</p>
												<?php if(get_post_meta(get_the_ID(), '_triptip_address',true)!=''){ ?>
													<p class="place-post__address">
														<i class="fa fa-map-marker" aria-hidden="true"></i>
														<?php echo esc_html(get_post_meta(get_the_ID(), '_triptip_address',true)) ?>
													</p>
												<?php } ?>
											</div>
										</div>
										<!-- end place-post module -->
										
									</div>

								<?php $i++; endwhile; ?>
								<?php else : ?>
								    <div class="blog-post not-found">
								      <div class="alert alert-danger" role="alert">
								        <strong><?php esc_html_e('Nothing Found Here!','triptip'); ?></strong> <?php esc_html_e('Search with other keyword:', 'triptip') ?>
								      </div>
								    </div>
								<?php endif; ?>

							</div>
							<div class="center-button">
								<a class="btn-default btn-default-red load-post-container" 
									data-load="#"  
									data-number="2" 
									href="#">
									<i class="fa fa-refresh"></i>
									<?php esc_html_e('Load More Listing','triptip'); ?>
								</a>
							</div>
						<?php } ?>

					</div>
				</div>

				<div class="col-lg-4">
					<div class="explore__filter">
						<?php echo do_shortcode('[advancesearch_listing]'); ?>

						<?php 
							if(is_active_sidebar('sidebar-list')){
								dynamic_sidebar('sidebar-list');
							}
						?>
						
					</div>
				</div>

			</div>
		</div>
	</section>
	<!-- End explore-module -->


<?php get_footer(); ?>