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

$header = 'Чародей';

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

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

//Фильтруем данные
$_GET['what'] = fl($_GET['what']);
$_GET['item'] = fl($_GET['item']);
$_GET['rune'] = fl($_GET['rune']);

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

switch($_GET['what']){

default:

echo '<div class="title">Чародей</div>';

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

echo '<table class="event_info"><tr><td><img src="/images/icons/info.png"></td><td> - Вставлять руну можно только в редкую вещь, либо выше.<br/>
- В одну вещь можно вставить только одну руну.<br/>
- Руна вставляется в вещь навсегда.</td></tr></table>';

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

$count = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `rune` = '0' AND `mark` > '0'") -> 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 `equip` = 'no' AND `rune` = '0' AND `mark` > '0' ORDER BY `id` DESC LIMIT $start,10");
if($count > 0){
	while($item = $sql->fetch_assoc()){
		echo ''.enchanter_item($item['id']).'';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>В вашем инвентаре нет вещей, в которые можно вставлять руны.</center>';
}

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

break;

case 'enchant':

if($_GET['item'] != '' AND $_GET['rune'] != ''){

	$e_item = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `rune` = '0' AND `mark` > '0'  AND `id` = '".$_GET['item']."' LIMIT 1") -> fetch_assoc();
	$e_rune = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'rune' AND `user` = '".$u['id']."' AND `id` = '".$_GET['rune']."' LIMIT 1") -> fetch_assoc();
	if(!$e_item OR !$e_rune){
		$_SESSION['message'] = 'Ошибка.';
		header('Location: /city/enchanter/');
		exit;
	}
	//Высчитываем цену
	if($e_item['mark'] == 1) $e_cost = 1000;
	elseif($e_item['mark'] == 2) $e_cost = 5000;
	elseif($e_item['mark'] == 3) $e_cost = 10000;
	elseif($e_item['mark'] == 4) $e_cost = 25000;
	if($u['silver'] < $e_cost){
		$_SESSION['message'] = 'Недостаточно серебра.';
		header('Location: /city/enchanter/');
		exit;
	}
	//Если всё нормально
	if($e_rune['mark'] == 1) $power = 5;
	if($e_rune['mark'] == 2) $power = 10;
	if($e_rune['mark'] == 3) $power = 15;
	if($e_rune['mark'] == 4) $power = 20;
	$base -> query('UPDATE `users_items` SET
		`'.$e_rune['rune_type'].'` = `'.$e_rune['rune_type'].'` + "'.$power.'",
		`rune_type` = "'.$e_rune['rune_type'].'",
		`rune` = "'.$e_rune['mark'].'"
		WHERE `id` = "'.$e_item['id'].'" LIMIT 1');
	$new_amount = $e_rune['amount'] - 1;
	if($new_amount <= 0) $base -> query('DELETE FROM `users_items` WHERE `id` = "'.$e_rune['id'].'" LIMIT 1');
	else $base -> query('UPDATE `users_items` SET `amount` = "'.$new_amount.'" WHERE `id` = "'.$e_rune['id'].'" LIMIT 1');
	$base -> query('UPDATE `users` SET `silver` = `silver` - "'.$e_cost.'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
	$_SESSION['message'] = 'Руна вставлена успешно.';
	header('Location: /city/enchanter/');
	exit;

}elseif($_GET['item'] != '' AND $_GET['rune'] == ''){

$e_item = $base -> query('SELECT * FROM `users_items` WHERE `what` = "clothing" AND `user` = "'.$u['id'].'" AND `equip` = "no" AND `rune` = "0" AND `id` = "'.$_GET['item'].'" LIMIT 1') -> fetch_assoc();

if(!$e_item){
	$_SESSION['message'] = 'Ошибка.';
	header('Location: /city/enchanter/');
	exit;
}

if($e_item['mark'] == 1) $e_cost = 1000;
elseif($e_item['mark'] == 2) $e_cost = 5000;
elseif($e_item['mark'] == 3) $e_cost = 10000;
elseif($e_item['mark'] == 4) $e_cost = 25000;

echo '<div class="title">Вставляем руну в '.$e_item['name'].'</div>';

echo ''.item($e_item['id']).'';

echo '<div class="line"></div><center>Работа чародея обойдётся тебе в <img src="/images/icons/silver.png">'.n_f($e_cost).'</center><div class="line"></div>';

echo '<div class="title">Выберите руну</div>';

$count = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'rune' 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` = 'rune' AND `user` = '".$u['id']."' ORDER BY `level` DESC LIMIT $start,10");
if($count > 0){
	while($res = $sql->fetch_assoc()){
		if($res['mark'] == '0') $mark = '<font color="#B1B1B1">Обычный';
		if($res['mark'] == '1') $mark = '<font color="#30C030">Редкий';
		if($res['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
		if($res['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
		if($res['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

		if($res['mark'] == '0') $border_color = 'B1B1B1';
		if($res['mark'] == '1') $border_color = '30C030';
		if($res['mark'] == '2') $border_color = '68A8F9';
		if($res['mark'] == '3') $border_color = 'D8CD32';
		if($res['mark'] == '4') $border_color = 'DD4FFF';

		echo '<div class="event_item">
		<table cellpadding="0" cellspacing="0"><tr>
		<td><img src="'.$res['image'].'" width="50px" style="border: 1px solid #'.$border_color.'; border-radius: 1px;"></td>
		<td valign="top" style="padding-left: 5px;"><a href="/res/'.$res['id'].'/">'.$res['name'].'</a><br/>
		<small>'.$mark.'</font><br/>
		<font color="#FFF">Количество: '.$res['amount'].'</font></small></td></tr></table>';
		echo ' <a class="btn_small" href="/city/enchanter/'.$e_item['id'].'/'.$res['id'].'/">Выбрать</a>';
		echo '</div>';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>В вашем инвентаре нет подходящих рун.</center><br/>';
}

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



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

break;

}

echo '</div>';

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


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