<?php
ini_set("display_errors","0");
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");

if($_POST['action'] == "get"){
	$sql = "SELECT pannelist_id, COUNT(*) as Count, SUM(points) as Sum, friend_id FROM ".DB_WARPIT_USERSPOOL."._friendPoints GROUP BY pannelist_id";
	$res = $db->SQLexecute($sql);
	while($dat = $db->fetchAssoc($res)){
		$sql = "SELECT email FROM ".DB_WARPIT_USERSPOOL."._Interviewer WHERE id = ".$dat['pannelist_id'];
		$rez = $db->SQLexecute($sql);
		$zad = $db->fetchAssoc($rez);
		$dat['email'] = $zad['email'];
		$data[] = $dat;
	}
	$results['success'] = true;
	$results['results'] = sizeof($data);
	$results['rows'] = $data;
	echo json_encode($results);
}elseif($_POST['action'] == "getPrijatelja"){
	$pannelistId = $_POST['pannelist_id'];
	$sql = "SELECT friend_id,points FROM ".DB_WARPIT_USERSPOOL."._friendPoints WHERE pannelist_id = $pannelistId GROUP BY friend_id DESC";
	$res = $db->SQLexecute($sql);
	while($dat = $db->fetchAssoc($res)){
		$friends[] = $dat['friend_id'];
		$points[] = $dat['points'];	
	}
	$i = 0;
	if(is_array($friends)){
		$sql = "SELECT * FROM ".DB_WARPIT_USERSPOOL."._Interviewer WHERE id IN ('".implode("','",$friends)."') GROUP BY id DESC";
		$res = $db->SQLexecute($sql);
		while($dat = $db->fetchAssoc($res)){
			$dat['Points'] = $points[$i];
			$data[] = $dat;
			$i++;
		}
	}else{
		$data[]="";
	}
	$results['success'] = true;
	$results['results'] = sizeof($data);
	$results['rows'] = $data;
	echo json_encode($results);
}elseif($_POST['action'] == "update"){
	echo "ID: ".$_POST['id']."\n";
	if($_POST['field'] == "Points"){
		$sql = "UPDATE ".DB_WARPIT_USERSPOOL."._friendPoints SET ".$_POST['field']." = '".$_POST['value']."' WHERE friend_id = ".$_POST['id'];
		$db->SQLexecute($sql);
	}else{
		$sql = "UPDATE ".DB_WARPIT_USERSPOOL."._Interviewer SET ".$_POST['field']." = '".$_POST['value']."' WHERE id = ".$_POST['id'];
		$db->SQLexecute($sql);
	}
	echo $sql;
}elseif($_POST['action'] == "DeleteFriend"){
	$sql = "DELETE FROM ".DB_WARPIT_USERSPOOL."._friendPoints WHERE friend_id = ". $_POST['friend_id'];
	$db->SQLexecute($sql);
}
?>