<?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='shoparmor.php'>Доспехи</a>] [<a href='shopelixir.php'>Эликсиры</a>] [<a href='shopjewelry.php'>Бижутерия</a>]<br><hr>";
	
	$num = 10; 
	$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page']))); 
	$result = mysql_query("SELECT * FROM `shop` WHERE `tip`='weapon'") 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`='weapon' 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='shopweapon.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[min_u] - $w[max_u]<br>
        &nbsp;$buys<hr>";
        
    }
	
	
	
	if ($page != 1) $pervpage = '<a href=?page=1>««</a> 
                               <a href=?page='. ($page - 1) .'>«</a> '; 
	if ($page != $total) $nextpage = ' <a href=?page='. ($page + 1) .'>»</a> 
                                   <a href=?page=' .$total. '>»»</a>'; 

	if($page - 2 > 0) $page2left = ' <a href=?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
	if($page - 1 > 0) $page1left = '<a href=?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
	if($page + 2 <= $total) $page2right = ' | <a href=?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
	if($page + 1 <= $total) $page1right = ' | <a href=?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: shopweapon.php');
    exit;
}

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

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


break;

///////////////////////




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