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

$header = 'Магазин';

include_once ('./core/head.php');

if($uloc['city'] > 0) {

$_GET['what'] = fl($_GET['what']);
$_GET['type'] = fl($_GET['type']);
$_GET['buy'] = fl($_GET['buy']);
$_GET['amount'] = fl($_GET['amount']);
if($_GET['amount'] == '') $amount = 1;
elseif($_GET['amount'] == 10) $amount = 10;
elseif($_GET['amount'] == 50) $amount = 50;

if($_GET['buy'] != ''){
	$itemq = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$_GET['buy'].'" LIMIT 1') -> fetch_assoc();
	if($itemq['what'] == 'clothing'){
		if($itemq['type'] == 'necklace' OR $itemq['type'] == 'ring'){
			$type_shop = 'jewelry';
		}else{
			$type_shop = 'equipment';
		}
	}elseif($itemq['what'] == 'potion'){
		$type_shop = 'potion';
	}
	$item = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$_GET['buy'].'" AND `shop` = "'.$uloc[''.$type_shop.'_shop'].'" LIMIT 1') -> fetch_assoc();
	if($item > 0){
		if($item['what'] == 'potion'){
			if($amount != 1) $cost = $item['cost'] * $amount;
			else $cost = $item['cost'];
		}else{
			$cost = $item['cost'];
		}
		if($u['silver'] >= $cost){
			if($item['what'] == 'clothing'){
					$base -> query('INSERT INTO `users_items` SET
					`user` = "'.$u['id'].'",
					`name` = "'.$item['name'].'",
					`description` = "'.$item['description'].'",
					`image` = "'.$item['image'].'",
					`level` = "'.$item['level'].'",
					`what` = "'.$item['what'].'",
					`type` = "'.$item['type'].'",
					`attack_type` = "'.$item['attack_type'].'",
					`p_att` = "'.$item['p_att'].'",
					`m_att` = "'.$item['m_att'].'",
					`p_def` = "'.$item['p_def'].'",
					`m_def` = "'.$item['m_def'].'",
					`endurance` = "'.$item['endurance'].'",
					`intellect` = "'.$item['intellect'].'",
					`agility` = "'.$item['agility'].'",
					`skill` = "'.$item['skill'].'",
					`max_quality` = "'.$item['max_quality'].'",
					`set` = "'.$item['set'].'",
					`mark` = "'.$item['mark'].'",
					`cost` = "'.($item['cost'] / 2).'",
					`ident_id` = "'.$item['id'].'"');
			}elseif($item['what'] == 'potion'){
				$old_item = $base -> query('SELECT * FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$_GET['buy'].'" LIMIT 1') -> num_rows;
				if($old_item > 0){
					$base -> query('UPDATE `users_items` SET `amount` = `amount` + "'.$amount.'" WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$_GET['buy'].'" LIMIT 1');
				}else{
					$base -> query('INSERT INTO `users_items` SET
					`user` = "'.$u['id'].'",
					`name` = "'.$item['name'].'",
					`description` = "'.$item['description'].'",
					`image` = "'.$item['image'].'",
					`what` = "potion",
					`potion` = "'.$item['potion'].'",
					`hp_regen` = "'.$item['hp_regen'].'",
					`mp_regen` = "'.$item['mp_regen'].'",
					`mark` = "'.$item['mark'].'",
					`amount` = "'.$amount.'",
					`cost` = "'.($item['cost'] / 2).'",
					`ident_id` = "'.$item['id'].'"');
				}
			}
			$base -> query('UPDATE `users` SET `silver` = `silver` - "'.$cost.'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			$_SESSION['message'] = 'Предмет '.$item['name'].' помещён в инвентарь.';
			if($item['what'] == 'clothing'){
				if($item['type'] != 'necklace' AND $item['type'] != 'ring'){
					header('Location: /city/shop/equipment/'.$item['type'].'/');
				}else{
					header('Location: /city/shop/jewelry/'.$item['type'].'/');
				}
			}elseif($item['what'] == 'potion'){
				header('Location: /city/shop/potion/'.$item['potion'].'/');
			}
			exit;
		}else{
			$_SESSION['message'] = 'Недостаточно серебра.';
			if($item['what'] == 'clothing'){
				if($item['type'] != 'necklace' AND $item['type'] != 'ring'){
					header('Location: /city/shop/equipment/'.$item['type'].'/');
				}else{
					header('Location: /city/shop/jewelry/'.$item['type'].'/');
				}
			}elseif($item['what'] == 'potion'){
				header('Location: /city/shop/potion/'.$item['potion'].'/');
			}
			exit;
		}
	}else{
		header('Location: /city/');
		exit;
	}
}

echo '<div class="b">';

switch($_GET['what']){

default:

if($uloc['equipment_shop'] > 0){

echo '<div class="title">Магазин снаряжения</div>';

echo '<center><div class="border"><img style="width: 100%; max-width: 336px;" src="/images/places/equipment_shop.png"></div></center>';

/*echo '<div class="event"><table><tr><td><img src="/images/npcs/equipment_shop.gif"></td><td><small><font color="#E0E0E0"><b>Джозеф</b>:</font><font color="#B1B1B1"> Без хорошего снаряжения, долго не протянешь! Особенно в нынешние времена... Мда... Так что не жалей монет, но выбирай с умом!</small></font></td></tr></table></div>';*/

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

echo '<center>';
if($_GET['type'] == 'right') echo '<div class="btn_small_pressed">П. Рука</div> ';
else echo '<a class="btn_small" href="/city/shop/equipment/">П. Рука</a> ';
if($_GET['type'] == 'left') echo '<div class="btn_small_pressed">Л. Рука</div> ';
else echo '<a class="btn_small" href="/city/shop/equipment/left/">Л. Рука</a> ';
if($_GET['type'] == 'head') echo '<div class="btn_small_pressed">Голова</div> ';
else echo '<a class="btn_small" href="/city/shop/equipment/head/">Голова</a> ';
if($_GET['type'] == 'body') echo '<div class="btn_small_pressed">Торс</div> ';
else echo '<a class="btn_small" href="/city/shop/equipment/body/">Торс</a> ';
if($_GET['type'] == 'hands') echo '<div class="btn_small_pressed">Руки</div> ';
else echo '<a class="btn_small" href="/city/shop/equipment/hands/">Руки</a> ';
if($_GET['type'] == 'foot') echo '<div class="btn_small_pressed">Обувь</div>';
else echo '<a class="btn_small" href="/city/shop/equipment/foot/">Обувь</a>';
echo '</center>';

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

}else{
header('Location: /city/');
exit;
}

break;

case 'jewelry':

if($uloc['jewelry_shop'] > 0){

echo '<div class="title">Ювелирная лавка</div>';

echo '<center><div class="border"><img style="width: 100%; max-width: 336px;" src="/images/places/jewelry_shop.png"></div></center>';

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

echo '<center>';
if($_GET['type'] == 'necklace') echo '<div class="btn_small_pressed">Ожерелья</div> ';
else echo '<a class="btn_small" href="/city/shop/jewelry/necklace/">Ожерелья</a> ';
if($_GET['type'] == 'ring') echo '<div class="btn_small_pressed">Кольца</div> ';
else echo '<a class="btn_small" href="/city/shop/jewelry/ring/">Кольца</a> ';
echo '</center>';

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

}else{
header('Location: /city/');
exit;
}

break;

case 'potion':

if($uloc['potion_shop'] > 0){

echo '<div class="title">Лавка зельевара</div>';

echo '<center><div class="border"><img style="width: 100%; max-width: 336px;" src="/images/places/potion_shop.png"></div></center>';

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

echo '<center>';
if($_GET['type'] == 'hp') echo '<div class="btn_small_pressed">Здоровье</div> ';
else echo '<a class="btn_small" href="/city/shop/potion/hp/">Здоровье</a> ';
if($_GET['type'] == 'mp') echo '<div class="btn_small_pressed">Мана</div> ';
else echo '<a class="btn_small" href="/city/shop/potion/mp/">Мана</a> ';
echo '</center>';

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

}else{
header('Location: /city/');
exit;
}

break;

}

echo '<br/><a class="btn" href="/city/"><img src="/images/icons/back.png"> Назад</a>';

echo '</div>';

}else{
header('Location: /location/');
exit;
}


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