<?php

function bp_dedication_ajax_autocomplete_results() {
	global $bp;

	$return = array(
		'query' 	=> $_REQUEST['query'],
		'data' 		=> array(),
		'suggestions' 	=> array()
	);

	$users = bp_dedication_members_query( $_REQUEST['query'] );

	if ( $users ) {
		$suggestions = array();
		$data 	     = array();

		foreach ( $users as $user ) {
			$suggestions[] 	= $user->display_name . ' (' . $user->user_login . ')';
			$data[] 	= $user->ID;
		}

		$return['suggestions'] = $suggestions;
		$return['data']	       = $data;
	}

	echo json_encode( $return );
}
add_action( 'wp_ajax_bp_dedication_autocomplete_ajax_handler', 'bp_dedication_ajax_autocomplete_results' );


function bp_dedication_ajax_add_user() {
	global $bp;

	//check_ajax_referer( 'groups_invite_uninvite_user' );

	if ( !$_POST['friend_id'] || !$_POST['friend_action'] )
		return false;

	if ( 'add' == $_POST['friend_action'] ) {

		$user = new BP_Core_User( $_POST['friend_id'] );

		echo '<li id="uid-' . $user->id . '">';
		echo bp_core_fetch_avatar( array( 'item_id' => $user->id ) );
		echo '<h4>' . bp_core_get_userlink( $user->id ) . '</h4>';
		echo '<div class="action">
				<a class="remove" href="' . wp_nonce_url( $bp->loggedin_user->domain . $group_slug . '/' . $_POST['group_id'] . '/invites/remove/' . $user->id, 'groups_invite_uninvite_user' ) . '" id="uid-' . esc_html( $user->id ) . '">' . __( 'Remove', 'bp_dedication' ) . '</a>
			  </div>';
		echo '</li>';

	} else if ( 'delete' == $_POST['friend_action'] ) {
		return true;
	} else {
		return false;
	}
}
add_action( 'wp_ajax_bp_dedication_add_user_ajax_handler', 'bp_dedication_ajax_add_user' );


function bp_dedication_members_query( $search_terms = false ) {
	// Get a list of group members to be excluded from the main query
	$group_members = array();
	$args = array(
		'exclude_admins_mods' => false
	);
	if ( $search_terms )
		$args['search'] = $search_terms;

	$group_members[] = bp_loggedin_user_id();

	// Now do a user query
	// Pass a null blog id so that the capabilities check is skipped. For BP blog_id doesn't
	// matter anyway
	$user_query = new Bp_Dedication_User_Query( array( 'blog_id' => NULL, 'exclude' => $group_members, 'search' => $search_terms ) );

	return $user_query->results;
}

//$oldURL = dirname(__FILE__);
//$newURL = str_replace(DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'dedication-from-site', '', $oldURL);
//
//require_once( $newURL . DIRECTORY_SEPARATOR . 'wp-load.php');
//global $wpdb;
//
//var_dump($_REQUEST);
//
//// if the 'term' variable is not sent with the request, exit
//if (!isset($_REQUEST['term']))
//    exit;
//
//$data = array();
//
//$wp_user_search = $wpdb->get_results("SELECT ID, display_name, user_email FROM $wpdb->users ORDER BY ID");
//foreach ($wp_user_search as $userid) {
//    $user_id = (int) $userid->ID;
//    $display_name = stripslashes($userid->display_name);
//    $user_email = stripslashes($userid->user_email);
//    
//    $data[] = array(
//        'label' => $display_name . ' (' . $user_email . ')',
//        'value' => $user_id
//    );
//}
//// jQuery wants JSON data
//echo json_encode($data);
//flush();


