<?php
	error_reporting(E_ALL);
	date_default_timezone_set('Europe/Madrid'); 
	require("config/config.php");
	session_name(SESSION);
	session_start();
	require("config/functions.php");
	require("config/session.php");
	include("config/url.php");
	include("config/languages/lang.".GetLang().".php");
?>

<html lang="<?php echo GetLang(); ?>">
	<head>
		<title><?php echo WEB_TITLE; ?></title>
		<base href="<?php echo URL_HOST; ?>">
		<link rel="stylesheet" type="text/css" href="style/main.css">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script src="style/main.js"></script>
		<script type="text/javascript" src="style/jquery.noty.packaged.min.js"></script>
		<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
		<script src='https://www.google.com/recaptcha/api.js'></script>
		
		<div class="container">
			<div id="customContainer"></div>
		</div>
		<script>
			function SendNotification(text="error",type="danger",icon="check"){
				var pretext = '<div class="activity-item"><div class="activity"><i class="fa fa-'+icon+'"</i> '+text+'</div></div>';
				if (type == "danger")
					type = "error";
				var n = noty({
					text        : pretext,
					type        : type,
					dismissQueue: true,
					progressBar : true,
					timeout     : 3000,
					layout      : 'topLeft',
					maxVisible  : 10,
				});
			}
			function ChangeTop(asd){
				var name = document.getElementById("top10_name").value;
				if (asd == 0){
					if (name == "Players"){
						return;
					}
					document.getElementById("top10_name").innerHTML = "Players";
				}
				else{
					if (name == "Guilds"){
						return;
					}
					document.getElementById("top10_name").innerHTML = "Guilds";
				}
				$.post("scripts/top10.php", {type:asd}, function(data){
					$("#ranking_top10").html(data);
				});
			}

			function ChangeLanguage(lang){
				$.post("scripts/language.php", {type:lang}, function(data){
					$("#customContainer").html(data);
				});
			}
		</script>
	</head>

	<body>
		<div class="wrapper">
			<nav>
				<ul>
					<a href="home/"><li class="w-100"><?php echo $lang["header1"]; ?></li></a>
					<?php
					if (empty($_SESSION["user_id"]))
						echo '<a href="register/"><li class="w-100">'.$lang["header2"].'</li></a>';
					else
						echo '<a href="user/"><li class="w-100">'.$lang["header9"].'</li></a>';
					?>
					<a href="board/"><li class="w-90"><?php echo $lang["header3"]; ?></li></a>
					<a href="staff/"><li class="w-90"><?php echo $lang["header4"]; ?></li></a>
					<a href="support/"><li class="w-100"><?php echo $lang["header5"]; ?></li></a>
					<a href="ranking/"><li class="w-130"><?php echo $lang["header6"]; ?></li></a>
					<a href="javascript:void(0)"><li class="w-100"><?php echo $lang["header7"]; ?></li></a>
				</ul>
				<br>
				<?php
				if (empty($_SESSION["user_id"]))
					echo '<button id="loginButton">'.$lang["header8"].'</button>';
				else
					echo '<a href="logout/"><button id="exitButton">'.$lang["header10"].'</button></a>';
				?>
			</nav>

			<a class="logo "href="#logo"></a>
			
			<div class="clearfix"></div>
			
			<section class="left-bar">
				<a href="downloads/"><img class="download-img" src="style/imgs/download-button.png" alt="Download Exodial"></a>
				
				<div class="clearfix"></div>
				
				<div class="channels">
					<ul>
						<li><img src="style/imgs/channel-<?php echo GetChannelStatus(PORTCH1); ?>.png" alt="channel online"></li>
						<li><img src="style/imgs/channel-<?php echo GetChannelStatus(PORTCH2); ?>.png" alt="channel online"></li>
						<li class="mt-1"><img src="style/imgs/channel-<?php echo GetChannelStatus(PORTCH3); ?>.png" alt="channel online"></li>
						<li><img src="style/imgs/channel-<?php echo GetChannelStatus(PORTCH4); ?>.png" alt="channel online"></li>
					</ul>
				</div>
				
				<div class="clearfix"></div>
				
				<div class="ranking" style="margin-left:0px">
					<div class="ranking-top">
						<ul>
						<li><a href="javascript:void(0)" onclick="ChangeTop(0)"><img src="style/imgs/left-arrow.png"></a></li>
						<li><h6 id="top10_name"><?php echo $lang["ranking3"]; ?></h6></li>
						<li><a href="javascript:void(0)" onclick="ChangeTop(1)"><img src="style/imgs/right-arrow.png"></a></li>
						</ul>
					</div>
						<div id="ranking_top10">
						<script>
						window.onload = function() {
							ChangeTop(0);
						};
						</script>
					</div>
				</div>

				<div class="clearfix"></div>
				
				<div class="player-statics">
					<div class="channel-bar" style="background-image:url(./style/imgs/statics/bar2.png);"></div>
					<div class="channel-bar" style="background-image:url(./style/imgs/statics/bar3.png);"></div>
					<div class="channel-bar" style="background-image:url(./style/imgs/statics/bar5.png);"></div>
					<div class="channel-bar" style="background-image:url(./style/imgs/statics/bar8.png);"></div>
				</div>
			</section>
			
			<section class="main-container">
				<?php
				if ($url['args'] != "user" && $url['args'] != "downloads" && $url['args'] != "settings" && $url['args'] != "ranking"){
				?>
				<div class="slider">
					<img id="slide-image" src="style/imgs/slide1.png">
					<img class="slider-frame" src="style/imgs/slider-frame.png">
					<ul>
						<li><img id="slide1" class="slide-button" src="style/imgs/slide-on.png"></li>
						<li><img id="slide2" class="slide-button" src="style/imgs/slide-off.png"></li>
						<li><img id="slide3" class="slide-button" src="style/imgs/slide-off.png"></li>
						<li><img id="slide4" class="slide-button" src="style/imgs/slide-off.png"></li>
					<ul>
				</div>
				<?php
				}
				?>
				<?php
					$includeDir = ".".DIRECTORY_SEPARATOR."scripts".DIRECTORY_SEPARATOR;
					$includeDefault = $includeDir."home.php";
					if (!isset($url['dir']) || ($url['dir'] == "home") || ($url['dir'] == "index.php")  || ($url['dir'] == "")){
						include($includeDefault);
					}
					else{
						if (!file_exists($includeDir.$url['args'].".php")){
							include($includeDefault);
						}
						else{
							include($includeDir.$url['args'].".php");
						}
					}
				?>
			</section>
			
			<section class="right-bar">
				<div class="vote4us">
					<a href="vote/">Voting for Exodial gives u coins to use in shop</a>
				</div>
				
				<div class="server-stats">
					<table>
						<tr>
							<th><?php echo$lang["sv_stats1"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetPlayersOnline()); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats2"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetPlayersOnline(1)); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats3"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetTotalAccounts()); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats4"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetTotalPlayers()); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats5"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetInfoByRace(0, 4)); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats6"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetInfoByRace(1, 5)); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats7"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetInfoByRace(2, 6)); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats8"]; ?></th>
							<th class="stats-number"><?php echo number_format(GetInfoByRace(3, 7)); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats9"]; ?></th>
							<th class="stats-number small-text"><?php echo number_format(GetItemCount()); ?></th>
						</tr>
						<tr>
							<th><?php echo$lang["sv_stats10"]; ?></th>
							<th class="stats-number small-text"><?php echo number_format(GetYangCount()); ?></th>
						</tr>
					</table>		
				</div>
				
				<div class="social-media">
					<a href="#fb"><img src="style/imgs/facebook.png"></a>
					<a href="discord"><img src="style/imgs/discord.png"></a>
					<a href="yt"><img src="style/imgs/youtube.png"></a>
				</div>
			</section>
		</div>

		<div id="loginModal" class="modal">
		<!-- Modal content -->
			<div class="modal-content">
				<div class="modal-body">
					<form action="home/" method="post">
						<input type="text" id="username" name="username" placeholder="<?php echo $lang["register6"]; ?>">
						<br>
						<input type="password" id="password" name="password" placeholder="<?php echo $lang["register7"]; ?>">
						<input type="submit" value="<?php echo $lang["header11"]; ?>">
					</form>
				</div>
			</div>
		</div>
		<?php
		if (!empty($_SESSION["user_id"])){
			?>
			<aside class="social_elements">
				<ul>
					<li><a href="javascript:void(0)" onclick="ChangeLanguage(1)" class="l_pt" title="Portugal"></a></li>
					<li><a href="javascript:void(0)" onclick="ChangeLanguage(2)" class="l_ro" title="Romanian"></a></li>
					<li><a href="javascript:void(0)" onclick="ChangeLanguage(3)" class="l_en" title="English"></a></li>
					<li><a href="javascript:void(0)" onclick="ChangeLanguage(4)" class="l_es" title="Español"></a></li>
				</ul>
			</aside>
			<?php
		}
		?>
	</body>
	<?php
		if (!empty($_POST["username"]) && !empty($_POST["password"])){
			$username = GetConexion()->real_escape_string($_POST["username"]);
			$password = GetConexion()->real_escape_string($_POST["password"]);
			$query = mysqli_query(GetConexion(), "select id, login, coins, email, ".LANGUAGE_FIELD." from ".DB_ACCOUNT.".account where login = '".$username."' and password = PASSWORD('".$password."') limit 1;");
			if($query->num_rows>0){
				$obj = mysqli_fetch_object($query);
				$_SESSION["user_id"] = $obj->id;
				$_SESSION["user_name"] = $obj->login;
				$_SESSION["user_coins"] = $obj->coins;
				$_SESSION["user_vcoins"] = $obj->drs;
				$_SESSION["user_email"] = $obj->email;
				$_SESSION["user_lang"] = $obj->idioma;
				echo '<script>SendNotification("'.$lang["header12"].'", "success");</script>';
				echo '<meta http-equiv="Refresh" content="2;url=user/" />';
			}
			else{
				echo '<script>SendNotification("'.$lang["header13"].'", "error", "exclamation-triangle");</script>';
			}
		}
	?>
</html>