<?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/';

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

	$limit = $pdo->query('SELECT `bans_lim2` FROM `config2` LIMIT 1');
			 $limit->setFetchMode(PDO::FETCH_OBJ);
	$limit = $limit->fetch();
	
	if($id)
		$query = $pdo->query('SELECT bans.id, bans.server, bans.status, bans.nick, bans.date, bans.author, users.login, users.avatar, servers.name FROM `bans` LEFT JOIN `users` ON bans.author=users.id LEFT JOIN `servers` ON bans.server=servers.id WHERE bans.server="'.$id.'" ORDER BY `date` DESC LIMIT '.$start.', '.$limit->bans_lim2);
	else
		$query = $pdo->query('SELECT bans.id, bans.server, bans.status, bans.nick, bans.date, bans.author, users.login, users.avatar, servers.name FROM `bans` LEFT JOIN `users` ON bans.author=users.id LEFT JOIN `servers` ON bans.server=servers.id ORDER BY `date` DESC LIMIT '.$start.', '.$limit->bans_lim2);

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

	$aData = array(
		0 => array('status' => 'Не рассмотрена', 'color' => 'warning'),
		1 => array('status' => 'Разбанен', 'color' => 'success'),
		2 => array('status' => 'Не разбанен', 'color' => 'danger')
	);

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

	while($ban = $query->fetch())
	{
		$tpl->load_template('unban.tpl');
			$tpl->set('{id}', $ban->id);
			$tpl->set('{nick}', $ban->nick);
			$tpl->set('{author}', $ban->author);
			$tpl->set('{login}', $ban->login);
			$tpl->set('{avatar}', $ban->avatar);
			$tpl->set('{name}', $ban->name);
			$tpl->set('{date}', expand_date($ban->date, 7));
			$tpl->set('{color}', $aData[$ban->status]['color']);
			$tpl->set('{status}', $aData[$ban->status]['status']);
		$tpl->compile('content');
	}

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

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