<?php

/********************** Code for admin setting page ************************/
/********************** Adding js/css file *********************************/
wp_enqueue_script( 
    'reputation_management_admin_validate'
    ,plugins_url( '/js/jquery.validate.js' , __FILE__ )
);
wp_enqueue_script( 
    'reputation_management_admin'
    ,plugins_url( '/js/reputation_management_admin.js' , __FILE__ )
);
global $wpdb;
$error = array();
$success = '';
/******************** Code to API settings form post *********************/
if(isset($_POST['formtype']) && $_POST['formtype'] === md5('reputation_management')) {
    extract($_POST);
    if(empty($key)) {
        $error[] = REPOTATION_API_KEY_INSERT_ERROR;
    }
    if(empty($reviews)) {
        $error[] = REPOTATION_NO_OF_REVIEWS_INSERT_ERROR;
    }
    if(empty($scroll_limit)) {
        $error[] = REPOTATION_SCROLL_LIMIT_INSERT_ERROR;
    } elseif($scroll_limit > 10) {
        $error[] = REPOTATION_SCROLL_LIMIT_EXCEED_ERROR;
    }
    if(empty($show_review)) {
        $error[] = REPOTATION_THERSHOLD_INSERT_ERROR;
    }
    if(empty($error)) {
        update_option( 'rb_api_key', $key );
        update_option( 'rb_scroll_limit', $scroll_limit );
        update_option( 'rb_no_of_reviews', $reviews );
        update_option('behavior_on_error', $behavior_on_error);

		$tReviewOptions = array(
			'internal_review',
			'yelp',
			'google_plus',
			'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',
		);

		foreach($tReviewOptions as $Option) {
			if(array_key_exists($Option, $show_review))
				update_option($Option, 1);
			else
				update_option($Option, 0);
		}

        $success = REPOTATION_API_SETTINGS_CHANGE_SUCCESS;
    }
}

/****************************** Showing Error/Success Message ******************/

if($error) {
    echo '<ul class="error_message">';
    foreach($error as $err) {
        echo '<li>'.$err.'</li>';
    }
    echo '</ul>';
    $error = '';
}
if($success) {
    echo    '<ul class ="success_message">
                <li>'.$success.'</li>
            </ul>';
}
?>
<!---------------------------- Code to showing api setting form ----------------->
<div id = "reputation_management_div" >
    <div><?php _e(REVIEW_MODULE_INSTRUCTION);?></div><br>
    <form method="post" id = "reputation_management_form">
        <div class="form-group">
            <div class="labelbox"><label><?php _e(REPOSITE_BUG_KEY);?><span>*</span> :</label></div>
            <div class="inputbox">
                <input type="text" placeholder="API Key" name="key" id="key" tabindex = "2" maxlength="32" value="<?php echo get_option( 'rb_api_key');?>"/>
                <span>
                    <label for="key" generated="true" class="error"></label>
                </span>
            </div>
        </div>
        <div class="form-group">
            <div class="labelbox">
                <label><?php _e(REPOSITE_BUG_SIDEWIDGET_SCROLL_LIMIT);?><span>*</span> :</label>
            </div>
            <div class="inputbox">
                <input type="text" placeholder="API Review Scroll Limit" name="scroll_limit" id="scroll_limit" maxlength="2" tabindex = "2" value="<?php echo get_option( 'rb_scroll_limit');?>"/>
                <span>
                    <label for="scroll_limit" generated="true" class="error"></label>
                </span>
                 
            </div>
        </div>
        <div class="form-group">
            <div class="labelbox">
                <label><?php _e(REPOSITE_BUG_NO_OF_REVIEWS);?><span>*</span> :</label>
            </div>
            <div class="inputbox">
                <input type="text" placeholder="API Review Scroll Limit" name="reviews" id="reviews" maxlength="2" tabindex = "2" value="<?php echo get_option( 'rb_no_of_reviews');?>"/>
                <span>
                    <label for="reviews" generated="true" class="error"></label>
                </span>
            </div>
        </div>
        <div class="form-group">
            <div class="labelbox">
                <label><?php _e(RL_BEHAVIOR_ON_CURL_ERROR);?><span>*</span> :</label>
            </div>
            <div class="inputbox">
                <select name="behavior_on_error">
                    <option <?php echo get_option('behavior_on_error') == 'show' ? "selected=selected" : ""; ?>  value="show"><?php _e(RL_SHOW_ERROR_MSG);?></option>
                    <option <?php echo get_option('behavior_on_error') == 'blank' ? "selected=selected" : ""; ?> value="blank"><?php _e(RL_SHOW_BLANK);?></option>
                    <option <?php echo get_option('behavior_on_error') == 'cache' ? "selected=selected" : ""; ?> value="cache" ><?php _e(RL_SHOW_CACHED);?></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <div class="labelbox">
                <label><?php _e(ALLOW_REVIEW);?><span>*</span> :</label>
            </div>
            <div class="inputbox">
            	<table>
            		<tbody>
            			<tr>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[internal_review]" id="internal_review" tabindex="6" value="<?php echo get_option('internal_review'); ?>" <?php echo (get_option('internal_review')) ? "checked=checked" : ""; ?> />
									<span><?php _e(INTERNAL); ?></span>
								</label>
							</td>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[yelp]" id="yelp" tabindex="7" value="<?php echo get_option('yelp'); ?>" <?php echo (get_option('yelp')) ? "checked=checked" : ""; ?> />
									<span><?php _e(YELP); ?></span>
								</label>
							</td>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[google_plus]" id="google_plus" tabindex="8" value="<?php echo get_option('google_plus'); ?>" <?php echo (get_option('google_plus')) ? "checked=checked" : ""; ?> />
									<span><?php _e(GOOGLE_PLUS); ?></span>
								</label>
							</td>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[yahoo]" id="yahoo" tabindex="8" value="<?php echo get_option('yahoo'); ?>" <?php echo (get_option('yahoo')) ? "checked=checked" : ""; ?> />
									<span><?php _e(YAHOO); ?></span>
								</label>
							</td>
            			</tr>

            			<tr>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[citysearch]" id="citysearch" tabindex="8" value="<?php echo get_option('citysearch'); ?>" <?php echo (get_option('citysearch')) ? "checked=checked" : ""; ?> />
									<span><?php _e(CITYSEARCH); ?></span>
								</label>
							</td>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[opentable]" id="opentable" tabindex="8" value="<?php echo get_option('opentable'); ?>" <?php echo (get_option('opentable')) ? "checked=checked" : ""; ?> />
									<span><?php _e(OPENTABLE); ?></span>
								</label>
							</td>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[facebook]" id="facebook" tabindex="8" value="<?php echo get_option('facebook'); ?>" <?php echo (get_option('facebook')) ? "checked=checked" : ""; ?> />
									<span><?php _e(FACEBOOK); ?></span>
								</label>
							</td>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[foursquare]" id="foursquare" tabindex="8" value="<?php echo get_option('foursquare'); ?>" <?php echo (get_option('foursquare')) ? "checked=checked" : ""; ?> />
									<span><?php _e(FOURSQUARE); ?></span>
								</label>
							</td>
            			</tr>


						<tr>
						    <td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[insiderpages]" id="insiderpages" tabindex="8" value="<?php echo get_option('insiderpages'); ?>" <?php echo (get_option('insiderpages')) ? "checked=checked" : ""; ?> />
									<span><?php _e(INSIDERPAGES); ?></span>
								</label>
							</td>
							<td>
								<label class="checkbox-input">
									<input type="checkbox" class="show_review" name="show_review[zillow]" id="zillow" tabindex="8" value="<?php echo get_option('zillow'); ?>" <?php echo (get_option('zillow')) ? "checked=checked" : ""; ?> />
									<span><?php _e(ZILLOW); ?></span>
								</label>
							</td>

						<?php
						    $Count = 9;
						    // Starting at 2 because we already have 2 at above
						    $Iterator = 2;
						    $tNewSites = array('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'
                                    );

                            foreach($tNewSites as $Site) { ?>

                                    <td>
                                        <label class="checkbox-input">
                                            <input type="checkbox" class="show_review" name="show_review[<?=$Site; ?>]" id="<?=$Site; ?>" tabindex="<?=$Count; ?>" value="<?php echo get_option($Site); ?>" <?php echo (get_option($Site)) ? "checked=checked" : ""; ?> />
                                            <span><?php _e($Site); ?></span>
                                        </label>
                                    </td>

                            <?php
                                $Count++;
                                $Iterator ++;
                                if($Iterator % 4 == 0)
                                    echo "</tr><tr>";
                                } ?>
                        </tr>
            		</tbody>
            	</table>

                <div>
                    <label id="show_review_error"></label>
                </div>
            </div>
        </div>
        <div class="btn-group_1">
            <input type="submit" value= "Submit" />
            <input type="hidden" name ="formtype" value="<?php echo md5('reputation_management');?>" />
        </div>
    </form>
</div>
<?php
wp_enqueue_style('review_admin_css',plugins_url( '/css/review_admin.css' , __FILE__ ));