<?php
define('_IN_JOHNCMS', 1);
$textl='Постройки'; 
require_once("../incfiles/core.php");
require_once("../incfiles/head.php");
require_once("manfunc.php"); 

if(!$club_id)
{
	echo'Вам нужен клуб!';
}
else
{

	if(isset($_GET['new']))
	{
		$new = intval($_GET['new']);
		
		$sql = mysql_query("SELECT * FROM `fman_build` WHERE `id`='$new' LIMIT 1");
		if(mysql_num_rows($sql))
		{
			if(mysql_result(mysql_query("select count(*) from `fman_clubs_build` where `bid`='$new' and `club_id`='$club_id' LIMIT 1"),0))
			{
				echo'Это здание уже построено!<br/><a href="build.php">Назад</a>';
			}
			else
			{
				$b = mysql_fetch_assoc($sql);
				
				if($b['pay']>$dataclub['budj'])
				{
					echo'Вам нехватает денег для постройки!<br/><a href="build.php">Назад</a>';
				}
				else
				{
					mysql_query("INSERT INTO `fman_clubs_build` SET `club_id`='$club_id', `bid`='$new'");
					mysql_query("UPDATE `fman_clubs` SET `budj`=`budj`-'$b[pay]',`plus`=`plus`+'$b[plus]' where `id`='$club_id' limit 1") or die(mysql_error());
					echo'Вы построили <b>'.$b['name'].'</b>!<br/><a href="build.php">Назад</a>';
				}
			}
		}
		else
		{
			echo'Такой постройки нет!<br/><a href="build.php">Назад</a>';
		}
		
	}
	elseif(isset($_GET['club']))
	{
		$club = intval($_GET['club']);
		
		$sql = mysql_query("SELECT `name` FROM `fman_clubs` WHERE `id`='$club' LIMIT 1");
		if(mysql_num_rows($sql))
		{
			$datac = mysql_fetch_assoc($sql);
			echo'<div class="phdr"><b>Постройки <a href="club.php?id='.$club.'">'.$datac['name'].'</a></b></div>';
			$sql = mysql_query("SELECT `fman_clubs_build`.*,`fman_build`.* FROM `fman_clubs_build` LEFT JOIN `fman_build` ON `fman_build`.`id`=`fman_clubs_build`.`bid` WHERE `club_id`='$club'");
			if(mysql_num_rows($sql))
			{
				echo
				'
					<table cellpadding="2" rules="rows" style="border:1px solid grey;margin:3px;">
				';
				
				
				while($b = mysql_fetch_assoc($sql))
				{
					echo'<tr><td><img width="100" height="50" src="buildings/'.$b['pic'].'" /></td><td><b>'.$b['name'].', $'.$b['pay'].'</b><br/>Доход: <b>$'.$b['plus'].'</b></td></tr>';
				}
				
				echo'</table>';
			}
			else
			{
				echo'У клуба <b>'.$datac['name'].'</b> нет построек!';
			}
		}
		else
		{
			echo'Такой клуба нет!';
		}
		
	}
	else
	{

		$sql = mysql_query("SELECT *,(select count(*) from `fman_clubs_build` where `fman_clubs_build`.`bid`=`fman_build`.`id` and `fman_clubs_build`.`club_id`='$club_id') as `yes` FROM `fman_build` HAVING `yes`='0'");
		if(mysql_num_rows($sql))
		{
			echo
			'
				<div class="phdr"><b>Доступные постройки</b></div>
				<table cellpadding="2" rules="rows" style="border:1px solid grey;margin:3px;">
			';
			
			
			while($b = mysql_fetch_assoc($sql))
			{
				echo'<tr><td><img width="100" height="50" src="buildings/'.$b['pic'].'" /></td><td><b>'.$b['name'].', $'.$b['pay'].'</b><br/>Доход: <b>$'.$b['plus'].'</b><br/><a href="build.php?new='.$b['id'].'"><button>Построить</button></a></td></tr>';
				//echo'<tr><td>'.$b['name'].'</td><td>$'.$b['pay'].'</td><td>$'.$b['plus'].'</td><td><a href="build.php?new='.$b['id'].'">Построить</a></td></tr>';
			}
			
			echo'</table>';
		}
		
		$sql = mysql_query("SELECT `fman_clubs_build`.*,`fman_build`.* FROM `fman_clubs_build` LEFT JOIN `fman_build` ON `fman_build`.`id`=`fman_clubs_build`.`bid` WHERE `club_id`='$club_id'");
		if(mysql_num_rows($sql))
		{
			echo
			'
				<div class="phdr"><b>Мои постройки</b></div>
				<table cellpadding="2" rules="rows" style="border:1px solid grey;margin:3px;">
			';
			
			
			while($b = mysql_fetch_assoc($sql))
			{
				echo'<tr><td><img width="100" height="50" src="buildings/'.$b['pic'].'" /></td><td><b>'.$b['name'].', $'.$b['pay'].'</b><br/>Доход: <b>$'.$b['plus'].'</b></td></tr>';
			}
			
			echo'</table>';
		}	
		
	}
	echo"<div class='phdr'><a href='index.php'>Фут.менеджер</a></div>";
}

require_once("../incfiles/end.php"); 
?>
