<?php
include 'core/sys.php';
head('Магазин патронов');
reg();
echo '<div class="row"><img src="img/mesto/shop.png"></div>';
echo '<div class="name">Магазин патронов:</div>';
	if(!empty($_GET['puli'])){
	$puli_id = int($_GET['puli']);
	$kol = int($_POST['kol']);
	$pulya= mysql_fetch_assoc(mysql_query("SELECT * FROM `puli` WHERE `id`='".$puli_id."' LIMIT 1"));
		if(!$pulya OR $kol<1){
		echo '<div id="error">Нету таких пуль/Кол-во меньше 1-го</div>';
		foot();
		exit;
		}
	
	$money = $kol*$pulya['money_one'];
		if($user['money']<$money){
		echo '<div id="error">У вас нету '.$money.' золота</div>';
		foot();
		exit;
		}
	echo '<div class="row">Патроны куплены за <img src="img/money/money.png"> '.$money.'!</div>';
	$test_puli = mysql_fetch_assoc(mysql_query("SELECT * FROM `puli_user` WHERE `user_id`='".$user['id']."' AND `puli_id`='".$puli_id."' LIMIT 1"));
		if(!$test_puli){
		mysql_query("INSERT INTO `puli_user` SET
		`user_id`='".$user['id']."',
		`puli_id`='".$puli_id."',
		`kol`='".$kol."'");
		}else{
		mysql_query("UPDATE `puli_user` SET `kol`=`kol`+".$kol." WHERE `user_id`='".$user['id']."' AND `puli_id`='".$puli_id."'");
		}
	mysql_query("UPDATE `users` SET `money`=`money`-".$money." WHERE `id`='".$user['id']."'");
	foot();
	exit;
	}
$puli_q = mysql_query("SELECT * FROM `puli`");
while($puli = mysql_fetch_assoc($puli_q)){
echo '<div class="row"><img src="img/puli/'.$puli['id'].'.png"> '.$puli['name'].'<br>
К урону: '.$puli['perc_uron'].'%<br>
Цена за 1 шт. <img src="img/money/money.png"> '.$puli['money_one'].'<br>
<form action="shop_puli.php?puli='.$puli['id'].'" method="post">
<input type="text" name="kol" value="1" size="1">
<input type="submit" value="купить">
</form>

</div>';
}
$total = mysql_result(mysql_query("SELECT SUM(kol) FROM `puli_user` WHERE `user_id`='".$user['id']."'"),0);
echo '<div class="name">На моем сладе патронов: <b>'.$total.'</b><br>';
	if($total>0){
		$my_patr_q = mysql_query("SELECT * FROM `puli_user` WHERE `user_id`='".$user['id']."'");
		while($my_patr = mysql_fetch_assoc($my_patr_q )){
		echo '<img src="img/puli/'.$my_patr['puli_id'].'.png"> '.$my_patr['kol'].' ';
		}
	}
echo '</div>';
foot();
?>