<?php
include_once ('./core/base.php');
falseauth();

$header = 'Команда';

include_once ('./core/head.php');

echo '<div class="b"><div class="title">Команда</div>';

$_GET['action'] = fl($_GET['action']);

if(!$uparty){

	if($_GET['action'] == 'new'){
		$base -> query('INSERT INTO `party` SET `first_user` = "'.$u['id'].'"');
		$_SESSION['message'] = 'Команда успешно создана';
		header('Location: /party/');
		exit;
	}

	echo '<table class="event_info"><tr><td><img src="/images/icons/info.png"></td><td> - В команде, получаемый опыт делится между игроками в команде.<br/>
	- В команде может находиться максимум три игрока.</td></tr></table>';

	echo '<center><a class="btn_small" href="/party/new/">Создать команду</a></center>';

}else{

	if($_GET['action'] == 'exit'){
		if($uparty['first_user'] == $u['id']){
			$base -> query('DELETE FROM `party` WHERE `id` = "'.$uparty['id'].'"');
			$base -> query('DELETE FROM `party_chat` WHERE `party_id` = "'.$uparty['id'].'"');
			$_SESSION['message'] = 'Команда расформирована.';
		}elseif($uparty['second_user'] == $u['id']){
			if($uparty['third_user'] != 0){
				$base -> query('UPDATE `party` SET `second_user` = "'.$uparty['third_user'].'", `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
			}else{
				$base -> query('UPDATE `party` SET `second_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
			}
			$_SESSION['message'] = 'Вы покинули команду.';
		}elseif($uparty['third_user'] == $u['id']){
			$base -> query('UPDATE `party` SET `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
			$_SESSION['message'] = 'Вы покинули команду.';
		}
	header('Location: /party/');
	exit;
	}

	if($_GET['action'] == 'kick_s_user' OR $_GET['action'] == 'kick_t_user'){
		if($uparty['first_user'] == $u['id']){
			if($_GET['action'] == 'kick_s_user'){
				if($uparty['second_user'] != 0){
					if($uparty['third_user'] != 0){
						$base -> query('UPDATE `party` SET `second_user` = "'.$uparty['third_user'].'", `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
					}else{
						$base -> query('UPDATE `party` SET `second_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
					}
					$_SESSION['message'] = 'Игрок исключён.';
				}
			}elseif($_GET['action'] == 'kick_t_user'){
				if($uparty['third_user'] != 0){
					$base -> query('UPDATE `party` SET `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
					$_SESSION['message'] = 'Игрок исключён.';
				}
			}
		}
		header('Location: /party/');
		exit;
	}

	echo '<div class="event">';

	//Показываем лидера
	$leader = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1') -> fetch_assoc();
	$leader_avatar = '/images/skins/'.$leader['skin'].'_big.png';
	echo '<a class="btn" href="/user/'.$leader['id'].'/"><img src="'.$leader_avatar.'" width="24px"> '.nolinkname($leader['id']).' <b>Лидер</b></a>';

	//Показываем второго игрока
	if($uparty['second_user'] != 0){
		$s_user = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1') -> fetch_assoc();
		$s_user_avatar = '/images/skins/'.$s_user['skin'].'_big.png';
		if($uparty['first_user'] == $u['id']){
			echo '<table width="100%" style="margin-top: 5px; border-spacing: 0px;"><tr><td style="padding: 0px; margin-right: 25px;" width="89%"><a class = "btn_table" href="/user/'.$s_user['id'].'/"><img src="'.$s_user_avatar.'" width="24px"> '.nolinkname($s_user['id']).'</a></td>
			<td width="1%"></td><td style="padding: 0px;" width="10%"><center><a class = "btn_table" href="/party/kick/s_user"><img src="/images/icons/no.png"></a></center></td></tr></table>';
		}else{
			echo '<a class="btn" href="/user/'.$s_user['id'].'/"><img src="'.$s_user_avatar.'" width="24px"> '.nolinkname($s_user['id']).'</a>';
		}
	}

	//Показываем третьего игрока
	if($uparty['third_user'] != 0){
		$t_user = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1') -> fetch_assoc();
		$t_user_avatar = '/images/skins/'.$t_user['skin'].'_big.png';
		if($uparty['first_user'] == $u['id']){
			echo '<table width="100%" style="margin-top: 5px; border-spacing: 0px;"><tr><td style="padding: 0px; margin-right: 25px;" width="89%"><a class = "btn_table" href="/user/'.$s_user['id'].'/"><img src="'.$t_user_avatar.'" width="24px"> '.nolinkname($t_user['id']).'</a></td>
			<td width="1%"></td><td style="padding: 0px;" width="10%"><center><a class = "btn_table" href="/party/kick/s_user"><img src="/images/icons/no.png"></a></center></td></tr></table>';
		}else{
			echo '<a class="btn" href="/user/'.$s_user['id'].'/"><img src="'.$t_user_avatar.'" width="24px"> '.nolinkname($t_user['id']).'</a>';
		}
	}

	echo '</div>';

}

//ЧАТ

if($uparty){

$_GET['reply'] = fl($_GET['reply']);
$_POST['msg'] = fl($_POST['msg']);

if(isset($_REQUEST['add'])){
	if(!$ban){
		if(strlen($_POST['msg']) > 3 AND strlen($_POST['msg']) < 1024 AND $_POST['msg'] != ''){
			$base -> query('INSERT INTO `party_chat` SET
				`party_id` = "'.$uparty['id'].'",
				`user` = "'.$u['id'].'",
				`text` = "'.$_POST['msg'].'",
				`time` = "'.time().'"');
			header('Location: /party/');
			exit;
		}else{
			$_SESSION['message'] = 'Сообщение должно содержать не менее 3 символов и не более 1024.';
			header('Location: /party/');
			exit;
		}
	}else{
		$_SESSION['message'] = 'Вы сможете писать в чате по истечению срока бана.';
		header('Location: /party/');
		exit;
	}
}

echo '<div class="title">Чат команды</div>';

echo '<center>';
echo '<form action="" method="POST">';
if($_GET['reply'] != ''){
	$reply_user = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$_GET['reply'].'" LIMIT 1') -> fetch_assoc();
	$reply_text = ''.$reply_user['login'].', ';
}
echo '<input type="text" name="msg" value="'.$reply_text.'" placeholder="Введите сообщение." /> <br/>';
echo '<input type="submit" name="add" value="Написать"></br>';
echo '</form>';
echo '</center>';

$count = $base -> query("SELECT * FROM `party_chat` WHERE `party_id` = '".$uparty['id']."'") -> num_rows;
$k_page = k_page($count,5);
$page = page($k_page);
$start = 5*$page-5;
$sql = $base -> query("SELECT * FROM `party_chat` WHERE `party_id` = '".$uparty['id']."' ORDER BY `id` DESC LIMIT $start,5");
if($count > 0){
	while($msg = $sql -> fetch_assoc()){
		if($msg['user'] != $u['id']) $reply = ' <a href="/party/reply/'.$msg['user'].'/">[отв]</a>';
		else $reply = '';
		$msg['text'] = str_replace(array($u['login'].','), "<font style='text-decoration: underline;'><b>".$u['login']."</b></font>, ", $msg['text']);
		echo '<div class="event">'.name($msg['user']).' <font color="#B1B1B1">('.tm($msg['time']).')'.$reply.'</font><br/>
		<div class="text_line"></div>
		'.nl2br(smiles($msg['text'])).'</div>';
	}
	if($count > '5'){
	echo str('?',$k_page,$page);
	}
}else{
	echo '<center>В чате ещё не было сообщений.</center><br/>';
}

}

echo '<br/>';

if($uparty){
if($uparty['first_user'] == $u['id']) echo '<a class="btn" href="/party/exit/"><img src="/images/icons/no.png"> Расформировать команду</a>';
else echo '<a class="btn" href="/party/exit/"><img src="/images/icons/no.png"> Покинуть команду</a>';
}

echo '<a class="btn" href="/"><img src="/images/icons/back.png"> Назад</a>';

echo '</div>';

//echo '<br/><a class="btn" href="javascript:history.go(-1)"><img src="/images/icons/back.png"> Назад</a></div>';

include_once ('./core/foot.php');
?>