<?php 

function search_results_cb() {
	if(!isset($_POST['search_term'])) return_method("dashboard");
	$args = $_POST;
	$session = Session::getInstance();
	$args['limit'] = isset($args['limit']) ? $args['limit'] : 20;
	$session->lastsearchresultstop = $args['limit'];
	$session->lastsearchresult = array();
	$search = (isset($args['table'])) ? new search($args['table']) :  new search("post");
	$args['search_in'] = base64_decode($args['search_in']);
	$args['search_in'] = is_serialized($args['search_in']) ? unserialize($args['search_in']) : $args['search_in'];
	$search->search_in = $args['search_in'];
	$search->limit = $args['limit'];
	if(isset($args['where'])) {
		$args['where'] = base64_decode($args['where']);
		$args['where'] = unserialize($args['where']);
		$search->where = $args['where'];
	}
	if(isset($args['compare'])) {
		$args['compare'] = base64_decode($args['compare']);
		$args['compare'] = unserialize($args['compare']);
		$search->compare = $args['compare'];
	}
	if(isset($args['order_by'])) {
		$search->order_by = $args['order_by'];
	}
	if(isset($args['order'])) {
		$search->order = $args['order'];
	}
	if(isset($args['offset'])) {
		$search->offset = $args['offset'];
	}
	$args['search-result-container-open'] = base64_decode($args['search-result-container-open']);
	$args['search-result-container-close'] = base64_decode($args['search-result-container-close']);
	$cryptargs = base64_encode(serialize($args));
	$args['id'] = "search-result-".$args['id'];
	?>
	<div class="row mb-4">
		<div class="col-md-12">
			<?php do_action("search_form", $args); ?>
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			<?php 
				do_action("before_search_results_".$args['id'], $args);
				$search_results = $search->get_results($args['search_term']);
				?>
				<div id="display-search-result" class="<?php echo $args['search-results-container-class'] ?>">
				<?php
				if(!_count($search_results)) {
					echo "No result found...";
					do_action("after_search_result", $args['search_term']);
					return;
				}
				if(!is_array($search_results)) {
					echo "No result found...";
					do_action("after_search_result", $args['search_term']);
					return;
				}
				echo $args['search-result-container-open'];
				foreach ($search_results as $search_result) {
					echo $args['template']($search_result);
				}
				echo $args['search-result-container-close'];
				?>
				</div>
				<?php
			 ?>
			 <div id="load-more-container">
			 	<a href="javaScript:void(0);" id="load-more-btn" data-args="<?php echo $cryptargs ?>">Load More Results</a>
			 </div>
			 <?php do_action("after_search_result", $args['search_term']); ?>
		</div>
	</div>
	<?php
	global $post_meta;
	add_page_javascript('app/assets/js/page-scripts/search-results.js');
}