<?php
	header('X-Frame-Options: DENY');
	header('X-XSS-Protection: 1; mode=block');
	header('Content-Type: text/html; charset=utf-8');

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

	if(!(strripos($users_groups[$_SESSION['rights']]['rights'], "j") !== false) && !(isset($_SESSION['admin']) && $_SESSION['admin'] == "yes"))
		exit('access error');
	
	$conf->time_zone = isset($conf->time_zone) ? $conf->time_zone : 'Europe/Moscow';

	date_default_timezone_set($conf->time_zone);

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

	$id = isset($_POST['server']) ? intval($_POST['server']) : 0;

	$servers = $pdo->query('SELECT `id`, `name`, `type` FROM `servers` WHERE `id`="'.$id.'" LIMIT 1');

	$servers->setFetchMode(PDO::FETCH_OBJ);

	$users = array();
	$aType = array('a' => 'Ник и пароль', 'ce' => 'SteamID', 'ca' => 'SteamID и пароль');

	while($server = $servers->fetch())
	{
		$i = 0;

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

		$admins = $pdo->query('SELECT `id`, `name`, `user_id`, `active`, `comment`, `pause` FROM `admins` WHERE `server`="'.$server->id.'"');
				  $admins->setFetchMode(PDO::FETCH_OBJ);
		while($admin = $admins->fetch())
		{
			$i += 1;

			$privileges = '';

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

			$check = 0;

			while($service = $services->fetch())
			{
				if(!in_array($service->show_adm, array('', '1')))
					continue;

				$privileges .= $service->name != '' ? $service->name.' + ' : 'Неизвестно + ';

				$check += 1;

				$time = $service->ending_date;
			}

			if($privileges != '')
			{
				$privileges = substr($privileges, 0, -3);

				if(!isset($users[$admin->user_id]))
				{
					$user = $pdo->query('SELECT `login`, `avatar` FROM `users` WHERE `id`="'.$admin->user_id.'" LIMIT 1');
							$user->setFetchMode(PDO::FETCH_OBJ);
					$user = $user->fetch();

					$users[$admin->user_id] = $user;
				}

				if(!$admin->user_id)
				{
					$users[$admin->user_id]->login = 'Неизвестно';
					$users[$admin->user_id]->avatar = '../files/avatars/no_avatar.jpg';
				}

				if($check == 1)
					$time = $time == '0000-00-00 00:00:00' ? 'Никогда' : expand_date($time, 1);
				else
					$time = '<a href="#" onclick="get_admin_info('.$admin->id.'); return false" data-target="#admin_modal'.$admin->id.'" data-toggle="modal"><u>Несколько услуг</u></a>';

				$tpl->load_template('admins_edit.tpl');
					$tpl->set('{i}', $i);
					$tpl->set('{id}', $admin->id);
					$tpl->set('{auth}', $admin->name);
					$tpl->set('{time}', $time);
					$tpl->set('{user}', $admin->user_id);
					$tpl->set('{comment}', $admin->comment != '' ? 'title="'.$admin->comment.'"' : '');
					$tpl->set('{login}', $users[$admin->user_id]->login);
					$tpl->set('{avatar}', $users[$admin->user_id]->avatar);
					$tpl->set('{privileges}', $privileges);
				$tpl->compile('admins');
			}
		}

		$hide = '';

		if($tpl->result['admins'] == '')
		{
			$hide = 'disp-n';

			$tpl->load_template('content_none.tpl');
				$tpl->set('{class}', 'm-l-5 m-b-5 m-r-5 m-t-5');
				$tpl->set('{text}', 'Список администраторов пуст');
			$tpl->compile('admins');
		}

		$tpl->load_template('server_admins.tpl');
			$tpl->set('{id}', $server->id);
			$tpl->set('{name}', $server->name);
			$tpl->set('{hide}', $hide);
			$tpl->set('{admins}', $tpl->result['admins']);
		$tpl->compile('content');
	}

	if($tpl->result['content'] == '')
	{
		$tpl->load_template('content_none.tpl');
			$tpl->set('{text}', 'Список администраторов пуст, нет игровых серверов');
		$tpl->compile('content');
	}

	$tpl->show($tpl->result['content']);
?>