<?php
session_start();
include_once "inc/db_connect.php";
include_once "inc/fnc.php";
header('Content-Type: text/html;charset=utf8');
$err = "";
if(isset($_POST['regok'])){unset($_SESSION['reg_ok']);}
if(!isset($_SESSION['user'])){
	header("Location: index.php?");
	exit();
}else{
	$data_user = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `nick`='".$_SESSION['user']."'")) or die(mysql_error());
{
include 'head.php';
if($data_user['level']<=4)
{
echo "<div class='mono'>";
echo' <center>Выставка доступна с 5 уровня.<br></center>';
echo "</div>";
include 'foot.php';
exit;
}
?>
<div class="title">Выставка</div>
<?php
if($err) echo "<div class='xar'>" , $err , "</div>";
if(isset($_POST['ok'])){
	if(isset($_POST['idcar'])){
		$auto = mysql_query("SELECT * FROM vis WHERE id='".$_POST['idcar']."'") or die(mysql_error());
		$auto_row = mysql_fetch_array($auto);
		if(abs($data_user['money2']) < $auto_row['price']){
			echo "<div class='menu'><center>Извини у тебя не хватает золотых монет!</center></div>";}
			if(abs($data_user['turnirnie']) < $auto_row['price2']){
			echo "<div class='menu'><center>Извини у тебя не хватает Тур. очков!</center></div>";
		}else{
			$query = mysql_query("SELECT COUNT(id_car) FROM mycars WHERE id_user='".$data_user['id']."' AND id_car='".$_POST['idcar']."'") or die(mysql_error());
				if(mysql_result($query, 0) > 0){
					echo "<div class='menu'><center>Эта машина уже стоит у тебя в гараже!</center></div>";
				}else{
						$okcar1 = mysql_query("INSERT INTO mycars(id_car,id_user,car,carname,speed) VALUES('".$_POST['idcar']."','".$data_user['id']."','".$auto_row['car']."','".$auto_row['carname']."','".$auto_row['speed']."')") or die(mysql_error());
						if($okcar1 == true){
						$resmon = abs($data_user['money2']) - $auto_row['price'];
						$resturnirnie = abs($data_user['turnirnie']) - $auto_row['price2'];
						if($auto_row['ogran'] == 0){
						     echo "<div class='menu'><center>Все авто данного типа раскуплены!</center></div>";
						} else {
						mysql_query("update `vis` set `ogran` = `ogran` - '1' where `id` = '".mysql_real_escape_string(intval($_POST['idcar']))."'");
						mysql_query("UPDATE `users` SET money2='".$resmon."',turnirnie='".$resturnirnie."',car='".$auto_row['car']."',carname='".$auto_row['carname']."',speed='".$auto_row['speed']."' WHERE nick='".$_SESSION['user']."'") or die(mysql_error());
						$_SESSION['carokyes'] = "carok";
						echo "<meta http-equiv='refresh' content='0; URL=index.php?'>";
						}
						exit();
				}
				}
		}
	}
}
$auto = mysql_query("SELECT * FROM vis WHERE `level` <= '".$data_user['level']."'") or die(mysql_error());
$auto_row = mysql_fetch_array($auto);
do{
echo "<div class='mono'>Уровень: <font color='DodgerBlue'><img src='img/lev.png' alt='.' width='16' height='16'/> <b>$auto_row[level]</b></font><br/>";
		printf("<b>%s</b><br/><center><img src='%s' alt='%s'/></center><br/>
<div class='okcar'> Цена:<br>- Золота: <font color='DodgerBlue'> %s </font><br/>
- Тур. очков: <font color='DodgerBlue'> %s </font><br/>
 Мощность: <font color='DodgerBlue'> %s</font><br/>
 Количество: <font color='DodgerBlue'> ".$auto_row['ogran']."</font>
		
		<form action='' method='post'>
			<input type='hidden' name='idcar' value='".$auto_row['id']."' />
			<center><input type='submit' class='btn' name='".($auto_row['ogran'] == 0 ? "" : "ok")."' value='".($auto_row['ogran'] == 0 ? "Авто раскупили" : "Купить")."' /></center>
		</form>
		</div></div>
		",$auto_row['carname'],$auto_row['car'],$auto_row['carname'],$auto_row['price'],$auto_row['price2'],$auto_row['speed']);
}while($auto_row = mysql_fetch_array($auto));
?></div>
<?
}
}
include 'foot.php';
?>