<?php
	header('Content-Type: text/html; charset=utf-8');

	include('../../../inc/start.php');
	include('../../../inc/protect.php');

	if(!isset($_POST['token']) || $_SESSION['token'] != clean($_POST['token'], null))
		exit(json_encode(array('status' => 'error', 'text' => 'Неверный токен')));

	$tpl = new Template;
	$tpl->dir = '../tpl/ajax/elements/';

	$date = date('Y-m-d H:i:s');

	$users = $pdo->query('SELECT `id`, `login`, `avatar`, `regdate` FROM `users` WHERE `invited`="'.$_SESSION['id'].'"');
			 $users->setFetchMode(PDO::FETCH_OBJ);

	$tpl->result['content'] = '';

	while($user = $users->fetch())
	{
		$query = $pdo->query('SELECT `shilings` FROM `shilings_actions` WHERE `gave_out`="'.$user['id'].'"');
				 $query->setFetchMode(PDO::FETCH_OBJ);

		$shilings = 0;

		while($shiling = $query->fetch())
			$shilings += $shiling->shilings;

		$tpl->load_template('profile_referal.tpl');
			$tpl->set('{shilings}', $shilings);
			$tpl->set('{id}', $user->id);
			$tpl->set('{login}', $user->login);
			$tpl->set('{avatar}', $user->avatar);
			$tpl->set('{regdate}', expand_date($user->regdate, 7));
		$tpl->compile('content');
	}

	if($tpl->result['content'] == '')
	{
		$tpl->load_template('content_none.tpl');
			$tpl->set('{text}', 'Рефералов нет');
		$tpl->compile('content');
	}

	echo $tpl->result['content'];
?>