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

$header = 'Купец';
include_once ('./core/head.php');

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

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

if($_GET['sell_all_common'] == 'question'){
	$_SESSION['message'] = '<center>Будут проданы все вещи обычного качества, кроме надетых.<br/>
	<a class="btn_small" href="/city/trader/sell_all_common/yes/"><img src="/images/icons/yes.png" width="20px"></a>
	 <a class="btn_small" href="/city/trader/"><img src="/images/icons/no.png" width="20px"></a></center>';
	header('Location: /city/trader/');
	exit;
}elseif($_GET['sell_all_common'] == 'yes'){
	$items_cost = $base -> query('SELECT SUM(cost) AS `sum` FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `mark` = "0" AND `what` = "clothing" AND `equip` = "no"') -> fetch_assoc();
	$sum = $items_cost['sum'] - ($items_cost['sum'] / 4);
	$base -> query('UPDATE `users` SET `silver` = `silver` + "'.$sum.'" WHERE `id` = "'.$u['id'].'"');
	$base -> query('DELETE FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `mark` = "0" AND `what` = "clothing" AND `equip` = "no"');
	$_SESSION['message'] = '<b>Обычные</b> вещи проданы на сумму <img src="/images/icons/silver.png"><b>'.$sum.'</b>';
	header('Location: /city/trader/');
	exit;
}

	$items_cost = $base -> query('SELECT SUM(cost) AS `sum` FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `mark` = "0" AND `what` = "clothing" AND `equip` = "no"') -> fetch_assoc();
	$sum = $items_cost['sum'] - ($items_cost['sum'] / 4);
	echo $sum;

if($_GET['sell'] != ''){
	$item_q = $base -> query('SELECT * FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `id` = "'.$_GET['sell'].'" LIMIT 1');
	$item_nr = $item_q -> num_rows;

	if($item_nr > 0){
		$item = $item_q -> fetch_assoc();
		$item['cost'] = intval($item['cost'] - ($item['cost'] / 4));
		if($item['what'] == 'clothing'){
			$base -> query('DELETE FROM `users_items` WHERE `id` = "'.$item['id'].'"');
			$base -> query('UPDATE `users` SET `silver` = `silver` + "'.$item['cost'].'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
			$_SESSION['message'] = 'Предмет '.$item['name'].' продан за '.n_f($item['cost']).' серебра.';
			header('Location: /city/trader/'.$item['type'].'/');
			exit;
		}else{
			if($_POST['amount'] != ''){
				if($_POST['amount'] > $item['amount']){
					$_SESSION['message'] = 'Ошибка.';
					header('Location: /city/trader/sell/'.$item['id'].'/');
					exit;
				}elseif($_POST['amount'] <= 0){
					$_SESSION['message'] = 'Ошибка.';
					header('Location: /city/trader/sell/'.$item['id'].'/');
					exit;
				}elseif($_POST['amount'] == $item['amount']){
					$base -> query('DELETE FROM `users_items` WHERE `id` = "'.$item['id'].'"');
					$base -> query('UPDATE `users` SET `silver` = `silver` + "'.($item['cost'] * $_POST['amount']).'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
					$_SESSION['message'] = 'Предмет '.$item['name'].' '.$_POST['amount'].'шт. продан за '.n_f($item['cost'] * $_POST['amount']).' серебра.';
					header('Location: /city/trader/res/'.$item['what'].'/');
					exit;
				}elseif($_POST['amount'] < $item['amount']){
					$base -> query('UPDATE `users_items` SET `amount` = `amount` - "'.$_POST['amount'].'" WHERE `id` = "'.$item	['id'].'" LIMIT 1');
					$base -> query('UPDATE `users` SET `silver` = `silver` + "'.($item['cost'] * $_POST['amount']).'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
					$_SESSION['message'] = 'Предмет '.$item['name'].' '.$_POST['amount'].'шт. продан за '.n_f($item['cost'] * $_POST['amount']).' серебра.';
					header('Location: /city/trader/res/'.$item['what'].'/');
					exit;
				}
			}else{
				echo '<div class="b">';
				echo '<div class="title">Продажа '.$item['name'].'</div>';
				echo '<center>';
				echo '<form action="" method="POST">';
				echo '<input type="text" name="amount" placeholder="Введите количество."/> <br/>';
				echo '<input type="submit" name="sell" value="Продать"></br>';
				echo '</form>';
				echo '</center>';
				echo '<a class="btn" href="javascript:history.go(-1)"><img src="/images/icons/back.png"> Назад</a>';
				echo '</div>';
				include_once ('./core/foot.php');
				exit;
			}
		}
	}else{
		$_SESSION['message'] = 'Ошибка.';
		header('Location: /city/trader/');
		exit;
	}
}

echo '<div class="b"><div class="title">Купец</div>';

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

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"');

echo '<center><div class="btn_small_pressed">Вещи</div> <a class="btn_small" href="/city/trader/res/">Расходники</a></center>';

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

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

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

echo '<a class="btn" href="/city/trader/sell_all_common/question/"><img src="/images/icons/silver_24.png"> Продать всё <b>Обычное</b></a>';

$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 ''.trader_item($item['id']).'';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>Этот раздел пуст.</center><br/>';
}

break;

case 'resource':

//Ресурсы
echo '<center><a class="btn_small" href="/city/trader/">Вещи</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="/city/trader/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="/city/trader/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="/city/trader/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="/city/trader/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 ''.trader_res($item['id']).'';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>Этот раздел пуст.</center><br/>';
}

break;

}

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

echo '</div>';

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

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