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




switch($sd){
default:
$okach = mysql_num_rows(mysql_query("SELECT * FROM `us_achievements` WHERE `user`='$uid' and `status`='0'"));
echo "Все [<a href=?sd=ok>Завершённые</a> <small>($okach)</small>]";

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

echo "</div>";

While($w = mysql_fetch_assoc($q)){
    
    $ua = mysql_query("SELECT * FROM `us_achievements` WHERE `user`='$uid' and `ach_id`='$w[id]'");
    $uw = mysql_fetch_assoc($ua);
    $ue = mysql_num_rows($ua);
    
    if($ue>='1'){
        echo "<div class='ach'><b>$w[name]</b><br>$w[text]<br>";
        if($uw['status']=='0'){
            echo "Награда: ";
            if($w['exp']>='1'){echo "".number_format($w['exp'])." <img src='img/exp.png'> ";}
            if($w['silver']>='1'){echo "".number_format($w['silver'])." <img src='img/silver.png'> ";}
            if($w['almaz']>='1'){echo "".number_format($w['almaz'])." <img src='img/almaz.png'>";}
            echo "<br><a href=?sd=award&id=$uw[id]>Получить награду</a>";
        }
        echo "</div>";
    }else{
        echo "<div class='achno'><b>$w[name]</b><br>$w[text]<br>";
            echo "Награда: ";
            if($w['exp']>='1'){echo "".number_format($w['exp'])." <img src='img/exp.png'> ";}
            if($w['silver']>='1'){echo "".number_format($w['silver'])." <img src='img/silver.png'> ";}
            if($w['almaz']>='1'){echo "".number_format($w['almaz'])." <img src='img/almaz.png'>";}
        echo "</div>";
    }
    
    
}

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

	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 'ok':

echo "[<a href='achievements.php'>Все</a>] [Завершённые]";

    $num = 10; 
	$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
	$result = mysql_query("SELECT * FROM `us_achievements` WHERE `user`='$uid' and `status`='0'") 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 `us_achievements` WHERE `user`='$uid' and `status`='0' ORDER BY `id` DESC LIMIT $start, $num");

echo "</div>";

While($w = mysql_fetch_assoc($q)){
    
    $ua = mysql_query("SELECT * FROM `achievements` WHERE `id`='$w[ach_id]'");
    $uw = mysql_fetch_assoc($ua);
    $ue = mysql_num_rows($ua);
    
    if($ue>='1'){
        echo "<div class='ach'><b>$uw[name]</b><br>$uw[text]<br>";
        if($w['status']=='0'){
            echo "Награда: ";
            if($uw['exp']>='1'){echo "".number_format($uw['exp'])." <img src='img/exp.png'> ";}
            if($uw['silver']>='1'){echo "".number_format($uw['silver'])." <img src='img/silver.png'> ";}
            if($uw['almaz']>='1'){echo "".number_format($uw['almaz'])." <img src='img/almaz.png'>";}
            echo "<br><a href=?sd=award&id=$w[id]>Получить награду</a>";
        }
        echo "</div>";
    }else{
        echo "<div class='achno'><b>$uw[name]</b><br>$uw[text]<br>";
            echo "Награда: ";
            if($uw['exp']>='1'){echo "".number_format($uw['exp'])." <img src='img/exp.png'> ";}
            if($uw['silver']>='1'){echo "".number_format($uw['silver'])." <img src='img/silver.png'> ";}
            if($uw['almaz']>='1'){echo "".number_format($uw['almaz'])." <img src='img/almaz.png'>";}
        echo "</div>";
    }
    
    
}

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

	if ($page != 1) $pervpage = '<a href=?sd=ok&page=1>««</a> 
                               <a href=?sd=ok&page='. ($page - 1) .'>«</a> '; 
	if ($page != $total) $nextpage = ' <a href=?sd=ok&page='. ($page + 1) .'>»</a> 
                                   <a href=?sd=ok&page=' .$total. '>»»</a>'; 
	if($page - 2 > 0) $page2left = ' <a href=?sd=ok&page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
	if($page - 1 > 0) $page1left = '<a href=?sd=ok&page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
	if($page + 2 <= $total) $page2right = ' | <a href=?sd=ok&page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
	if($page + 1 <= $total) $page1right = ' | <a href=?sd=ok&page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 
    
	echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 


break;


case 'award':
$q = mysql_query("SELECT * FROM `us_achievements` WHERE `user`='$uid' and `id`='$id' and `status`='0'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: achievements.php');
    exit;
}


$a = mysql_fetch_assoc($q);
$w = mysql_fetch_assoc(mysql_query("SELECT * FROM `achievements` WHERE `id`='$a[ach_id]'"));

mysql_query("UPDATE `us_achievements` SET `status`='1' WHERE `id`='$id' and `user`='$uid'");

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

header('Location: achievements.php');
exit;

break;


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