<?


include 'connect.php';

//Доступ авторизованным
if(!$u['id']){ header ("Location: aut.php"); exit; }

switch($_GET['act'])
{

//############################################# История #############################################
case 'history':

//Доступ только админам
if($u['admin'] != 1){ header ("Location: index.php"); exit; }

include 'head.php';
echo '<div class="title"><img src= "image/rating.png"> История рейтинга</div>';

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

$count = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `rating_history`"), 0);
if($count > 0){
   $total = intval(($count - 1) / 10) + 1;
   $page = intval($_GET['page']);
   if(empty($page) or $page < 0) $page = 1; 
   if($page > $total) $page = $total;
   $start = $page * 10 - 10;
   $result = mysql_query("SELECT * FROM `rating_history` ORDER BY `time` DESC LIMIT $start, 10");
   while($history = mysql_fetch_assoc($result)){
      $user1 = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$history["user"]."'"));
	  $user2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$history["outuser"]."'"));
      $message = checkout($wall['message']);	  
      echo '<div class="block">';
	  echo '
	  <a href = "page.php?id='.$user1['id'].'"><b>'.$user1['login'].'</b></a>
	  повысил рейтинг';
	  
	  if($user1['id'] == $user2['id']) echo ' себе';
	  else echo ' <a href = "page.php?id='.$user2['id'].'"><b>'.$user2['login'].'</b></a>';
	  
	  echo ' на <b>'.$history['rating'].'</b> за <b>'.$history['money'].'</b> монет.
	  ';
	  echo '</div>';
   }
   navigation($page, $total,'?act=history&');
} else {
   echo '<div class = "block"><div class = "center">Сообщений еще нет. Будь первым!</div></div>';
}


echo '</div>';
include 'foot.php';

break;

//############################################# Дефаулт #############################################
default:

//Выбираем пользователя
if(isset($_GET['id'])){
   $id=num($_GET['id']);
   $req = mysql_query("SELECT * FROM `users` WHERE `id` = '$id' LIMIT 1");
   if (mysql_num_rows($req)) $user = mysql_fetch_assoc($req);
}
if(empty($user)){
   $req = mysql_query("SELECT * FROM `users` WHERE `id` = '".$u['id']."' LIMIT 1");
   $user = mysql_fetch_assoc($req);
}
   
include 'head.php';
echo '<div class="title"><img src= "image/rating.png"> Поднять рейтинг</div>';

if($u['admin'] == 1) echo '<div class="subtitle"><a href = "?act=history">История</a></div>';

echo '<div class="main_menu">';
   
//Если нажал
if (isset($_POST['submit'])) {
   if(isset($_POST['rating']) && $_POST['rating']>0) {
      $rating = num($_POST['rating']);
	  $money = $rating*5;
      if($money <= $u['money']){
	     $user['rating'] = $user['rating']+$rating;
         mysql_query("UPDATE `users` SET `rating` = '".$user['rating']."' WHERE `id` = '".$user['id']."'");
		 mysql_query("UPDATE `users` SET `money` = '".($u['money']-$money)."' WHERE `id` = '".$u['id']."'");
		 //Записываем в историю
		 mysql_query("INSERT INTO `rating_history` SET
		 `user` = '".$u['id']."',
		 `outuser` = '".$user['id']."',
		 `time` = '".time()."',
		 `rating` = '".$rating."',
		 `money` = '".$money."'
		 ");
		 
		 //Если не себе, пишем в журнал
		 if($u['id'] != $user['id']){
		    if($u['sex'] == 'm'){
               $action[0] = 'Повысил';
            } else {
               $action[0] = 'Повысила';
            }
            $text = $action[0].' Ваш рейтинг на <b>'.$rating.'</b>.';
            mysql_query("INSERT INTO `jurnal` SET
            `user` = '".$user['id']."',
            `outuser` = '".$u['id']."',
            `text` = '$text',
            `time` = '".time()."',
            `new` = '1'
            ");
		 }
         show_ok('Рейтинг успешно повышен!');
      } else {
         show_error('У Вас недостаточно монет!');
      }
   } else {
      show_error('Введите количество рейтинга!');
   }
}

//Вывод
echo '<table><tr>';
echo '<td valign=top>';
   ava($user['id'],50);
echo '</td>';
echo '<td valign=top>';
   echo ''.ico($user['sex'],$user['admin']).' <a href = "page.php?id='.$user['id'].'">'.$user['login'].'</a> '.online($user['online']).'';
   echo '<br/>';
    echo '<img src = "image/rating.png"> '.$user['rating'].'';
echo '</td>';
echo '</tr></table>';
echo '<form action="?id='.$user['id'].'" method="post">';
echo 'Количество: <br/>';
echo '<input type="text" name="rating" value = "'.(intval($u['money']/5)).'" maxlength="5" size = "7"><br/>';
echo '<input type="submit" name ="submit" value="Поднять" class="submit"></form>';
   
echo '<div class="main_block">Стоимость 1 <img src = "image/rating.png"> = 5 <img src = "image/balanse.png"></div>';
echo '</div>';
include 'foot.php';

break;

}

?>