<?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_spons` WHERE `id`='$new' LIMIT 1");
		if(mysql_num_rows($sql))
		{
		
			$b = mysql_fetch_assoc($sql);
				
			if($b['pay']>$dataclub['budj'])
			{
				echo'Вам нехватает денег для подписания контракта!<br/><a href="spons.php">Назад</a>';
			}
			else
			{
				mysql_query("DELETE FROM `fman_clubs_spons` WHERE `club_id`='$club_id'");
				mysql_query("INSERT INTO `fman_clubs_spons` SET `club_id`='$club_id', `sid`='$new'");
				mysql_query("UPDATE `fman_clubs` SET `budj`=`budj`-'$b[pay]',`spons`='$b[plus]' where `id`='$club_id' limit 1");
				echo'Вы подписали контракт со спонсором <b>'.$b['name'].'</b>!<br/><a href="spons.php">Назад</a>';
			}
		}
		else
		{
			echo'Такого спонсора нет!<br/><a href="spons.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_spons`.*,`fman_spons`.* FROM `fman_clubs_spons` LEFT JOIN `fman_spons` ON `fman_spons`.`id`=`fman_clubs_spons`.`sid` 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="spons/'.$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
	{
		$new = intval($_GET['new']);
		$sql = mysql_query("SELECT *,(select count(*) from `fman_clubs_spons` where `fman_clubs_spons`.`sid`=`fman_spons`.`id` and `fman_clubs_spons`.`club_id`='$club_id') as `yes` FROM `fman_spons` 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="spons/'.$b['pic'].'" /></td><td><b>'.$b['name'].', $'.$b['pay'].'</b><br/>Доход за матч: <b>$'.$b['plus'].'</b><br/><a href="spons.php?new='.$b['id'].'"><button>Контракт</button></a></td></tr>';
			}
			
			echo'</table>';

			$sql = mysql_query("SELECT `fman_clubs_spons`.*,`fman_spons`.* FROM `fman_clubs_spons` LEFT JOIN `fman_spons` ON `fman_spons`.`id`=`fman_clubs_spons`.`sid` WHERE `club_id`='$club_id' limit 1");
			if(mysql_num_rows($sql))
			{
			echo
			'
				<div class="phdr"><b>Мой спонсор</b></div>
				<table cellpadding="2" rules="rows" style="border:1px solid grey;margin:3px;">
			';
				
				
				$b = mysql_fetch_assoc($sql);
				
				echo'<tr><td><img width="100" height="50" src="spons/'.$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"); 
?>
