<?php

$title = 'Рюзкак';
$paginate=1;
$load=array("weapon");
include_once 'protected/sys.php';

if (!$user)
{
	header("Location:/");
	exit;
}

if(isset($_GET['sell']) and isset($_GET['id_weapon'])){
	$id=intval($_GET['id_weapon']);
	$wea=$db->fetch("SELECT * FROM `weapons` WHERE `id`=?",array($id));
		if($wea['id_user']!=$user['id']){
			$error="Извините эта вещь не пренадлежыт вам!";
		}
		if($wea['status']=="yes"){
			$error="Извините нельзя продать вещи которая одета!";
		}
		if(isset($error)){
			$_SESSION['error']=$error;
			header("Location: ?");
		} else {
			$update=$db->query("UPDATE `users` SET `silver`=? WHERE `id`=?",array($user['silver']+$wea['money'],$user['id']));
			$delete=$db->query("DELETE FROM `weapons` WHERE `id`=?",array($id));
		}

}


if(isset($_GET['wear']) AND isset($_GET['id_weapon'])){
		$weap=$db->fetch("SELECT * FROM `weapons` WHERE `id`=?",array(intval($_GET['id_weapon'])));
			if($user[$weap['type']]!=0){echo "На вас уже надета вещь даного типа!";}
		if($user[$weap['type']]==0 and $weap['id_user']==$user['id'] and $weap['lvl']<=$user['level']){
		$str=$user['str']+$weap['str'];
			$def=$user['def']+$weap['def'];
			$hp=$user['max_hp']+$weap['hp'];
			$set=$db->query("UPDATE `users` SET `str`=?, `def`=?, `max_hp`=?, `".$weap['type']."`=? WHERE `id`=?",array($str,$def,$hp,$weap['id'],$user['id']));
			$set=$db->query("UPDATE `weapons` SET `status`=? WHERE `id`=?",array("yes",$weap['id']));

	} 
	}
			$c = 10;
			$k_post = $db->rows("SELECT * FROM `weapons` WHERE `id_user`=? and `status`=?",array($user['id'],"no"));
			$k_page = k_page($k_post,$c);
			$page = page($k_page);
			$start = $c*$page-$c;
echo "<div class='content'>";

$db->Emulate(FALSE);	
			if($k_post==0){
			echo "Сумка пуста";
			} else {
				$weaps = $db->fetchAll("SELECT * FROM `weapons` WHERE `id_user`=? and `status`=?  LIMIT ?,?",
							array($user['id'],"no",$start,$c));
				foreach ($weaps as $weap)
				{echo "<div class='menu'>";
				  	$class_weap=new weap($db);
					$class_weap->view($weap['id'],$user,"yes","weapons");
					echo "<a href='?wear&id_weapon=".$weap['id']."'>Надеть</a> | <a href='?sell&id_weapon=".$weap['id']."'>Продать <img src='/imgData/static/silver.png'> ".$weap['money']."</a>"; 
				echo "</div>";
				}

			}

echo "</div>";
if ($k_page > 1)
{
	?>
	<div class="hr_arr mt2 mb2 mlr10">
		<div class="alf">
			<div class="art">
				<div class="acn">
				</div>
			</div>
		</div>
	</div>
	<center>
	<?


	str('?',$k_page,$page,"&id=".$id); // Вывод страниц


	?>
	</center>
	<div class="hr_arr mt2 mb2 mlr10">
		<div class="alf">
			<div class="art">
				<div class="acn">
				</div>
			</div>
		</div>
	</div>
	<?
}



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


?>