<?php
	session_start();
	include('spaceship/krypton.php');
	
	// initializing variables
	$useradm = "";
	$email    = "";
	$errors = array();

	// LOGIN USER
	if (isset($_POST['signin'])) {
		$useradm = mysqli_real_escape_string($krypton, $_POST['username']);
		$password = mysqli_real_escape_string($krypton, $_POST['password']);

		if (empty($useradm)) {
			array_push($errors, "Username should not Empty!!");
		}
		if (empty($password)) {
			array_push($errors, "Password should not Empty!!");
		}

		if (count($errors) == 0) {
			$password = md5($password);
			$ip = getip();
			$query = "SELECT * FROM superman WHERE name='$useradm' AND password='$password'";
			$results = mysqli_query($krypton, $query);
			if (mysqli_num_rows($results) == 1) {
				$fperan = mysqli_fetch_assoc($results);
				$peran = $fperan['peran'];
				$ipuser = $fperan['ip_user'];

				if ($peran == 'admin') {
					$_SESSION['username'] = $useradm;
					$_SESSION['peran'] = $peran;
					echo '<script>
    window.setTimeout(function() {
        window.location = "dashboard.php";
      }, 2000);
    </script>';
				} else if ($ipuser == $ip) {
					$_SESSION['username'] = $useradm;
					$_SESSION['peran'] = $peran;
					echo '<script>
    window.setTimeout(function() {
        window.location = "dashboard.php";
      }, 2000);
    </script>';
				} else {
					array_push($errors, "IP DILARANG!! ");
				}
			} else {
				array_push($errors, "Username or Password is Incorrect");
			}
		}
	}


	if (isset($_SESSION['username'])) {
		echo '<script>
    window.setTimeout(function() {
        window.location = "dashboard.php";
      }, 2000);
    </script>';
	}

	?>


<!DOCTYPE html>
<html lang="en" class="login_page">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>Login To <?php include 'name.php'; ?></title>

	<!-- Bootstrap framework -->
	<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
	<!-- theme color-->
	<link rel="stylesheet" href="css/tamarillo.css" />
	<!-- tooltip -->
	<link rel="stylesheet" href="lib/qtip2/jquery.qtip.min.css" />
	<!-- main styles -->
	<link rel="stylesheet" href="css/style.css" />

	<!-- favicon -->
	<link rel="shortcut icon" href="favicon.ico" />
</head>

<body>
	


	<div class="login_box">
		<?php include('spaceship/errors.php') ?>

		<form method="post" id="login_form">
			<div class="top_b">Sign in to <?php include 'name.php'; ?></div>
			<div class="cnt_b">
				<div class="form-group">
					<div class="input-group">
						<span class="input-group-addon input-sm"><i class="glyphicon glyphicon-user"></i></span>
						<input class="form-control input-sm" type="text" id="username" name="username" placeholder="Username" />
					</div>
				</div>
				<div class="form-group">
					<div class="input-group">
						<span class="input-group-addon input-sm"><i class="glyphicon glyphicon-lock"></i></span>
						<input class="form-control input-sm" type="password" id="password" name="password" placeholder="Password" />
					</div>
				</div>
			</div>
			<div class="btm_b clearfix">
				<a class="btn btn-info btn-sm" target="_blank" href="https://t.me/tiger_devs">Telegram: @TIGER_DEVS</a>
				<button class="btn btn-default btn-sm pull-right" type="submit" name="signin">Sign In</button>
			</div>
		</form>



	</div>




	<script src="js/jquery.min.js"></script>
	<script src="js/jquery.actual.min.js"></script>
	<script src="lib/validation/jquery.validate.js"></script>
	<script src="bootstrap/js/bootstrap.min.js"></script>
	<script>
		$(document).ready(function() {

			//* boxes animation
			form_wrapper = $('.login_box');

			function boxHeight() {
				form_wrapper.animate({
					marginTop: (-(form_wrapper.height() / 2) - 24)
				}, 400);
			};
			form_wrapper.css({
				marginTop: (-(form_wrapper.height() / 2) - 24)
			});
			$('.linkform a,.link_reg a').on('click', function(e) {
				var target = $(this).attr('href'),
					target_height = $(target).actual('height');
				$(form_wrapper).css({
					'height': form_wrapper.height()
				});
				$(form_wrapper.find('form:visible')).fadeOut(400, function() {
					form_wrapper.stop().animate({
						height: target_height,
						marginTop: (-(target_height / 2) - 24)
					}, 500, function() {
						$(target).fadeIn(400);
						$('.links_btm .linkform').toggle();
						$(form_wrapper).css({
							'height': ''
						});
					});
				});
				e.preventDefault();
			});

			//* validation
			$('#login_form').validate({
				onkeyup: false,
				errorClass: 'error',
				validClass: 'valid',
				rules: {
					username: {
						required: true,
						minlength: 3
					},
					password: {
						required: true,
						minlength: 3
					}
				},
				highlight: function(element) {
					$(element).closest('div').addClass("f_error");
					setTimeout(function() {
						boxHeight()
					}, 200)
				},
				unhighlight: function(element) {
					$(element).closest('div').removeClass("f_error");
					setTimeout(function() {
						boxHeight()
					}, 200)
				},
				errorPlacement: function(error, element) {
					$(element).closest('div').append(error);
				}
			});
		});
	</script>


</body>

</html>