<?php

$title = 'Банды';
foreach (array('common','functions','user','h') as $data)
{
	include_once $_SERVER['DOCUMENT_ROOT'].'/system/'.$data.'.php';
}

if (!$user)
{
	header("Location:/");
	exit;
}


$dungs = array('Кровавая река','Сердце мрака','Призрачный лес');
$dungsMap = array('BloodyRiver','HeartOfDarkness','GhostForest');

if ($user['party'] > 0)
{
	$party =  @mysql_fetch_assoc(mysql_query("SELECT * FROM `party` WHERE `id`='".$user['party']."'"));

	if (!$party)
	{
		mysql_query("UPDATE `users` SET `party`='0' WHERE `id`='".$user['id']."'");
		header("Location:?");
		exit;
	}


	if (isset($_GET['exit']))
	{

		if (isset($_GET['true']))
		{
			if ($user['id'] == $party['leader'])
			{
				mysql_query("DELETE FROM `party` WHERE `id`='".$user['party']."'");
				mysql_query("UPDATE `users` SET `party`= '0' WHERE `party`='".$user['party']."'");
			} else {
				mysql_query("UPDATE `users` SET `party`='0' WHERE `id`='".$user['id']."'");
			}
				header("Location:/dungeons/");
				exit;
		}
		?>
		<div class ='content'/>
		<center>
			Вы желаете покинуть банду?
			<font color = '#fefefe'/><?=($user['id'] ==  $party['leader'] ? '<br/>Внимание банда будет расформирована!':null);?></font>
		</center>
		</div>
		<div class ='line'/></div>
		<div class ='content'/>
			<center>
				<a class ='button' href ='?exit&amp;true'/> Да покинуть банду </a>
				<br/><br/>
				<a class ='button' href ='/dungeons/party.php'/> Нет, отмена </a>
			</center>
		</div>
		<?

		include_once '../system/f.php';
		exit;
	}


	?>
	<div class ='content'/>
		Ваша банда отправляеться в подземелье <i> <?=$dungs[$party['dungeon']];?></i>
		<br/>
		<a href ='/dungeons/<?=$dungsMap[$party['dungeon']];?>' style ='text-decoration:none;'/> Войти в подземелье </a>
	</div>
	<div class ='dl'/></div>
	<div class ='title'/>
		<center>
			Чат банды
		</center>
	</div>
	<div class ='dl'/></div>
	<div class ='content'/>
	<?

	$chatCount = mysql_num_rows(mysql_query("SELECT `id`,`party` FROM `party_chat` WHERE `party`='".$party['id']."'"));

	if (isset($_GET['sendchat']))
	{
		$message = _string($_POST['message']);

		if (mb_strlen($message) < 1 OR mb_strlen($message)>1024)
		{
			?>
			<div class ='title'/>
				<center>
					Длина сообщения  1-1024 символов.
				</center>
			</div>
			<?
		} else {
			mysql_query("INSERT INTO `party_chat` SET `party`='".$party['id']."', `user`='".$user['id']."',`text`='".$message."',`time`='".$_SERVER['REQUEST_TIME']."'")or die (mysql_error());
			header("Location:?rndChat=".rand(100,9999));
			exit;
		}
	}


	if ($chatCount == '0')
	{
		?>
		Чат банды пуст...

		</div>
		<div class ='line'/></div>
		<?
	} else {


		$max = 10;
		$count = $chatCount;
		$pages = ceil($count/$max);
		$page = _string(_num($_GET['page']));

		if($page > $pages)
		{
			$page = $pages;
		}
		if($page < 1) 
		{
			$page = 1;
		}
		$start = $page * $max - $max;

		$dataChat = mysql_query("SELECT * FROM `party_chat` WHERE `party`='".$party['id']."' ORDER BY `time` DESC  LIMIT $start,$max");

		while ($chat = @mysql_fetch_assoc($dataChat))
		{
			$row = @mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='".$chat['user']."'"));
			?>
			<img src='/images/icon/race/<?=$row['r'].($row['online'] > (time() - 300) ? '':'-off')?>.png' alt='*'/> <a href='/user/<?=$row['id']?>/'><?=$row['login']?></a>:
			<?=_string($chat['text']);?>
			</div>
			<div class='dl'/></div>
			<div class ='content'/>
			<?
		}

		?>
		<center><?=pages('?');?></center>

		</div>

		<?php

		 }
		 ?>
		 <div class 'line'/></div>
		 <div class ='content'/>
		 <center>
		<form  action ='?sendchat' method ='post'/>
			<textarea name ='message' placeholder ='Введите сообщение' rows ='4' cols ='55'></textarea><br/>
			<input type ='submit' value ='Сказать' style ='width:75%'/>
		</form>
		</center>
		</div>
		<div class ='line'/></div>
		<div class ='title'/>
			<center>
				Участники
			</center>	
		</div>
		<div class ='line'/></div>

		<div class ='content'/>

		<?php

		$members = mysql_query("SELECT * FROM `users` WHERE `party`= '".$user['party']."'");

		if (isset($_GET['leader']))
		{
			$leader = (int) abs($_GET['leader']);

			print_r($_GET);
			exit;
		}

		while ($row = @mysql_fetch_assoc($members)) {
			?>
			<img src='/images/icon/race/<?=$row['r'].($row['online'] > (time() - 300) ? '':'-off')?>.png' alt='*'/> <a href='/user/<?=$row['id']?>/'><?=$row['login']?></a> 
			<?=($row['id'] == $party['leader'] ? 'лидер':null);?>
			<?=($user['id'] == $party['leader'] && $row['id']!=$user['id'] ? '<a style="text-decoration:none;color: rgb(144, 144, 144);" href =\'?leader='.$row['id'].'\'/> Передать лидерство </a>':null);?>
			<?=($user['id'] == $party['leader'] && $row['id']!=$user['id'] ? '<a style="text-decoration:none;color: rgb(144, 144, 144);" href =\'?kick='.$row['id'].'\'/> Кикнуть </a>':null);?>
			<br/>
			<?
		}

		?>
		</div>
		<div class ='line'/></div>
		<div class='list'/>
			<li>
				<a href = '?exit'/> Покинуть банду </a>
			</li>
		</div>
		<?

} else {
	if (isset($_GET['create']))
	{
		if (isset($_GET['form']))
		{
			$dungeonP = (int) abs($_POST['dungeon']);

			mysql_query("INSERT INTO `party` SET `leader`='".$user['id']."',`dungeon`='$dungeonP'")or die (mysql_error());
			$idP = mysql_insert_id();
			mysql_query("UPDATE `users` SET `party`='$idP' WHERE `id`='".$user['id']."'");
			header("Location:party.php");
			exit;
		}

		?>
		<div class ='content'/>
			<form action = '?create&amp;form' method='post'/>
			Выберите направление:
			<br/>
			<select name ='dungeon'>
				<?php
				$idDung  = -1;
				foreach ($dungs as $k=>$v) {
					$idDung ++;
					?>
					<option value ='<?=$idDung;?>'/><?=$v;?></option>
					<?
				}

				?>
			</select>
			<input type ='submit' class ='button' value ='Создать банду'/>
			</form>
		</div>


		<?
	} 
	elseif (isset($_GET['find'])) 
	{
		$partyes = mysql_query("SELECT * FROM `party` ORDER BY `id` DESC");

		while  ($row = @mysql_fetch_assoc($partyes)) 
		{
			$partymemb = mysql_num_rows(mysql_query("SELECT `id`,`party` FROM `users` WHERE `party`='".$row['id']."' and `id`!='".$user['id']."'"));

			?>
			<div class ='content'/>
				Банда <?=$dungs[$row['dungeon']];?><br/>
				<a href ='/user/<?=$row['leader'];?>/'/> Лидер </a>
			</div>
			<div class ='dl'/></div>
			<?
		}
	} else {
		?>
		<div class='content'/>
			<center>
				Тут вы сможете найти себе компанию для похода в подземелье.
			</center>
		</div>
		<div class ='line'/></div>
		<div class ='list'/>
			<li>
				<a href ='?create'/> Создать банду </a>
			</li>
			<li>
				<a href ='?find'/> Найти банду </a>
			</li>
		</div>
		<?
	}
}


include_once '../system/f.php';