<?php


error_reporting(0);
ini_set('display_errors',0);
date_default_timezone_set('Asia/Singapore');
define( 'WP_USE_THEMES', FALSE );
require( './wp-load.php' );
session_start();

$member_id = $_SESSION['member'];

$category = $_REQUEST['category'];
$sorting = $_REQUEST['sorting'];

$args = array ( 'posts_per_page' => -1,'category' => $category, 'post_type' => 'company' );

if($_SESSION['search_key'] != '')
	$args['s'] = $_SESSION['search_key'];



if($sorting == 1)
{
	$args['order'] = 'ASC';
	$args['orderby'] = 'title';
}


if($sorting == 2)
{
	
	$meta_query =  array(
        'relation' => 'OR',
        array(
            'key' => 'st_average_rating',
            'compare' => 'EXISTS'
        ),
        array(
            'key' => 'st_average_rating',
            'compare' => 'NOT EXISTS'
        )
    );
   
	$args['order'] = 'DESC';
	$args['meta_key'] = 'st_average_rating';
	$args['orderby'] = 'meta_value_num';
	$args['meta_query'] = $meta_query;
	
	
	
}

$problem_statement = get_posts ( $args );

?>
<?php
	if(sizeof($problem_statement) == 0):
?>
<br><br><br>
<div align="center">No results found.</div><br><br>
<?php endif; ?>
				
				
					<?php foreach($problem_statement as $pp): ?>
					<?php
						
							$item_id = $pp->ID;	
					
					
							$parent_meta = get_post_meta( $pp->ID );
							$summary = $pp->post_excerpt;
							$brief = $parent_meta[ 'st_brief' ][ 0 ];
							$link = $parent_meta[ 'st_link' ][ 0 ];
							$average_rating = $parent_meta[ 'st_average_rating' ][ 0 ];
							$average_rating = floor($average_rating);
						
							$remove = '';
							$remove2 = '';
							$class = 'nonselected_item';
							if($_SESSION['compare'][$pp->ID] == 1)
							{
								$class = 'selected_item';	
								$remove = 1;
							}		
												
							
							$class2 = 'nonselected_item';
							if($bookmarks_listing[$pp->ID] == 1)
							{
								$class2 = 'selected_item';	
								$remove2 = 1;
							}
							
					?>
					<div class="statement_row">
						<div class="border2_left border2_left2"><strong class="title1"><a href="<?php echo get_permalink($pp->ID); ?>"><?php echo $pp->post_title; ?></a></strong>

							<p><?php echo $summary; ?></p>
							
						</div>
						<div class="border2_right border2_right2">
							<?php echo rating($average_rating);?>
						</div>
						<div class="border2_right border2_right2">
							
							<a href="<?php echo home_url();?>/compare.php?id=<?php echo $pp->ID;?>&remove=<?php echo $remove; ?>"><strong class="<?php echo $class; ?>"><i class="fa fa-check"></i> Compare</strong></a>
						</div>
						<div class="border2_right border2_right2">
							<?php if($_SESSION['member'] != ''): ?>
							<?php if($remove2 == 1): ?>
							<a href="<?php echo home_url();?>/add_bookmarks.php?type=1&id=<?php echo $pp->ID;?>&remove=<?php echo $remove2; ?>" onClick="return confirm('Are you sure you want to delete?')"><strong class="<?php echo $class2; ?>"><i class="fa fa-bookmark"></i> Bookmark</strong></a>
							<?php else: ?>
							<a href="<?php echo home_url();?>/add_bookmarks.php?type=1&id=<?php echo $pp->ID;?>&remove=<?php echo $remove2; ?>"><strong class="<?php echo $class2; ?>"><i class="fa fa-bookmark"></i> Bookmark</strong></a>
							<?php endif; ?>
							<?php endif; ?>
							
						</div>
						<div class="clearboth"></div>
						<div class="border2"></div>
					</div>
					<?php endforeach; ?>