<?php
include_once ('./core/base.php');
falseauth();

$header = 'Инвентарь';
include_once ('./core/head.php');

$_GET['place'] = fl($_GET['place']);
$_GET['type'] = fl($_GET['type']);

echo '<div style="position: relative;"><div class="b"><div class="title">Инвентарь</div>';

switch($_GET['place']){

default:

$itemsnr = $base -> query('SELECT * FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `what` = "clothing" AND `equip` = "no"') -> num_rows;
$items = $base -> query('SELECT * FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `what` = "clothing" AND `equip` = "no"');

//Надеваем вещи
$equip = fl($_GET['equip']);
if($equip){
	$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$equip.'" AND `user` = "'.$u['id'].'" AND `what` = "clothing" AND `equip` = "no" LIMIT 1') -> fetch_assoc();
	if($equipitem != 0){
		if($uclan){
		$u['endurance'] = $u['endurance'] - $uclan['earth_altar'];
		$u['intellect'] = $u['intellect'] - $uclan['water_altar'];
		$u['agility'] = $u['agility'] - $uclan['wind_altar'];
		$u['skill'] = $u['skill'] - $uclan['fire_altar'];
		}
		if($u['equip_'.$equipitem['type'].''] != 0){
			$equippeditem = $base -> query('SELECT * FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `id` = "'.$u['equip_'.$equipitem['type'].''].'" LIMIT 1') -> fetch_assoc();
			$base -> query('UPDATE `users` SET
			`p_att` = "'.(($u['p_att']-$equippeditem['p_att'])+$equipitem['p_att']).'",
			`p_def` = "'.(($u['p_def']-$equippeditem['p_def'])+$equipitem['p_def']).'",
			`m_att` = "'.(($u['m_att']-$equippeditem['m_att'])+$equipitem['m_att']).'",
			`m_def` = "'.(($u['m_def']-$equippeditem['m_def'])+$equipitem['m_def']).'",
			`endurance` = "'.(($u['endurance']-$equippeditem['endurance'])+$equipitem['endurance']).'",
			`intellect` = "'.(($u['intellect']-$equippeditem['intellect'])+$equipitem['intellect']).'",
			`agility` = "'.(($u['agility']-$equippeditem['agility'])+$equipitem['agility']).'",
			`skill` = "'.(($u['skill']-$equippeditem['skill'])+$equipitem['skill']).'",
			`equip_'.$equipitem['type'].'` = "'.$equipitem['id'].'"
			WHERE `id` = "'.$u['id'].'" LIMIT 1');
			$base -> query('UPDATE `users_items` SET `equip` = "no" WHERE `user` = "'.$u['id'].'" AND `id` = "'.$equippeditem['id'].'" LIMIT 1');
			$base -> query('UPDATE `users_items` SET `equip` = "yes" WHERE `user` = "'.$u['id'].'" AND `id` = "'.$equipitem['id'].'" LIMIT 1');
			$_SESSION['message'] = 'Вещь надета.';
			header('Location: /inventory/'.$equipitem['type'].'/');
		}else{
			$base -> query('UPDATE `users` SET
			`p_att` = "'.($u['p_att']+$equipitem['p_att']).'",
			`p_def` = "'.($u['p_def']+$equipitem['p_def']).'",
			`m_att` = "'.($u['m_att']+$equipitem['m_att']).'",
			`m_def` = "'.($u['m_def']+$equipitem['m_def']).'",
			`endurance` = "'.($u['endurance']+$equipitem['endurance']).'",
			`intellect` = "'.($u['intellect']+$equipitem['intellect']).'",
			`agility` = "'.($u['agility']+$equipitem['agility']).'",
			`skill` = "'.($u['skill']+$equipitem['skill']).'",
			`equip_'.$equipitem['type'].'` = "'.$equipitem['id'].'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			$base -> query('UPDATE `users_items` SET `equip` = "yes" WHERE `user` = "'.$u['id'].'" AND `id` = "'.$equipitem['id'].'" LIMIT 1');
			$_SESSION['message'] = 'Вещь надета.';
			header('Location: /inventory/'.$equipitem['type'].'/');
		}
	}else{
		$_SESSION['message'] = 'Ошибка.';
		header('Location: /');
		exit;
	}
}

if($_GET['type'] == '') $_GET['type'] = 'right';

//Указатель
if($u['training_step'] == 10){
	echo '<div style="position: absolute; bottom: 17%; right: 80%; z-index: 9999;"><img src="/images/cursor.gif"></div>';
	if($u['equip_right'] == 0){
		if($_GET['type'] != 'right'){
			header('Location: /inventory/right/');
			exit;
		}
	}
	if($u['equip_right'] != 0 AND $u['equip_body'] == 0){
		if($_GET['type'] != 'body'){
			header('Location: /inventory/body/');
			exit;
		}
	}
	if($u['equip_right'] != 0 AND $u['equip_body'] != 0){
		header('Location: /training/next/');
		exit;
	}
}

//if($u['training_step'] == 10 AND $u['equip_right'] == 0 AND $_GET['type'] != 'right'){
//	header('Location: /inventory/right/');
//	exit;
//}

//Сам инвентарь
echo '<center><div class="btn_small_pressed">Вещи</div> <a class="btn_small" href="/inventory/res/">Расходники</a></center>';

echo '<div class="line"></div>';

echo '<center>';
if($_GET['type'] == 'right') echo '<div class="btn_small_pressed"><small>П. Рука</small></div> ';
else echo '<a class="btn_small" href="/inventory/right/"><small>П. Рука</small></a> ';
if($_GET['type'] == 'left') echo '<div class="btn_small_pressed"><small>Л. Рука</small></div> ';
else echo '<a class="btn_small" href="/inventory/left/"><small>Л. Рука</small></a> ';
if($_GET['type'] == 'head') echo '<div class="btn_small_pressed"><small>Голова</small></div> ';
else echo '<a class="btn_small" href="/inventory/head/"><small>Голова</small></a> ';
if($_GET['type'] == 'body') echo '<div class="btn_small_pressed"><small>Торс</small></div> ';
else echo '<a class="btn_small" href="/inventory/body/"><small>Торс</small></a> ';
if($_GET['type'] == 'hands') echo '<div class="btn_small_pressed"><small>Руки</small></div> ';
else echo '<a class="btn_small" href="/inventory/hands/"><small>Руки</small></a> ';
if($_GET['type'] == 'foot') echo '<div class="btn_small_pressed"><small>Обувь</small></div>';
else echo '<a class="btn_small" href="/inventory/foot/"><small>Обувь</small></a>';
echo '<br/>';
if($_GET['type'] == 'necklace') echo '<div class="btn_small_pressed"><small>Ожерелья</small></div> ';
else echo '<a class="btn_small" href="/inventory/necklace/"><small>Ожерелья</small></a> ';
if($_GET['type'] == 'ring') echo '<div class="btn_small_pressed"><small>Кольца</small></div>';
else echo '<a class="btn_small" href="/inventory/ring/"><small>Кольца</small></a>';
echo '</center>';

$count = $base->query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `type` = '".$_GET['type']."' AND `equip` = 'no'") -> num_rows;
$k_page = k_page($count,10);
$page = page($k_page);
$start = 10*$page-10;
$sql = $base->query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `type` = '".$_GET['type']."' AND `equip` = 'no' ORDER BY `id` DESC LIMIT $start,10");
if($count > 0){
	while($item = $sql->fetch_assoc()){
		echo ''.inventory_item($item['id']).'';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>Этот раздел пуст.</center><br/>';
}

break;

case 'resource':

if($u['training_step'] > 0){
	header('Location: /inventory/');
	exit;
}

//Ресурсы
echo '<center><a class="btn_small" href="/inventory/">Вещи</a> <div class="btn_small_pressed">Расходники</div></center>';

echo '<div class="line"></div>';

if($_GET['type'] == '') $_GET['type'] = 'resource';

echo '<center>';
if($_GET['type'] == 'resource') echo '<div class="btn_small_pressed"><small>Ресурсы</small></div> ';
else echo '<a class="btn_small" href="/inventory/res/resource/"><small>Ресурсы</small></a> ';
if($_GET['type'] == 'potion') echo '<div class="btn_small_pressed"><small>Зелья</small></div> ';
else echo '<a class="btn_small" href="/inventory/res/potion/"><small>Зелья</small></a> ';
if($_GET['type'] == 'scroll') echo '<div class="btn_small_pressed"><small>Свитки</small></div> ';
else echo '<a class="btn_small" href="/inventory/res/scroll/"><small>Свитки</small></a> ';
if($_GET['type'] == 'rune') echo '<div class="btn_small_pressed"><small>Руны</small></div> ';
else echo '<a class="btn_small" href="/inventory/res/rune/"><small>Руны</small></a> ';
echo '</center>';

$count = $base->query("SELECT * FROM `users_items` WHERE `what` = '".$_GET['type']."' AND `user` = '".$u['id']."'") -> num_rows;
$k_page = k_page($count,10);
$page = page($k_page);
$start = 10*$page-10;
$sql = $base->query("SELECT * FROM `users_items` WHERE `what` = '".$_GET['type']."' AND `user` = '".$u['id']."' ORDER BY `id` DESC LIMIT $start,10");
if($count > 0){
	while($item = $sql->fetch_assoc()){
		echo ''.inventory_res($item['id']).'';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>Этот раздел пуст.</center><br/>';
}

break;

}

echo '<br/><a class="btn" href="javascript:history.go(-1)"><img src="/images/icons/back.png"> Назад</a>';

echo '</div></div>';

include_once ('./core/foot.php');
?>