<?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' => 'Неверный токен')));


	$id = intval($_POST['id']);

	if(!$id)
		exit;

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

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

	$admins = $pdo->query('SELECT `admins_services`.`id`, `services`.`name`, `admins_services`.`service`, `admins_services`.`bought_date`, `admins_services`.`ending_date` FROM `admins_services` LEFT JOIN `services` ON `admins_services`.`service` = `services`.`id` WHERE `admins_services`.`admin_id` ="'.$id.'"'); 
			  $admins->setFetchMode(PDO::FETCH_OBJ);

	while($admin = $admins->fetch())
	{
		$left = 'Вечность';
		$color = 'success';
		$ending_date = 'Никогда';

		if($admin->ending_date != '0000-00-00 00:00:00')
		{
			$left = strtotime($admin->ending_date)-$_SERVER['REQUEST_TIME'];

			if($left > 432000)
				$color = 'success';
			elseif($left > 86400)
				$color = 'warning';
			else
				$color = 'danger';

			$left = expand_seconds2($left, 2);

			$ending_date = expand_date($admin->ending_date, 1);
		}

		$tpl->load_template('profile_privileges_info.tpl');
			$tpl->set('{name}', $admin->name != '' ? $admin->name : 'Неизвестно');
			$tpl->set('{bought_date}', $admin->bought_date != '0000-00-00 00:00:00' ? expand_date($admin->bought_date, 1) : 'Неизвестно');
			$tpl->set('{ending_date}', $ending_date);
			$tpl->set('{left}', $left);
			$tpl->set('{color}', $color);
		$tpl->compile('content');
	}

	if($tpl->result['content'] == '')
	{
		$tpl->load_template('content_none.tpl');
			$tpl->set('{text}', 'Привилегий нет');
		$tpl->compile('content');
	}

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