<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');


if($uid!=='7'){
echo "Магазин закрыт на технические работы!";
include ('tmp/foot.php');
exit;
}



switch($sd){
default:

	

    echo "[<a href='shopweapon.php'>Оружие</a>] [Доспехи] [<a href='shopelixir.php'>Эликсиры</a>] [<a href='shopjewelry.php'>Бижутерия</a>]<br><hr>";
    
    if($_GET['t'] == '' or $_GET['t'] == '1'){$f1 = "Шлемы";}else{$f1 = "<a href='shoparmor.php?t=1'>Шлемы</a>";}
    if($_GET['t'] == '2'){$f2 = "Наплечники";}else{$f2 = "<a href='shoparmor.php?t=2'>Наплечники</a>";}
    if($_GET['t'] == '3'){$f3 = "Доспехи";}else{$f3 = "<a href='shoparmor.php?t=3'>Доспехи</a>";}
    if($_GET['t'] == '4'){$f4 = "Щиты";}else{$f4 = "<a href='shoparmor.php?t=4'>Щиты</a>";}
    if($_GET['t'] == '5'){$f5 = "Перчатки";}else{$f5 = "<a href='shoparmor.php?t=5'>Перчатки</a>";}
    if($_GET['t'] == '6'){$f6 = "Поножи";}else{$f6 = "<a href='shoparmor.php?t=6'>Поножи</a>";}
    if($_GET['t'] == '7'){$f7 = "Сапоги";}else{$f7 = "<a href='shoparmor.php?t=7'>Сапоги</a>";}
    if($_GET['t'] == '8'){$f8 = "Амулеты";}else{$f8 = "<a href='shoparmor.php?t=8'>Амулеты</a>";}

    $_GET['t'] = mysql_real_escape_string(htmlspecialchars($_GET['t']));

    if($_GET['t'] == '' or $_GET['t'] == '1'){$tip = "helmet";}
    if($_GET['t'] == '2'){$tip = "ple4i";}
    if($_GET['t'] == '3'){$tip = "armor";}
    if($_GET['t'] == '4'){$tip = "shield";}
    if($_GET['t'] == '5'){$tip = "gloves";}
    if($_GET['t'] == '6'){$tip = "pants";}
    if($_GET['t'] == '7'){$tip = "boots";}
    if($_GET['t'] == '8'){$tip = "amulet";}

    echo "$f1<br>$f2<br>$f3<br>$f4<br>$f5<br>$f6<br>$f7<br>$f8<hr>";
	
	$num = 10; 
	$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page']))); 
	$result = mysql_query("SELECT * FROM `shop` WHERE `tip`='$tip'") or die(mysql_error()); 
	$posts = mysql_num_rows($result); 
	$total = intval(($posts - 1) / $num) + 1; 
	$page = intval($page); 
	if(empty($page) or $page < 0) $page = 1; 
    if($page > $total) $page = $total; 
	$start = $page * $num - $num; 
 
    $q = mysql_query("SELECT * FROM `shop` WHERE `tip`='$tip' ORDER BY `level` ASC, `silver` ASC LIMIT $start, $num");
    
    While($w = mysql_fetch_assoc($q)){
        
        if($w['level'] > $user['level']){$buys = "[<font color=grey>Купить</font>]";}else{$buys = "<a style='color:gold' href='shoparmor.php?sd=buy&id=$w[id]'>[Купить <small><img src='img/silver.png'> $w[silver]</small>]</a>";}
        echo "<span style='float:left'><img src='/img/shmot/$w[img]' width='50'></span>&nbsp;$w[name] $w[level] ур.<br>&nbsp;Живучесть: $w[hp]<br>
        &nbsp;$buys<hr>";
        
    }
	
	if ($page != 1) $pervpage = '<a href=?t='.$_GET['t'].'&page=1>««</a> 
                               <a href=?t='.$_GET['t'].'&page='. ($page - 1) .'>«</a> '; 
	if ($page != $total) $nextpage = ' <a href=?t='.$_GET['t'].'&page='. ($page + 1) .'>»</a> 
                                   <a href=?t='.$_GET['t'].'&page=' .$total. '>»»</a>'; 

	if($page - 2 > 0) $page2left = ' <a href=?t='.$_GET['t'].'&page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
	if($page - 1 > 0) $page1left = '<a href=?t='.$_GET['t'].'&page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
	if($page + 2 <= $total) $page2right = ' | <a href=?t='.$_GET['t'].'&page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
	if($page + 1 <= $total) $page1right = ' | <a href=?t='.$_GET['t'].'&page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

	echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 

break;

case 'buy':

$q = mysql_query("SELECT * FROM `shop` WHERE `id`='$id'");
$e = mysql_num_rows($q);
$w = mysql_fetch_assoc($q);
if($e == '0'){
    header('Location: shoparmor.php');
    exit;
}

if($user['level'] < $w['level']){
    header('Location: shoparmor.php');
    exit;
}

if($user['almaz'] < $w['almaz']){
    echo "У вас недостаточно денег для покупки.<br><a href='shoparmor.php'>Назад</a>";
    include ('tmp/foot.php');
    exit;
}
if($user['silver'] < $w['silver']){
    echo "У вас недостаточно денег для покупки.<br><a href='shoparmor.php'>Назад</a>";
    include ('tmp/foot.php');
    exit;
}



mysql_query("INSERT INTO `item` SET 
`user`='$uid',
`name`='$w[name]',
`tip`='$w[tip]',
`level`='$w[level]',
`almaz`='$w[almaz]',
`silver`='$w[silver]',
`img`='$w[img]',
`hp`='$w[hp]',
`mp`='$w[mp]',
`min_u`='$w[min_u]',
`max_u`='$w[max_u]',
`status`='not'");

mysql_query("UPDATE `users` SET `almaz`=`almaz`-'$w[almaz]',`silver`=`silver`-'$w[silver]' WHERE `id`='$uid'");

echo "Вы купили <b>$w[name]</b><br><a href='shoparmor.php'>Вернуться</a>";


break;







}
include ('tmp/foot.php');
?>