<?php
	if (!empty($directory[1])){
		$cant_per_page = 25;
		if (!empty($directory[2]) && is_numeric($directory[2])){
			$actual = GetConexion()->real_escape_string($directory[2]-1) * $cant_per_page;
			$page = GetConexion()->real_escape_string($directory[2]);
		}
		else{
			$actual = 0;
			$page = 1;
		}
		if (empty($_POST["pname"])){
			$pname = "";
		}
		else{
			$pname = GetConexion()->real_escape_string($_POST["pname"]);
		}
		$ranking = GetConexion()->real_escape_string($directory[1]);
		$limit = (($page-1)*$cant_per_page);
		if ($directory[1] == "players"){
			$query = "SELECT player.id,player.playtime,player.job,player.skill_group,player.name,player.level,player.gold,player.exp,player_index.empire,guild.name AS guild_name,guild.id as guild_id 
				FROM ".DB_PLAYER.".player 
				LEFT JOIN ".DB_PLAYER.".player_index 
				ON player_index.id=player.account_id 
				LEFT JOIN ".DB_PLAYER.".guild_member 
				ON guild_member.pid=player.id 
				LEFT JOIN ".DB_PLAYER.".guild 
				ON guild.id=guild_member.guild_id
				INNER JOIN ".DB_ACCOUNT.".account 
				ON account.id=player.account_id
				WHERE player.name NOT LIKE '[%]%' AND player.name like '%".$pname."%' AND account.status !='BLOCK'
				ORDER BY player.level DESC, player.exp DESC, player.playtime ASC 
				LIMIT ".$limit.",".$cant_per_page.";";
			$tot = mysqli_fetch_assoc(mysqli_query(GetConexion(), "SELECT COUNT(*) as counts
				FROM ".DB_PLAYER.".player 
				LEFT JOIN ".DB_PLAYER.".player_index 
				ON player_index.id=player.account_id 
				LEFT JOIN ".DB_PLAYER.".guild_member 
				ON guild_member.pid=player.id 
				LEFT JOIN ".DB_PLAYER.".guild 
				ON guild.id=guild_member.guild_id
				INNER JOIN ".DB_ACCOUNT.".account 
				ON account.id=player.account_id
				WHERE player.name NOT LIKE '[%]%' AND account.status!='BLOCK'
				ORDER BY player.level DESC, player.exp DESC, player.playtime ASC ;"))["counts"];
		}
		else{
			$query = "SELECT id,name,master,level,exp,win,draw,loss,ladder_point
				FROM ".DB_PLAYER.".guild
				ORDER BY level DESC, exp DESC, win DESC, ladder_point DESC LIMIT ".$limit.",".$cant_per_page.";";
			$tot = mysqli_fetch_assoc(mysqli_query(GetConexion(), "SELECT COUNT(*) as counts 
				FROM ".DB_PLAYER.".guild
				ORDER BY level DESC, exp DESC, win DESC, ladder_point DESC;"))["counts"];
		}
	?>
	<div class="news" style="margin-top:0px">
	<br><br>
		<div class="article">
			<h2><?php echo $lang["ranking1"]; ?></h2>
			<?php
				if ($directory[1] == "players" || $directory[1] == "guilds"){
					?>
					<form action="ranking/<?php echo $directory[1]; ?>" method="POST">
						<center>
							<input type="text" name="pname" class="form_input" placeholder="Player or Guild name"> <input type="submit" id="login_submit" name="login_submit" value="Search" class="button"></span>
						</center>
					</form>
					<?php
				}
				if ($directory[1] == "players"){
					?>
					<table style="width:100%">
						<thead>
							<tr>
								<th align="center">#</th>
								<th align="center"><?php echo $lang["ranking6"]; ?></th>
								<th align="center"><?php echo $lang["ranking7"]; ?></th>
								<th align="center"><?php echo $lang["ranking8"]; ?></th>
								<th align="center"><?php echo $lang["ranking9"]; ?></th>
								<th align="center"><?php echo $lang["ranking10"]; ?></th>
							</tr>
						</thead>
						<tbody>
							<?php
							$conex = mysqli_query(GetConexion(),$query);
							$pos = $actual;
							while($obj = mysqli_fetch_object($conex)){
								?>
								<tr>
									<td align="center"><?php echo $pos+1 ?></td>
									<td align="center"><a href="more_info/<?php echo $directory[1]; ?>/<?php echo $obj->name ?>"><?php echo $obj->name ?></a></td>
									<td align="center"><img style="width:27px;height:17px;" src="style/imgs/reinos/<?php echo $obj->empire ?>.png"/></td>
									<td align="center"><?php echo $obj->level ?></td>
									<td align="center"><?php echo $raza[$obj->job]; ?></td>
									<td align="center"><a href="more_info/guilds/<?php echo $obj->guild_name ?>"><?php echo $obj->guild_name ?></a></td>
								</tr>
								<?php
								$pos++;
							}
							?>
						</tbody>
					</table>
				<?php
				}
				else if ($directory[1] == "guilds"){
					?>
					<table style="width:100%">
						<thead>
							<tr>
								<th align="center">#</th>
								<th align="center"><?php echo $lang["ranking6"]; ?></th>
								<th align="center"><?php echo $lang["ranking8"]; ?></th>
								<th align="center"><?php echo $lang["ranking11"]; ?></th>
								<th align="center"><?php echo $lang["ranking12"]; ?></th>
								<th align="center"><?php echo $lang["ranking13"]; ?></th>
								<th align="center"><?php echo $lang["ranking14"]; ?></th>
								<th align="center"><?php echo $lang["ranking15"]; ?></th>
							</tr>
						</thead>
						<tbody>
							<?php
							$conex = mysqli_query(GetConexion(),$query);
							$pos = $actual;
							while($obj = mysqli_fetch_object($conex)){
								?>
								<tr>
									<td align="center"><?php echo $pos+1 ?></td>
									<td align="center"><a href="more_info/<?php echo $directory[1]; ?>/<?php echo $obj->name ?>"><?php echo $obj->name ?></a></td>
									<td align="center"><?php echo $obj->level ?></td>
									<?php $owner = GetInfoFromPlayer($obj->master,"name"); ?>
									<td align="center"><a href="more_info/players/<?php echo $owner; ?>"><?php echo $owner; ?></a></td>
									<td align="center"><?php echo number_format($obj->ladder_point) ?></td>
									<td align="center"><?php echo number_format($obj->win) ?></td>
									<td align="center"><?php echo number_format($obj->draw) ?></td>
									<td align="center"><?php echo number_format($obj->loss) ?></td>
								</tr>
								<?php
								$pos++;
							}
							?>
						</tbody>
					</table>
				<?php
				}
			?>
			<br/>
			<center>
				<?php echo Pagination($tot,$cant_per_page,$page,URL_HOST."ranking/".$directory[1]."/%d"); ?>
			</center>
		</div>
	</div>
	<?php
	}else{
	?>
	<div class="news" style="margin-top:0px">
	<br><br>
		<div class="article">
			<h2><?php echo $lang["ranking1"]; ?></h2>
			<center>
				<a href="ranking/players/"><input type="submit"  class="button" value="<?php echo $lang["ranking2"]; ?>"><a href="ranking/guilds/"><input type="submit" class="button" value="<?php echo $lang["ranking3"]; ?>">
			</center>
		</div>
	</div>
	<?php
	}
?>