<?
//
//
$location = true;
require_once($_SERVER['DOCUMENT_ROOT']."/core/func.php");
head("Рюкзак",$uid); # Голова
//
//
$dat = array('1' => 'Обычное', '2' => 'Редкое', '3' => 'Эпическое', '4' => 'Легендарное', '5' => 'Мифическое');
//
$dat2 = array('1' => 'Обычная', '2' => 'Редкая', '3' => 'Эпическая', '4' => 'Легендарная', '5' => 'Мифическая');
//
$color = array('1' => '#32cd32', '2' => '#a335ee', '3' => '#ff8000', '4' => '#05CCFF', '5' => '#f5270d');
//
$case = array('1' => '2', '2' => '4', '3' => '6', '4' => '8', '5' => '10');
//


if(isset($_GET['del'])){
	$id = abs(intval($_GET['del']));
	$inv2 = fch("SELECT * FROM `inv` WHERE `yes` = ? AND `id_user` = ? AND `id` = ? LIMIT 1", array(0,$uid,$id));
	if(!$inv2){
		header('location:?r='.$r);
		exit();
	}
	$silver = $case[$inv2['set']];
	qry("UPDATE `users` SET `metal` = `metal` + ?, `tkan` = `tkan` + ? WHERE `id` = ? LIMIT 1", array($silver,$silver,$uid));
	qry("DELETE FROM `inv` WHERE `id_user` = ? AND `yes` = ? AND `id` = ?", array($uid,0,$inv2['id']));
	msg2("<div style='clear:both;'></div><div class='b-notice trsmgr'><div class='notify-inner'> Вещь успешно продана!</div></div>");
	header('location:?');
	exit();
}

$del_item = abs(intval($_GET['del_item']));


if($del_item >= 1){
	$invs = fch("SELECT * FROM `inv` WHERE `yes` = ? AND `id_user` = ? AND `id` = ? LIMIT 1", array(0,$uid,$del_item));
	if(!$invs){
		header('location:?r='.$r);
		exit();
	}

	$silver = $case[$invs['set']];
	echo "
	<div class = 'tophead2 cntr'>
	Хотите разобрать <font color = '".$color[$invs['set']]."'><b>".$invs['name']."</b></font>?
	<br>
Вы получите <img src = '/images/metal.png' width='17' height='17'> ".$case[$invs['set']]." <img src = '/images/tkan.png' width='17' height='17'> ".$case[$invs['set']]." <br>
	<a href = '?del=".$del_item."'><span class='orange'><u>Да, разобрать</u></span></a> - <a href = '?'><span class='orange'><u>Нет, передумал</u></span></a>
	</div>";
}



if(isset($_GET['view'])){
	$id = abs(intval($_GET['view']));
	$item = fch("SELECT * FROM `inv` WHERE `id_user` = ? AND `yes` = ? AND `id` = ? LIMIT 1", array($uid,0,$id));

	if(!$item){
		header('location:?r='.$r);
		exit();
	}

	$inv = fch("SELECT * FROM `inv` WHERE `type` = ? AND `yes` = ? AND `id_user` = ? LIMIT 1", array($item['type'],1,$uid));


	$param_1 = $inv['power']+$inv['block']+$inv['health'];
	$param_2 = $item['power']+$item['block']+$item['health'];

	if($param_1 >= $param_2){
		$param = "Хуже: <font color='red'>-".($param_1-$param_2)."</font>";
	}
	elseif($param_1 < $param_2){
		$param = "Лучше: <font color='lime'>+".($param_2-$param_1)."</font>";
	}

	$param_11 = $item['power']+$item['block']+$item['health'];
	$param_21 = $inv['power']+$inv['block']+$inv['health'];

	if($param_11 >= $param_21){
		$param2 = "Хуже: <font color='red'>-".($param_11-$param_21)."</font>";
	}
	elseif($param_11 < $param_21){
		$param2 = "Лучше: <font color='lime'>+".($param_21-$param_11)."</font>";
	}

echo "
<div class='tophead'>
<div class='ohh'>
<img class = 'imgItem".$item['set']."' src = '/images/items/".$item['set']."/".$item['type'].".png' width='50' height='50'>
</div>

	<a href = '?view=".$item['id']."'><font color = '".$color[$item['set']]."'>".$item['name']."</font></a> <small>(".$item['smitch']."/".$item['max_smitch'].")</small> <span style='float: right;'><a href='?view=".$item['id']."&del_item=".$item['id']."'><img src='/images/icon_del.png' width='16' height='16'></a></span>
<small>
	<br>".$param."<br><br>
</small>
	</div>
";



//
//
	echo "
	<div class = 'tophead'>
        <img src='/images/type".$item['set'].".png'  width='' height=''> <font color = '".$color[$item['set']]."'>".$dat[$item['set']]." качество</font> <br>
";

if($item['rune'] >= 1)echo "<img class='img2' src='/images/type".$item['rune'].".png'> <font color = '".$color[$item['rune']]."'> ".$dat2[$item['rune']]." руна</font> <font color = 'lime'>+".($item['rune_param']*3)."</font>"; else echo "<img src='/images/type0.png'> Руна не установлена"; 

echo "
        <br><img src='/images/shop.png'  width='17' height='17'> Уровень заточки: <a href='/shop.php?loc=2'><span class='orange'>+".$item['smitch']."</span><br>
	</div>
	<div class = 'tophead'>
	<img src='/images/power.png'> Сила: ".$item['power']."<br>
	<img src='/images/block.png'> Защита: ".$item['block']."<br>
	<img src='/images/health.png'> Здоровье: ".$item['health']."<br>
	</div>
	";

	//if($user['level'] >= $item['level']){
		echo "
			<div class='tophead2 cntr'>
			<form action = '?open=".$item['id']."' method = 'post'>
			<input type = 'submit' name = '' class = '' value = 'Надеть'>
			</form>
			</div>
		";
	//}


	echo "
	<a href = '/bag.php' class='tophead'>Вернуться назад</a>
	";

	foot($uid);
	exit();
}


$count = $BaseConnect->query("SELECT (id) FROM `inv` WHERE `yes` = '0' AND `id_user` = '".$uid."'")->rowCount();


if(isset($_GET['open'])){
	$id = abs(intval($_GET['open']));
	$item2 = fch("SELECT * FROM `inv` WHERE `id_user` = ? AND `yes` = ? AND `id` = ? LIMIT 1", array($uid,0,$id));
	if(!$item2){
		header('location:?r='.$r);
		exit();
	}
	if($user['level'] < $item2['level']){
		err2("Вещь можно надеть с <b>".$item2['level']."</b> уровня</a>");
		header('location:?');
		exit();
	}
	if($count == $user['limit_inv']){
		err2("У вас недостаточно места в рюкзаке, для того чтобы надеть вещь!");
		header('location:?');
		exit();
	}

	$inv = fch("SELECT * FROM `inv` WHERE `type` = ? AND `yes` = ? AND `id_user` = ? LIMIT 1", array($item2['type'],1,$uid));
//
//
	if($inv){
//
$param = (($user['power']-$inv['power'])+$item2['power']);
//
		qry("UPDATE `users` SET `power` = ?, `block` = ?, `health` = ? WHERE `id` = ? LIMIT 1", array($param,$param,$param,$uid));
//
if($item2['smitch'] == 0 && $inv['smitch'] >= 1 && $item2['set'] >= $inv['set']){ 
$stat = ($inv['smitch']*3);
//
qry("UPDATE `inv` SET `power` = `power` + ?, `block` = `block` + ?, `health` = `health` + ?, `smitch` = ? WHERE `id` = ? LIMIT 1", array($stat,$stat,$stat,$inv['smitch'],$item2['id']));
//
qry("UPDATE `inv` SET `power` = `power` - ?, `block` = `block` - ?, `health` = `health` - ?, `smitch` = ? WHERE `id` = ? LIMIT 1", array($stat,$stat,$stat,0,$inv['id']));
//
qry("UPDATE `users` SET `power` = `power` + ?, `block` = `block` + ?, `health` = `health` + ? WHERE `id` = ? LIMIT 1", array($stat,$stat,$stat,$uid));
//
// 
}
//
//
if($item2['rune'] == 0 && $inv['rune'] >= 1 && $item2['set'] >= $inv['set']){ 
$stat2 = $inv['rune_param'];
//
qry("UPDATE `inv` SET `power` = `power` + ?, `block` = `block` + ?, `health` = `health` + ?, `rune` = ?, `rune_param` = ? WHERE `id` = ? LIMIT 1", array($stat2,$stat2,$stat2,$inv['rune'],$inv['rune_param'],$item2['id']));
//
qry("UPDATE `inv` SET `power` = `power` - ?, `block` = `block` - ?, `health` = `health` - ?, `rune` = ?, `rune_param` = ? WHERE `id` = ? LIMIT 1", array($stat2,$stat2,$stat2,0,0,$inv['id']));
//
qry("UPDATE `users` SET `power` = `power` + ?, `block` = `block` + ?, `health` = `health` + ? WHERE `id` = ? LIMIT 1", array($stat2,$stat2,$stat2,$uid));
//
// 
}
//
//
qry("UPDATE `inv` SET `yes` = ? WHERE `id` = ? LIMIT 1", array(0,$inv['id']));
qry("UPDATE `inv` SET `yes` = ? WHERE `id` = ? LIMIT 1", array(1,$item2['id']));
//
//
}else{
//
//
qry("UPDATE `users` SET `power` = `power` + ?, `block` = `block` + ?, `health` = `health` + ? WHERE `id` = ? LIMIT 1", array($item2['power'],$item2['block'],$item2['health'],$uid));
//
qry("UPDATE `inv` SET `yes` = ? WHERE `id` = ? LIMIT 1", array(1,$item2['id']));
//
//
}
//
//
//
//
	msg2("<div style='clear:both;'></div><div class='b-notice trsmgr'><div class='notify-inner'> Вещь успешно надета</div></div>");
	header('location:?');
	exit();
}


if($count <= 0)echo '<div class="tophead cntr">Рюкзак пуст</div>';
$msg_q = $BaseConnect->query("SELECT * FROM `inv` WHERE `id_user` = '".$uid."' AND `yes` = '0' ORDER BY `set` DESC");


while($msg = $msg_q->fetch(PDO::FETCH_ASSOC)){
	$inv = fch("SELECT * FROM `inv` WHERE `type` = ? AND `yes` = ? AND `id_user` = ? LIMIT 1", array($msg['type'],1,$uid));
	$param_1 = $inv['power']+$inv['block']+$inv['health'];
	$param_2 = $msg['power']+$msg['block']+$msg['health'];
	if($param_1 >= $param_2){
		$param = "Хуже: <font color='red'>-".($param_1-$param_2)."</font>";
	}
	elseif($param_1 < $param_2){
		$param = "Лучше: <font color='lime'>+".($param_2-$param_1)."</font>";
	}
	echo "
<div class = 'tophead'> 
<div class='ohh'>
<img class = 'imgItem".$msg['set']."' src = '/images/items/".$msg['set']."/".$msg['type'].".png' width='50' height='50'>
</div>

	<a href = '?view=".$msg['id']."'><font color = '".$color[$msg['set']]."'><u>".$msg['name']."</u></font></a> <small>(".$msg['smitch']."/".$msg['max_smitch'].")</small> <span style='float: right;'><a href='?del_item=".$msg['id']."'><img src='/images/icon_del.png' width='16' height='16'></a></span>
	";

	echo "
<small>
	<br>".$param."<br><br>
</small>
	</div>

	";
	if($user['level'] >= $msg['level']){
		echo "
		<div class='tophead2 cntr'>
		<form action = '?open=".$msg['id']."' method = 'post'>
		<input type = 'submit' name = '' class = '' value = 'Надеть'>
		</form>
		</div>
		";
	}


}
//
//


//
//
foot($uid);
?>