<?php
$load=array("weapon");
$title ='Магазин снаряжения!';
$paginate = 1;
include_once '../protected/sys.php';
if (!$user)
{
	header("Location:/");
	exit;
}

echo "<div class='menu'>";
if(isset($_GET['buy']) and isset($_GET['id'])){
	$id=intval($_GET['id']);
	$rows=$db->rows("SELECT * FROM `shop_items` WHERE `id`=?",array($id));
		if($rows==0){
			$_SESSION['error']="Нет такой вещи!";
			header("Location: ?");
		} else{
			$weap=$db->fetch("SELECT * FROM `shop_items` WHERE `id`=?",array($id));
			if($user['gold']<$weap['money']){
				$_SESSION['error']="У вас недостаточно золото!";
				header("Location: ?type=".$weap['type']."");
			} else {
				$set_1=$db->query("INSERT INTO `weapons` SET `id_user`=?,`str`=?,`def`=?,`hp`=?,`type`=?, `status`=?, `image`=?,`lvl`=?,`name`=?,`money`=?",array($user['id'],$weap['str'],$weap['def'],$weap['hp'],$weap['type'],"no",$weap['image'],$weap['lvl'],$weap['name'],floor($weap['money']/2)));
				$set_2=$db->query("UPDATE `users` SET `gold`=? WHERE `id`=?",array($user['gold']-$weap['money'],$user['id']));
$db->query("INSERT INTO `log` SET `user`=?,`type`=?,`text`=?,`time`=?",array($user['id'],"Снаряжение"," $weap " ,time()));

			}
		}
}


if(isset($_GET['type'])){
	$c = 10;
	$k_post = $db->rows("SELECT * FROM `shop_items` WHERE `type`=?",array($_GET['type']));
	$k_page = k_page($k_post,$c);
	$page = page($k_page);
	$start = $c*$page-$c;
	$db->Emulate(FALSE);
	if($k_post==0){
		$_SESSION['error']="Неверная категория или нет вещей в данной категории";
		header("Location: ?");		
	} else {
		$weap=$db->fetchAll("SELECT * FROM `shop_items` WHERE `type`=? ORDER BY `lvl` ASC LIMIT ?,?",array($_GET['type'],$start,$c));
			foreach ($weap as $wea)
			{
echo "<div class='menu'>";
		$class_weap=new weap($db);
		$class_weap->view($wea['id'],$user,"yes","shop_items");
		echo "<a href='?id=".$wea['id']."&buy'>Купить <img src='/imgData/static/gold.png'>".$wea['money']."</a>";
echo "</div>";
		}
	}
}




if(!isset($_GET['type']) AND !isset($_GET['id'])){
?>
<div class="content">
<a class="menu" href ="?type=w_head"/>Шлем</a></li>
<a class="menu" href ="?type=w_shoulders"/>Наплечник</a></li>
<a class="menu" href ="?type=w_torso"/>Торс</a></li>
<a class="menu" href ="?type=w_mittens"/>Рукавицы</a></li><a class="menu" href ="?type=w_weapon"/>Оружие</a></li>
<a class="menu" href ="?type=w_pants"/>Штаны</a></li>
<a class="menu" href ="?type=w_boots"/>Обувь</a></li>
<a class="menu" href ="?type=w_shield"/>Щит</a></li>
</div>
<?
}

echo "</div>";

include_once $config['root'].'/protected/footermain.php';