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

if(!$club_id)
{
	echo'Siz olidn klub tanlang!';
}
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'Bu imoratni qurgansiz!<br/><a href="build.php">Orqaga</a>';
			}
			else
			{
				$b = mysql_fetch_assoc($sql);
				
				if($b['pay']>$dataclub['budj'])
				{
					echo'Sizda qurilish uchun mablag\' yetarli emas!<br/><a href="build.php">Orqaga</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");
					echo'<div class="list2">Sizning qurilishlar <b>'.$b['name'].'</b>!</div><div class="gmenu"><a href="build.php">Orqaga</a></div>';
				}
			}
		}
		else
		{
			echo'<div class="list2">Bunaqa qurilish mavjud emas!</div><div class="gmenu"><a href="build.php">Orqaga</a></div>';
		}
		
	}
	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>Qurilishlar <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/>Daromad: <b>$'.$b['plus'].'</b></td></tr>';
				}
				
				echo'</table>';
			}
			else
			{
				echo'<div class="list2"> <b>'.$datac['name'].'</b> klubida qurilishlar yo\'q!</div>';
			}
		}
		else
		{
			echo'<div class="list2">Bunaqa klub yo\'q!</div>';
		}
		
	}
	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>Mavjud qurilishlar</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/>Daromad: <b>$'.$b['plus'].'</b><br/><a href="build.php?new='.$b['id'].'"><button>Qurish</button></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>Mening qurilishlarim</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'>Menejer</a></div>";
}

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