<?php
require_once('conf/dbc.php');
require_once('conf/session_start.php');

if ((!isset($_SESSION['id'])) or (!isset($_SESSION['nick'])))  {
  ?>
<!--1390114136-->
  <script type="text/javascript">
  document.location.href = "login.php";
  </script>
  <?php
  exit();
}

$page_title = 'В чате';
require_once('conf/head.php');
if ((isset($_SESSION['id'])) and (isset($_SESSION['nick'])))  {
$user_id = abs(intval($_SESSION['id']));
  /////////////////////////////////
  ////////////////////////////////////Записали локацию
  $query = "Select  location from users where id = '$user_id'  limit 1";
  $result = mysqli_query($dbc, $query) or die ('Ошибка передачи запроса к БД6');
  $row = mysqli_fetch_array($result);
  $location = $row['location'];
  if ($location == 'chat') {
    }
else {
$query_loc = "update users set location = 'chat' where id = '$user_id' limit 1";
$result_loc = mysqli_query($dbc, $query_loc) or die ('Ошибка передачи запроса к БД');
}
require_once('conf/top.php');
}

?>
    <div id="main">
   <?php
$query_num = "Select id from users where last_active > NOW()-(300*5) and location = 'chat' and gruppa <> 'mytants' and gruppa <>'bandits' and gruppa <> 'zombie' and gruppa <> 'monolits'" ;
	$result_num = mysqli_query($dbc, $query_num) or die ('Ошибка передачи запроса к БД');
	$total = mysqli_num_rows($result_num); 
	?>
 <p class="link">В чате <b class="bonus"><?php echo "$total"; ?> </b> сталкера</p>
	<?php
   /////////////////////////////////
   ////////////////////////////////
if (!empty($_GET['page'])) {
$cur_page = abs(intval($_GET['page']));
}
else {
  $cur_page = 1;
}
    $result_per_page = 10;
	$skip = (($cur_page - 1) * $result_per_page);
		$num_page = ceil($total / $result_per_page);
	if ($num_page > 0) {
$query_us = "Select nick ,id,location,admin,lvl,gruppa,last_active from users where location = 'chat' and last_active > NOW()-(300*5) and gruppa <> 'mytants' and gruppa <>'bandits' and gruppa <> 'zombie' and gruppa <> 'monolits' order by opit DESC, yron_p DESC ,tochn_p DESC, bronya DESC limit $skip, $result_per_page";
      $result_us = mysqli_query($dbc, $query_us) or die ('Ошибка передачи запроса к БД');
	  while ($row_us = mysqli_fetch_array($result_us)) {
           $location = $row_us['location'];
           $lvl = $row_us['lvl'];
	    $nick = $row_us['nick'];
	    $id_us = $row_us['id'];
            $admin = $row_us['admin'];
           $last_active = $row_us['last_active'];
		$gruppa = $row_us['gruppa'];
	    ?>
	    <p class="zx">
<?php if ($admin == '-1' or $admin == '4' or $admin == '5') { echo '<span class="golub"><small>(vip)</small></span> ';}?>

		<?php
        if ($gruppa == 'svoboda') {?><img src="img/ico/svobodaon.png" width="12" height="12" alt="s"/><?php }
	    if ($gruppa == 'dolg') {?><img src="img/ico/dolgon.png" width="12" height="12" alt="d"/><?php }
		if ($gruppa == 'naemniki') {?><img src="img/ico/odinochkion.png" width="12" height="12" alt="od."/><?php }
	  ?><a style="text-decoration:none;"  href="chat.php?otv=<?php echo "$id_us"; ?>"><?php echo "$nick";?></a> [ <a href="profile.php?id=<?php echo "$id_us"; ?>"><img src="img/ico/profile.png" width="15" height="15"></a> ] <img src="img/ico/lvl.gif" width="15" height="15" alt="ур."/><?php echo "$lvl";?> [<?php echo "$last_active";?>]

	  
	    <?php
	  }
	  ?>
	  <div class="zx">
	  <?php
$phpself= filter($_SERVER['PHP_SELF']);
	  ///////////////////////////////////////
	  ////////////////////////////////////////
	  if ($cur_page > 1) {
	    echo '<a href="' . "$phpself" . '?page=1"><<</a> ';
      }
	  else {
	    echo '<< ';
	  }
	  if ($cur_page > 1) {
	    echo '<a href="' . "$phpself" . '?page=' . ($cur_page-1) . '"><</a> ';
      }
	  else {
	    echo '<';
	  }
	/////
	  if (($cur_page-3)>0) {
	 $k = ($cur_page-3);
	    ?><a href="<?php echo "$phpself" . '?page=' . ($cur_page-3)?>"><?php echo "$k";?></a><?php
      }
	 if (($cur_page-2)>0) {
	 $k = ($cur_page-2);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page-2)?>"><?php echo "$k";?></a> <?php
      }
     if (($cur_page-1)>0) {
	 $k = ($cur_page-1);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page-1)?>"><?php echo "$k";?></a> <?php
      }
	?> <span class="white"><?php echo " $cur_page ";?></span><?php
	 if (($cur_page+1)<=$num_page) {
	 $k = ($cur_page+1);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page+1)?>"><?php echo "$k";?></a> <?php
      }
	  	 if (($cur_page+2)<=$num_page) {
	 $k = ($cur_page+2);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page+2)?>"><?php echo "$k";?></a> <?php
      }
	 if (($cur_page+3)<=$num_page) {
	 $k = ($cur_page+3);
	    ?> <a href="<?php echo "$phpself" . '?page=' . ($cur_page+3)?>"><?php echo "$k";?></a> <?php
      }
	/////
	if ($cur_page < $num_page) {
	  echo '<a href="' . "$phpself" . '?page=' . ($cur_page+1) . '">></a> ';
    }
	else {
	  echo '>';
	}
	if ($cur_page < $num_page) {
	  echo ' <a href="' . "$phpself" .  '?page=' . $num_page . '">>></a> ';
    }
	else {
	  echo ' >>';
	}

	////////////////////////////////
	///////////////////////////////
	}
	?>

<a href="/chat.php" class="menu"><img src="img/ico/chat.png" width="12" height="12"/>Вернуться в чат</a>
</div>
	<?php
   /////////////////////////////////
   //////////////////////////////////
require_once('conf/navig.php');
require_once('conf/foot.php');
mysqli_close($dbc);

?>
</body>
</html>