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




switch($sd){
default:



echo "Аукцион(Вещи) | <a href='/complekt'>Магазин(Комплекты)</a><hr>";

$num = 10; 
$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
$result = mysql_query("SELECT * FROM `au_item`") 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 `au_item` LIMIT $start, $num");

While($w = mysql_fetch_assoc($q)){
    
    $it = mysql_fetch_assoc(mysql_query("SELECT * FROM `item` WHERE `id`='$w[id_item]'"));
    
    echo "<span style='float:left'><img src='/img/shmot/$it[img]' width='38'></span><small>&nbsp;<font color=$it[sort]>$it[name]</font> $it[level] ур.";
	$sq = mysql_query("SELECT * FROM `item` WHERE `user`='$it[user]' and `status`='odet' and `tip`='$it[tip]'");
	$se = mysql_num_rows($sq);
	if($se>='1'){
		$sw = mysql_fetch_assoc($sq);
			if($it['tip']=='weapon'){
				$au = round(($it['min_u']+$it['max_u'])/2);
				$au1 = round(($sw['min_u']+$sw['max_u'])/2);
			}else{
				$au = $it['hp'];
				$au1 = $sw['hp'];
			}
			if($au<$au1){
				$ck = $au1-$au;
				echo "(<font color=red>Хуже -$ck</font>)";
			}
			if($au1<$au){
				$ck = $au-$au1;
				echo "(<font color=lightgreen>Лучше +$ck</font>)";
			}
			}else{
			if($it['tip']=='weapon'){
			$au = round(($it['min_u']+$it['max_u'])/2);
			$au1 = round(($user['min_u']+$user['max_u'])/2);
			
			if($au<$au1){
				$ck = $au1-$au;
				echo "(<font color=red>Хуже -$ck</font>)";
			}
			if($au1<$au){
				$ck = $au-$au1;
				echo "(<font color=lightgreen>Лучше +$ck</font>)";
			}
			}else{
				echo "(<font color=lightgreen>Лучше +$it[hp]</font>)";	
			}
		}
        
    if($w['user']!==$uid){
	   echo "<br>&nbsp;[<a href='/auction/buy/$w[id]'>Купить</a>] за ";
        if($w['silver']!==''){echo "$w[silver] <img src='/img/silver.png'> ";}
        if($w['almaz']!==''){echo "<font color=violet>$w[almaz]</font> <img src='/img/almaz.png'> ";}
        echo "</small><hr>";
    }else{
        echo "<br>&nbsp;Ваш лот (";
        if($w['silver']!==''){echo "$w[silver] <img src='/img/silver.png'> ";}
        if($w['almaz']!==''){echo "<font color=violet>$w[almaz]</font> <img src='/img/almaz.png'> ";}
        echo ")[<a href='/auction/out/$w[id]'>Отменить</a>]</small><hr>";
    }
    
    
}


if ($page != 1) $pervpage = '<a href=?sd=comment&id='.$id.'&page=1>««</a> 
<a href=?sd=comment&id='.$id.'&page='. ($page - 1) .'>«</a> '; 
if ($page != $total) $nextpage = ' <a href=?sd=comment&id='.$id.'&page='. ($page + 1) .'>»</a> 
<a href=?sd=comment&id='.$id.'&page=' .$total. '> »»</a>'; 
if($page - 2 > 0) $page2left = ' <a href=?sd=comment&id='.$id.'&page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
if($page - 1 > 0) $page1left = '<a href=?sd=comment&id='.$id.'&page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
if($page + 2 <= $total) $page2right = ' | <a href=?sd=comment&id='.$id.'&page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
if($page + 1 <= $total) $page1right = ' | <a href=?sd=comment&id='.$id.'&page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 
if($posts>='10' or $page!='1'){
    echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage.'<hr>'; 
}
    
break;


case 'out':
$q = mysql_query("SELECT * FROM `au_item` WHERE `id`='$id' and `user`='$uid'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: /bag');
    exit;
}
$w = mysql_fetch_assoc($q);

mysql_query("DELETE FROM `au_item` WHERE `id`='$id'");

header('Location: /bag');
exit;

break;


case 'stend':
$q = mysql_query("SELECT * FROM `item` WHERE `id`='$id' and `user`='$uid' and `status`='not'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: /bag');
    exit;
}
$w = mysql_fetch_assoc($q);

$t = mysql_num_rows(mysql_query("SELECT * FROM `au_item` WHERE `id_item`='$w[id]'"));
if($t>='1'){
    header('Location: /bag');
    exit;
}


if(empty($_POST['stend'])){
    
    echo "Продать <font color=$w[sort]>$w[name]</font> за:<br>";
    
    echo "<form action='/auction/stend/$id' method='post'>
    <img src='/img/silver.png'> <input type='text' name='silver' class='enter' value='0'><br>
    <img src='/img/almaz.png'> <input type='text' name='almaz' class='enter' value='0'><br>
    <input type='submit' name='stend' class='enter' value='Продать'></form>";
    
}else{
    
    $silver = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_POST['silver'])));
	$almaz = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_POST['almaz'])));
	
 
    
	if($silver<'0' or $almaz<'0'){
		header('Location: /bag');
		exit;
	}
	
	mysql_query("INSERT INTO `au_item` SET `user`='$uid',`id_item`='$w[id]',`silver`='$silver',`almaz`='$almaz'");
    
    header('Location: /auction');
    exit;
    
}


break;


case 'buy':
$q = mysql_query("SELECT * FROM `au_item` WHERE `id`='$id'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: /auction');
    exit;
}
$w = mysql_fetch_assoc($q);

if($user['silver']<$w['silver']){
    header('Location: /auction');
    exit;
}
if($user['almaz']<$w['almaz']){
    header('Location: /auction');
    exit;
}

$it = mysql_fetch_assoc(mysql_query("SELECT * FROM `item` WHERE `id`='$w[id_item]'"));

mysql_query("UPDATE `item` SET `user`='$uid' WHERE `id`='$w[id_item]'");

mysql_query("DELETE FROM `au_item` WHERE `id`='$id'");

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

$silver1=($w['silver']/100)*10;
$almaz1=($w['almaz']/100)*10;

$silver=$w['silver']-$silver1;
$almaz=$w['almaz']-$almaz1;

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


mysql_query("INSERT INTO `mail` SET `user`='1',`to`='$uid',`text`='Вы купили $it[name] за <img src=img/silver.png> $w[silver], <img src=img/almaz.png> $w[almaz]',`time`='".time()."'");

mysql_query("INSERT INTO `mail` SET `user`='1',`to`='$w[user]',`text`='У вас купили $it[name] за <img src=img/silver.png> $silver, <img src=img/almaz.png> $almaz, процент аукциона составил <img src=img/silver.png> $silver1, <img src=img/almaz.png> $almaz1',`time`='".time()."'");


header('Location: /bag');
exit;

break;



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