<?php
	admin :: access(array('a'=>3));
	$table = 'blocks';
	$id = (isset($_GET['id']) ? intval($_GET['id']) : 0 );
	$d = profile :: getUser($id);
	$bl = false;
	$errorP = NULL;
	if(isset($_POST['cancel'])) {
		system :: redirect(NULL, $thisLink."/?act=user/block&id={$d['id']}");
	} else if(isset($_POST['cfms'])) {
		$timeb = intval($_POST['time']);
		$cause = intval($_POST['cause']);
		if(in_array($timeb, array(0,1,3,12,24,48,120))) {
			if($cause >= 0 && $cause <= 5) {
				if($cause == 0 && $timeb != 0) {
					$errorP = "Предупреждение можно выдавать только без времени.";
				} else {
					$bl = true;
					$timest = time() + $timeb * 60 * 60; // Time block in timest. .

					if(isset($_POST['access']) && $_POST['access'] == ACCESS) {

						$block_db = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `blocks` WHERE `id_user` = ? AND `id_data` = ? AND `cause` <> ? AND `unlock` = ? LIMIT 1;", array($d['id'], $user['id'], 0, 0));
						if($block_db == 0) {
							DB :: $pdo -> query("INSERT INTO `blocks` (`id_user`, `id_data`, `time`, `date`, `cause`) VALUES (?, ?, ?, ?, ?);", array($d['id'], $user['id'], time(), $timest, $cause));
							DB :: $pdo -> query("UPDATE `users` SET `block_time` = ?, `count_blocks` = `count_blocks`+1, `block_unlock` = '0',`block_cause` = ?  WHERE `id` = ? LIMIT 1;", array($timest, $cause, $d['id']));
							system :: redirect("Пользователь успешно заблокирован.", profile :: getLink($d));
						} else {
							$errorP = "Пользователь уже заблокирован.";
						}
					}
				}
			} else { $errorP = "Не верно указана причина блокировки."; }
		} else { $errorP = "Не верно указано время блокировки."; }
	}


	$title = $l = "Панель управления - Блокировка пользователя";
	$lBack ="/admin/?act=user/info&amp;id=".$d['id']."";

	require (CORE."head.php");

	system :: show($errorP);
	echo "<div class=\"app-block\">";
	echo system :: touch(profile :: getLink($d),"
			<div class=\"post-l emotion-show\" data-emotions=\"".system::textOut($d['emotions'])."\">
				".profile :: userAvatar($d,0)."
			</div>
			<div class=\"post-c\">
				<div class=\"_name\">".profile :: getName($d, true)."</div>
				<div class=\"_gray\">Время на сайте: ".profile :: onlineTime($d['online_time'])."</div>
				<div class=\"_gray\">Дата регистрации: ".system :: times($d['first_time'])."</div>
				<div class=\"_gray\">Блокировок: ".$d['count_blocks']."</div>
			</div>
			"); 

		?>
		<div class="list">
			<div class="app-catergory-line"><span>Текущее состояние</span></div>
			<?php if($d['block_unlock'] == 0) { ?>
			<div><span class="_gray">Блок до:</span> <?=system :: times($d['block_time'])?></div>
			<div><span class="_gray">Причина:</span> <?=$config['CAUSE_BLOCK'][$d['block_cause']]?></div>
			<?=($d['block_time'] < time() ? '<div>Нуждается в раблокировке</div>' : '' ) ?>
			<?php } else { ?>
				<div class="_center _gray _font-s">Пользователь не находится в блокировке.</div>
			<?php } ?>	
			<div class="app-catergory-line"><span>Все блокировки</span></div>
		<?php


		$query = "SELECT * FROM `{$table}` WHERE `id_user` = ? ORDER by `time` DESC ";
		$total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `{$table}` WHERE `id_user` = ?;", [$d['id']]);
		
		if($total > 10) {
			$p = (($_GET['p']) ? intval($_GET['p']) : 1);
			$pagination = new pagination(10, $total, $p);
			$query .= $pagination -> limit;
		}
		
		$q = DB :: $pdo -> query("{$query}",[$d['id']]);

		$i = 0;
		$in = 0;

		while($r = $q -> fetch()) {
			$adm = profile :: getUser($r['id_data']);
			?>
			<div >
				<div class="list-flex">
				<div class="post-l"><?=profile :: userAvatar($adm,3)?></div>
				<div class="post-c">
					<div class="_name"><?=profile :: goPage($adm)?></div>
					<div class="_time"><?=system :: times($r['time'])?></div>
				</div>
				</div>
				<div class="_oh">
						<div><span class="_gray">Блок до:</span> <?=system :: times($r['date'])?></div>
						<div><span class="_gray">Причина:</span> <?=$config['CAUSE_BLOCK'][$r['cause']]?></div>	
						<div><span class="_gray"><a href="?delete">Удалить блок</a></span> </div>	
				</div>
			</div>
			<?php
		}
		echo "</div>";
    	if($total > 10)	echo $pagination -> printPages("/admin/?act=user/blocklist&amp;id={$d['id']}&amp;"); 
	echo system :: back("/admin/?act=user/info&amp;id={$d['id']}","Информация", true);
	echo "</div>";
	require (CORE."foot.php");