<?php
define('_IN_JOHNCMS', 1);
$headmod = 'tournament';
require_once ("../incfiles/core.php");

$q = @mysql_query("select * from `r_cup` where id='" . $id . "' LIMIT 1;");
$cup = @mysql_fetch_array($q);

$textl = $cup['name'];

require_once ("../incfiles/head.php");
$vaqt = time();
if (empty($cup[id]))
{
    echo display_error('Kuboklar mavjud emas');
    require_once ('../incfiles/end.php');
    exit;
}

echo '<div class="phdr"><b>'.$cup['name'].'</b> '.date("H:i", $cup['time']).'</div>';

echo'<p style="text-align:center"><img src="/images/cup/b_'.$cup[id_cup].'.jpg" alt="Кубок"/></p>';
    
    


$req = mysql_query("SELECT * FROM `r_bilet` where id_cup = '" . $id . "' order by time asc;");
$totalbilet = mysql_num_rows($req);


if ($cup['time'] > $vaqt || $totalbilet < 8)
{
$dostart = $cup['time'] - $vaqt;
$dostartmin = round(($dostart-900)/60);


	///4 5 6 7 8
	

if($totalbilet > 2 and $totalbilet !=8){
	if($totalbilet==3){
		$reqs = mysql_query("SELECT * FROM `fman_clubs` where id = '13' Limit 1;");
$t = mysql_fetch_array($reqs);
$result = @mysql_query("SELECT SUM(fiz) FROM `fman_players` WHERE (`id`='".$t['p1']."' or `id`='".$t['p2']."' or `id`='".$t['p3']."' or `id`='".$t['p4']."' or `id`='".$t['p5']."' or `id`='".$t['p6']."' or `id`='".$t['p7']."' or `id`='".$t['p8']."' or `id`='".$t['p9']."' or `id`='".$t['p10']."' or `id`='".$t['p11']."') AND `team_id`='".$t['id']."' LIMIT 11");


$qw = @mysql_query("select * from `r_bilet` where `id_team`='13' Order by `id_cup` DESC LIMIT 1;");
$bilet = @mysql_fetch_array($qw);
$qs = @mysql_query("select * from `r_cup` where `id`='" . $bilet[id_cup] . "' LIMIT 1;");
$bc = @mysql_fetch_array($qs);

if($bc['status']=='yes'){
		mysql_query("insert into `r_bilet` set 
`time`='".$vaqt."', 
`id_cup`='".$id."', 
`id_team`='".$t[id]."', 
`union_team`='0', 
`name_team`='".$t[name]."', 
;");
	}
	}
		if($totalbilet==4){
		$reqs = mysql_query("SELECT * FROM `fman_clubs` where id = '14' Limit 1;");
$t = mysql_fetch_array($reqs);
$result = @mysql_query("SELECT SUM(fiz) FROM `fman_players` WHERE (`id`='".$t['p1']."' or `id`='".$t['p2']."' or `id`='".$t['p3']."' or `id`='".$t['p4']."' or `id`='".$t['p5']."' or `id`='".$t['p6']."' or `id`='".$t['p7']."' or `id`='".$t['p8']."' or `id`='".$t['p9']."' or `id`='".$t['p10']."' or `id`='".$t['p11']."') AND `team_id`='".$t['id']."' LIMIT 11");

$qw = @mysql_query("select * from `r_bilet` where `id_team`='14' Order by `id_cup` DESC LIMIT 1;");
$bilet = @mysql_fetch_array($qw);
$qs = @mysql_query("select * from `r_cup` where `id`='" . $bilet[id_cup] . "' LIMIT 1;");
$bc = @mysql_fetch_array($qs);

if($bc['status']=='yes'){
		mysql_query("insert into `r_bilet` set 
`time`='".$vaqt."', 
`id_cup`='".$id."', 
`id_team`='".$t[id]."', 
`union_team`='0', 
`name_team`='".$t[name]."', 
;");
	}
	
	
	}
		if($totalbilet==5){
		$reqs = mysql_query("SELECT * FROM `fman_clubs` where id = '15' Limit 1;");
$t = mysql_fetch_array($reqs);
$result = @mysql_query("SELECT SUM(fiz) FROM `fman_players` WHERE (`id`='".$t['p1']."' or `id`='".$t['p2']."' or `id`='".$t['p3']."' or `id`='".$t['p4']."' or `id`='".$t['p5']."' or `id`='".$t['p6']."' or `id`='".$t['p7']."' or `id`='".$t['p8']."' or `id`='".$t['p9']."' or `id`='".$t['p10']."' or `id`='".$t['p11']."') AND `team_id`='".$t['id']."' LIMIT 11");

$qw = @mysql_query("select * from `r_bilet` where `id_team`='15' Order by `id_cup` DESC LIMIT 1;");
$bilet = @mysql_fetch_array($qw);
$qs = @mysql_query("select * from `r_cup` where `id`='" . $bilet[id_cup] . "' LIMIT 1;");
$bc = @mysql_fetch_array($qs);

if($bc['status']=='yes'){
		mysql_query("insert into `r_bilet` set 
`time`='".$vaqt."', 
`id_cup`='".$id."', 
`id_team`='".$t[id]."', 
`union_team`='0', 
`name_team`='".$t[name]."', 
;");
	}

	}
		if($totalbilet==6){
		$reqs = mysql_query("SELECT * FROM `fman_clubs` where id = '16' Limit 1;");
$t = mysql_fetch_array($reqs);
$result = @mysql_query("SELECT SUM(fiz) FROM `fman_players` WHERE (`id`='".$t['p1']."' or `id`='".$t['p2']."' or `id`='".$t['p3']."' or `id`='".$t['p4']."' or `id`='".$t['p5']."' or `id`='".$t['p6']."' or `id`='".$t['p7']."' or `id`='".$t['p8']."' or `id`='".$t['p9']."' or `id`='".$t['p10']."' or `id`='".$t['p11']."') AND `team_id`='".$t['id']."' LIMIT 11");

$qw = @mysql_query("select * from `r_bilet` where `id_team`='16' Order by `id_cup` DESC LIMIT 1;");
$bilet = @mysql_fetch_array($qw);
$qs = @mysql_query("select * from `r_cup` where `id`='" . $bilet[id_cup] . "' LIMIT 1;");
$bc = @mysql_fetch_array($qs);

if($bc['status']=='yes'){
		mysql_query("insert into `r_bilet` set 
`time`='".$vaqt."', 
`id_cup`='".$id."', 
`id_team`='".$t[id]."', 
`union_team`='0', 
`name_team`='".$t[name]."', 
;");
	}
	}
}
echo '<center><form method="post" action="/tournament/say.php?id='.$cup[id].'"><input type="submit" class="write" name="submit" value="Qatnashish" /></form></center></div>';
echo '<div class="phdr" style="font-weight:bold;"> <center> Klublar</center></div>';
echo'<div class="menu"><p>';
while ($arr = mysql_fetch_array($req))
{
$reqs = mysql_query("SELECT * FROM `fman_clubs` where id = '".$arr[id_team]."'");
$team = mysql_fetch_array($reqs);
{

echo '<img src="/man/emblems/' . $team['id'] . '.jpg" width="20" height="20" alt="" style="vertical-align: bottom;"/>';
}
echo '<a href="/man/club.php?id='.$arr[id_team].'"> '.$arr['name_team'].'</a><p>';
}
echo'</div>';
if ($totalbilet > 8)
{
echo '<br/><br/>Qatnashuvchilar soni limitdan ko\'p ular avto tanlab olinadi.';
}

echo '<div class="gmenu" style="font-weight:bold;"> <center> Hammasi: '.$totalbilet.' </center> </div>';


require_once ("../incfiles/end.php");
exit;
}














$g = @mysql_query("select * from `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/4';");
$totalgame = mysql_num_rows($g);

// Проверяем 1/4

//echo '1/8 ВСЕ ЕСТЬ!!!!!!!<br/>';
if ($totalgame == 0)
{
$para = 0;

 $i = 0;
$rosp = mysql_query("SELECT * FROM `r_bilet` where id_cup = '" . $id . "' ORDER BY RAND() LIMIT 8;");
while ($ros = mysql_fetch_array($rosp))
{


if (is_integer($i / 2))
{


$id_team1 = $ros['id_team'];
$name_team1 = $ros['name_team'];


}

if (!is_integer($i / 2))
{


$id_team2 = $ros['id_team'];
$name_team2 = $ros['name_team'];



$para = $para+1;


mysql_query("insert into `r_cupgame` set
`id_chemp`='".$id."',
`s_chemp`='1/4',
`p_chemp`='".$para."',
`time`='".$vaqt."',
`id_team1`='".$id_team1."',
`id_team2`='".$id_team2."',
`name_team1`='".$name_team1."',
`name_team2`='".$name_team2."';");
$lastid = mysql_insert_id();
mysql_query("insert into `fman_matches` set
`id_match`='".$lastid."',
`turn`='lc',
`time`='".($vaqt+180)."',
`end`='0',
`sc1`='0',
`sc2`='0',
`pen1`='0',
`pen2`='0',
`land`='lc',
`tur`='11',
`turnir`='1',
`id1`='".$id_team1."',
`id2`='".$id_team2."',
`kom1`='".$name_team1."',
`kom2`='".$name_team2."'
;") or die(mysql_error());

//$lastid = mysql_insert_id();



}



//echo '<div class="c">'.$name_team2.'</div>';

++$i;
}


}

// УДАЛЯЕМ !!!!
if ($totalgame > 4)
{
mysql_query("delete from `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/4';");
}
















// Выводим 1/4
$req = mysql_query("SELECT * FROM `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/4' order by time desc;");
//$total = mysql_num_rows($req);



echo '<div class="phdr"> <center> 1/4 Final</center></div>';

while ($arr = mysql_fetch_array($req))
{
	
$futmen = mysql_query("SELECT * FROM `fman_matches` where id_match = '" . $arr[id] . "'");
$fm	= mysql_fetch_array($futmen);

$k1 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id_team1] . "' LIMIT 1;");
$kom1 = @mysql_fetch_array($k1);

$k2 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id_team2] . "' LIMIT 1;");
$kom2 = @mysql_fetch_array($k2);


echo '<div class="menu"><center>'.date("d.m H:i", $arr['time']).'</center>';
if($kom1[union_mod]==0){
echo '<center> <img src="' . $home . '/man/emblems/' . $kom1['id'] . '.jpg" width="20" height="20" alt="" style="vertical-align: bottom;"/> ';
}else{
}

echo '<a href="/man/club.php?id='.$arr[id_team1].'">  '.$arr['name_team1'].'</a> - ';



echo '<a href="/man/club.php?id='.$arr[id_team2].'">  '.$arr['name_team2'].'</a>';
if($kom2[union_mod]==0){
echo ' <img src="' . $home . '/man/emblems/' . $kom2['id'] . '.jpg" width="20" height="20" alt="" style="vertical-align: bottom;"/></center> ';
}else{
	
}
if (!empty($arr[rez1]) || !empty($arr[rez2]) || $arr[rez1] == '0' || $arr[rez2] == '0')

{
echo '<center> <a href="/man/match.php?id='.$fm[id].'"><span style="color:green"><b>'.$arr[rez1].':'.$arr[rez2].'</b> </span></a> </center>';
if ($arr[rez1] == $arr[rez2])
{echo ' <center> <a href="/man/match.php?id='.$fm[id].'"><span style="color:green"><b>(pen. '.$arr[pen1].':'.$arr[pen2].')</b> </span></a> </center>';
}





// К то победил?
if ($arr[rez1] > $arr[rez2] || $arr[pen1] > $arr[pen2])
{



if ($arr[p_chemp] == 1)
{
$id_12_1 = $arr['id_team1'];
$name_12_1 = $arr['name_team1'];
}
elseif ($arr[p_chemp] == 2)
{
$id_12_2 = $arr['id_team1'];
$name_12_2 = $arr['name_team1'];
}
elseif ($arr[p_chemp] == 3)
{
$id_12_3 = $arr['id_team1'];
$name_12_3 = $arr['name_team1'];
}
elseif ($arr[p_chemp] == 4)
{
$id_12_4 = $arr['id_team1'];
$name_12_4 = $arr['name_team1'];
}









}
else
{


if ($arr[p_chemp] == 1)
{
$id_12_1 = $arr['id_team2'];
$name_12_1 = $arr['name_team2'];
}
elseif ($arr[p_chemp] == 2)
{
$id_12_2 = $arr['id_team2'];
$name_12_2 = $arr['name_team2'];
}
elseif ($arr[p_chemp] == 3)
{
$id_12_3 = $arr['id_team2'];
$name_12_3 = $arr['name_team2'];
}
elseif ($arr[p_chemp] == 4)
{
$id_12_4 = $arr['id_team2'];
$name_12_4 = $arr['name_team2'];
}




}






}
else
{
	
echo '<div class="menu"> <center> <a href="go.php?id='.$fm['id'].'"><span style="color:green"><b>?:?</b>  </span></a> </center>';
}

echo'</div>';

}











// Проверяем 1/2
if (!empty($id_12_1) && !empty($id_12_2) && !empty($id_12_3) && !empty($id_12_4))
{
//echo '1/4 ВСЕ ЕСТЬ!!!!!!!<br/>';

$gn = @mysql_query("select * from `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/2';");
$totalgame = mysql_num_rows($gn);


if ($totalgame == 0)
{


mysql_query("insert into `r_cupgame` set
`id_chemp`='".$id."',
`s_chemp`='1/2',
`p_chemp`='2',
`time`='".$vaqt."',
`id_team1`='".$id_12_1."',
`id_team2`='".$id_12_2."',
`name_team1`='".$name_12_1."',
`name_team2`='".$name_12_2."';");
$lastid = mysql_insert_id();
mysql_query("insert into `fman_matches` set
`turn`='lc',
`id_match`='".$lastid."',
`time`='".($vaqt+180)."',
`end`='0',
`sc1`='0',
`sc2`='0',
`land`='lc',
`tur`='11',
`turnir`='1',
`id1`='".$id_12_1."',
`id2`='".$id_12_2."',
`kom1`='".$name_12_1."',
`kom2`='".$name_12_2."';") or die(mysql_error());

mysql_query("insert into `r_cupgame` set
`id_chemp`='".$id."',
`s_chemp`='1/2',
`p_chemp`='1',
`time`='".$vaqt."',
`id_team1`='".$id_12_3."',
`id_team2`='".$id_12_4."',
`name_team1`='".$name_12_3."',
`name_team2`='".$name_12_4."'
;");
$lastid = mysql_insert_id();
mysql_query("insert into `fman_matches` set
`turn`='lc',
`id_match`='".$lastid."',
`time`='".($vaqt+180)."',
`end`='0',
`sc1`='0',
`sc2`='0',
`land`='lc',
`tur`='11',
`turnir`='1',
`id1`='".$id_12_3."',
`id2`='".$id_12_4."',
`kom1`='".$name_12_3."',
`kom2`='".$name_12_4."';") or die(mysql_error());

}



}
else
{

require_once ("../incfiles/end.php");
exit;
}

// УДАЛЯЕМ !!!!
if ($totalgame > 2)
{
mysql_query("delete from `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/2';");
}

















// Выводим 1/2
$reqq = mysql_query("SELECT * FROM `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/2' order by time desc;");
//$total = mysql_num_rows($req);


echo '<div class="phdr"> <center> 1/2 Final</center></div>';


while ($arr = mysql_fetch_array($reqq))
{
$futmen = mysql_query("SELECT * FROM `fman_matches` where id_match = '" . $arr[id] . "'");
$fm	= mysql_fetch_array($futmen);

$k1 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id_team1] . "' LIMIT 1;");
$kom1 = @mysql_fetch_array($k1);

$k2 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id_team2] . "' LIMIT 1;");
$kom2 = @mysql_fetch_array($k2);

echo '<div class="menu"><center>'.date("d.m H:i", $arr['time']).'</center>';

if($kom1[union_mod]==0){
echo' <center> <img src="' . $home . '/man/emblems/' . $kom1['id'] . '.jpg" width="20" height="20" alt="" style="vertical-align: bottom;"/> ';
}else{
	
}

echo'<a href="/man/club.php?id='.$arr[id_team1].'">  '.$arr['name_team1'].'</a> - ';



echo'<a href="/man/club.php?id='.$arr[id_team2].'">  '.$arr['name_team2'].'</a>';

if($kom2[union_mod]==0){
echo' <img src="' . $home . '/man/emblems/' . $kom2['id'] . '.jpg" width="20" height="20" alt="" style="vertical-align: bottom;"/></center> ';
}else{	
}

if (!empty($arr[rez1]) || !empty($arr[rez2]) || $arr[rez1] == '0' || $arr[rez2] == '0')

{
echo '<center> <a href="/man/match.php?id='.$fm[id].'"><span style="color:green"><b>'.$arr[rez1].':'.$arr[rez2].'</b>  </span></a></center>';
if ($arr[rez1] == $arr[rez2])
{echo ' <center> <a href="/man/match.php?id='.$fm[id].'"><span style="color:green"><b>(pen. '.$arr[pen1].':'.$arr[pen2].')</b>  </span></a></center>';}




// К то победил?
if ($arr[rez1] > $arr[rez2] || $arr[pen1] > $arr[pen2])
{



if ($arr[p_chemp] == 1)
{
$id_11_1 = $arr['id_team1'];
$name_11_1 = $arr['name_team1'];
}
elseif ($arr[p_chemp] == 2)
{
$id_11_2 = $arr['id_team1'];
$name_11_2 = $arr['name_team1'];
}


}
else
{


if ($arr[p_chemp] == 1)
{
$id_11_1 = $arr['id_team2'];
$name_11_1 = $arr['name_team2'];
}
elseif ($arr[p_chemp] == 2)
{
$id_11_2 = $arr['id_team2'];
$name_11_2 = $arr['name_team2'];
}




}



}
else
{
echo '<center> <a href="go.php?id='.$fm['id'].'"><span style="color:green"><b>?:?</b>  </span></a> </center> ';

}

echo'</div>';



}








// Проверяем 1/1
if (!empty($id_11_1) && !empty($id_11_2))
{
//echo '1/2 ВСЕ ЕСТЬ!!!!!!!<br/>';

$gf = @mysql_query("select * from `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/1';");
$totalgame = mysql_num_rows($gf);


if ($totalgame == 0)
{


mysql_query("insert into `r_cupgame` set
`id_chemp`='".$id."',
`s_chemp`='1/1',
`p_chemp`='1',
`time`='".$vaqt."',
`id_team1`='".$id_11_1."',
`id_team2`='".$id_11_2."',
`name_team1`='".$name_11_1."',
`name_team2`='".$name_11_2."'
;");
$lastid = mysql_insert_id();
mysql_query("insert into `fman_matches` set
`turn`='lc',
`id_match`='".$lastid."',
`time`='".($vaqt+180)."',
`end`='0',
`sc1`='0',
`sc2`='0',
`land`='lc',
`tur`='11',
`turnir`='1',
`id1`='".$id_11_1."',
`id2`='".$id_11_2."',
`kom1`='".$name_11_1."',
`kom2`='".$name_11_2."';") or die(mysql_error());



}



}
else
{
echo '</div>';
require_once ("../incfiles/end.php");
exit;
}

// УДАЛЯЕМ !!!!
if ($totalgame > 1)
{
mysql_query("delete from `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/1';");
}



// Выводим ФИНАЛ
$reqf = mysql_query("SELECT * FROM `r_cupgame` where id_chemp = '" . $id . "' and s_chemp = '1/1' LIMIT 1;");
//$total = mysql_num_rows($req);
$arr = mysql_fetch_array($reqf);


echo '<div class="phdr"> <center>Final</center></div>';



if ($totalgame == 1)
{

$k1 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id_team1] . "' LIMIT 1;");
$kom1 = @mysql_fetch_array($k1);

$k2 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id_team2] . "' LIMIT 1;");
$kom2 = @mysql_fetch_array($k2);

$futmen = mysql_query("SELECT * FROM `fman_matches` where id_match = '" . $arr[id] . "'");
$fm	= mysql_fetch_array($futmen);
echo '<div class="menu"><center>'.date("d.m H:i", $arr['time']).'</center>';

if($kom1[union_mod]==0){
echo'<center> <img src="' . $home . '/man/emblems/' . $kom1['id'] . '.jpg" width="20" height="20" alt="" style="vertical-align: bottom;"/> ';
}else{	
}

echo'<a href="/man/club.php?id='.$arr[id_team1].'"> <font color="red"> '.$arr['name_team1'].'</font></a> - ';



echo'<a href="/man/club.php?id='.$arr[id_team2].'"> <font color="red"> '.$arr['name_team2'].'</font></a>';

if($kom2[union_mod]==0){
echo' <img src="' . $home . '/man/emblems/' . $kom2['id'] . '.jpg" width="20" height="20" alt="" style="vertical-align: bottom;"/></center> ';
}else{	
}
if (!empty($arr[rez1]) || !empty($arr[rez2]) || $arr[rez1] == '0' || $arr[rez2] == '0')
{
echo '<center> <a href="/man/match.php?id='.$fm[id].'"><span style="color:green"><b>'.$arr[rez1].':'.$arr[rez2].'</b>  </span></a> </center>';
if ($arr[rez1] == $arr[rez2])
{echo ' <center> <a href="/man/match.php?id='.$fm[id].'"><span style="color:green"><b>(pen. '.$arr[pen1].':'.$arr[pen2].')</b>  </span></a> </center>';}



// К то победил?
if ($arr[rez1] > $arr[rez2] || $arr[pen1] > $arr[pen2])
{
$id_win_1 = $arr['id_team1'];
$name_win_1 = $arr['name_team1'];
}
else
{
$id_win_1 = $arr['id_team2'];
$name_win_1 = $arr['name_team2'];
}



if ($cup[status] != 'yes')
{
mysql_query("update `r_cup` set `win`='" . $id_win_1 . "', `status`='yes' where id='" . $id . "' LIMIT 1;");

mysql_query("insert into `r_priz` set
`id_cup`='".$cup[id_cup]."',
`time`='".$cup[time]."',
`name`='".$cup[name]."',
`priz`='".$cup[priz]."',
`win`='".$id_win_1."',
`url`='/tournament/cup.php?id=".$id."'
;");

$q1 = @mysql_query("select * from `fman_clubs` where id='" . $id_win_1 . "' LIMIT 1;");
$winkom = @mysql_fetch_array($q1);

$oput = $winkom[fan]+15;
$money = $winkom[budj]+$cup[priz];

$kfcup = round($cup[priz]/100);

$slava = $winkom[slava]+(1*$kfcup);

mysql_query("update `fman_clubs` set `fan`='" . $oput . "', `budj`='" . $money . "' where id='" . $winkom[id] . "' LIMIT 1;");
}

}else
{
echo '<div class="menu"> <center> <a href="go.php?id='.$fm['id'].'"><span style="color:green"><b>?:?</b>  </span></a> </center> </div> ';

}


echo'</div>';


}




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