<?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';
?>
<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 auto WHERE id='".$_POST['idcar']."'") or die(mysql_error());
		$auto_row = mysql_fetch_array($auto);
		if(abs($data_user['money']) < $auto_row['price']){
			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['money']) - $auto_row['price'];
						mysql_query("UPDATE `users` SET money='".$resmon."',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 auto 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'>Цена: <font color='DodgerBlue'><img src='img/money.png' alt='Баксы' width='16' height='16'/> %s </font><br/>
Мощность: <font color='DodgerBlue'><img src='img/speed.png' alt='Скорость' width='16' height='16'/> %s</font>
		<form action='' method='post'>
			<input type='hidden' name='idcar' value='".$auto_row['id']."' />
			<center><input type='submit' class='btn' name='ok' value='Купить' /></center>
		</form>
		</div></div>
		",$auto_row['carname'],$auto_row['car'],$auto_row['carname'],$auto_row['price'],$auto_row['speed']);
}while($auto_row = mysql_fetch_array($auto));
}
}
include 'foot.php';
?>