<?php

require_once("reputation_management_macros.php");

/**************************** Code to show list of review ********************/
global $wpdb,$post;
$page_id = $post->ID;
$key = get_option( 'rb_api_key');
$limit = get_option( 'rb_no_of_reviews');
$internal = get_option( 'internal_review');
$yelp =	get_option( 'yelp');
$google_plus	= get_option( 'google_plus' );
$error_behavior = get_option('behavior_on_error');

if (isset($_GET['offset']) && $_GET['offset']) {
    $offset = $_GET['offset'];
} else {
    $offset = 0;
}
	$tReviewOptions = array(
		'internal',
		'yelp',
		'google',
		'yahoo',
		'citysearch',
		'opentable',
		'facebook',
		'foursquare',
		'insiderpages',
		'zillow',
        'Avvo',
        'Booking',
        'Caring',
        'Cars',
        'CreditKarma',
        'CustomerLobby',
        'DealerRater',
        'Expedia',
        'GoldenReviews',
        'HomeAdvisor',
        'Houzz',
        'JudysBook',
        'Kudzu',
        'MenuPages',
        'Orbitz',
        'OurParents',
        'RateMds',
        'RealPatientRatings',
        'Rent',
        'RentersVoice',
        'SeniorAdvisor',
        'SeniorHomes',
        'Snap21',
        'Travelocity',
        'Trip',
        'TripAdvisor',
        'Trulia',
        'UCompareHealthCare',
        'Vitals',
        'YellowPages',
        'ZocDoc',
	);

	$tQuery = [];

	foreach ($tReviewOptions as $Option) {
		// Hacky I know, but I'm not aware of the reliance on the option being named internal_review / google_plus
		if($Option == 'internal')
			$tQuery[$Option] = get_option('internal_review');
		elseif ($Option == 'google')
			$tQuery[$Option] = get_option('google_plus');
		else
			$tQuery[$Option] = get_option($Option);
	}
$url = REPUTATION_API_URL."apiKey=$key&noOfReviews=$limit&offset=".$offset.'&'.http_build_query($tQuery);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3000);
$res = curl_exec($ch);
if($res) {
    $data = json_decode($res);
    if(isset($data->errorMessage))
    	exit($data->errorMessage);

    $info = $data->business_info;
    $reviews = $data->reviews;
    $total = $info->total_rating->total_no_of_reviews;
    $prev = max($offset - $limit, 0);
    $next = min($offset + $limit, floor($total / $limit) * $limit);
    $show_next = $next < floor($total / $limit) * $limit;
?>
    <div itemtype="http://schema.org/LocalBusiness" itemscope="">
        <div class="top-section-wp">
            <div class="business_name" itemprop="name">
                <?php echo $info->business_name; ?>
            </div>
            <div class="business_add">
                <?php echo $info->business_address; ?>
            </div>
            <div class="business_add">
                <?php echo $info->business_phone; ?>
            </div>
            <div class="total_rating_section" itemtype="http://schema.org/AggregateRating" itemscope=""
                 itemprop="aggregateRating">
                <span class="total-rating"><?php echo $info->total_rating->total_avg_rating; ?>
                    <meta content="<?php echo $info->total_rating->total_avg_rating; ?>" itemprop="ratingValue">
                </span>
                <label class='rating'>
                    <img src='<?php echo WP_PLUGIN_URL?>/reputation_management/images/<?php echo round($info->total_rating->total_avg_rating); ?>star.png'>
                </label>
                <span class="total-review"><?php echo $info->total_rating->total_no_of_reviews; ?> <?php echo REVIEWS_LABEL?>
                    <meta content="<?php echo $info->total_rating->total_no_of_reviews; ?>" itemprop="ratingCount">
                </span>
            </div>
            <a class="write-review" target="_blank" href="<?php echo $info->external_url; ?>"><?php echo WRITE_A_REVIEW_LABEL?></a>
        </div>
        <div>
            <?php if(count($reviews)) { ?>
            <div class="slider1">
                <?php foreach($reviews as $r) { ?>
					<div class='slide' itemprop="review" itemscope itemtype="http://schema.org/Review">
						<div class='logo-block'>
							<label class='review_logo'><a target='_blank' href='<?php echo $r->customer_url; ?>'>
                                    <?php
                                        $file_ext = $r->review_source == 'facebook' ? '_small' : '';

                                    ?>
								<img src='<?php echo WP_PLUGIN_URL?>/reputation_management/images/<?php echo $r->review_source; ?>logo<?=$file_ext; ?>.png'></a></label>
						</div>
						<div class='dec-block'>
							<label class='cust-name' itemprop="author">
								<a target='_blank' href='<?php echo $r->customer_url; ?>'>
									<?php echo $r->customer_name; ?>
									<?php echo $r->customer_last_name; ?>
								</a>
							</label>
							<div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
								<meta itemprop="worstRating" content = "1">
								<meta itemprop="ratingValue" content = "<?php echo $r->rating; ?>">
								<meta itemprop="bestRating" content = "5">
								<a target='_blank' href='<?php echo $r->customer_url; ?>' style="color: inherit;">
									<label class='rating'><img src='<?php echo WP_PLUGIN_URL?>/reputation_management/images/<?php echo $r->rating; ?>star.png'></label>
									<label class='date' itemprop="datePublished" content="<?php echo date('m/d/Y', strtotime($r->date_of_submission)); ?>"><?php echo date('m/d/Y', strtotime($r->date_of_submission)); ?></label>
									<label class='dec' itemprop="description"><?php echo $r->description; ?></label>
								</a>
							</div>
						</div>
					</div>
				<?php } ?>
            </div>
            <?php } ?>
        </div>
        <div class='pagination'>
        <?php if($prev) { ?>
            <a class='viewevent' id="reputation_management-prev" href="?offset=<?php echo $prev; ?>">Previous</a>
        <?php } ?>
        <?php if($show_next) { ?>
            <a class='viewevent' id="reputation_management-next" href="?offset=<?php echo $next; ?>">Next</a>
        <?php } ?>
        </div>
    </div>
<?php
}
wp_enqueue_style('review_all_css',plugins_url( '/css/review_all.css' , __FILE__ ));
wp_enqueue_script('slide_js',plugins_url( '/js/reputation_management_init.min.js' , __FILE__ ));
