<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
	<title>Login | GoPOS</title>
	<!-- Favicon-->
	<link rel="icon" href="<?php echo getConfig($connect, 'base_url');?>assets/images/icon.png" type="image/x-icon">
	<!-- Google Fonts -->
	<link href="<?php echo getConfig($connect, 'base_url');?>assets/css/style.css.php" rel="stylesheet" type="text/css">
	<link href="<?php echo getConfig($connect, 'base_url');?>assets/css/themes/all-themes.min.css" rel="stylesheet" type="text/css">
	<!-- Jquery Core Js -->
	<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/jquery/jquery.min.js"></script>
	<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/pace/pace.min.js"></script>
</head>

<body class="login-page theme-blue-grey">
	<?php
	if ( isset( $_POST[ 'submit' ] ) ) {
		if ( $_POST[ 'submit' ] == 'sign-in' ) {
			if ($_POST[ 'captcha_login' ] == $_SESSION[ "captcha" ]) {
				$email = $_POST[ 'email' ];
				$password = Password( $_POST[ 'password' ] );
				$login = mysqli_query($connect, "select a.* from users a where a.user_email='$email'" )or die( mysqli_error($connect) );
				$j = mysqli_num_rows( $login );
				if ($j) {
					$bl = mysqli_fetch_array( $login );
					if ( $bl[ 'password' ] == $password ) {
						if($bl['user_status']=='1'){
							$_SESSION[ 'id_user' ] = $bl[ 'ID' ];
							$_SESSION[ 'nama_user' ] = $bl[ 'user_nama' ];
							$_SESSION[ 'email_user' ] = $bl[ 'user_email' ];
							$_SESSION[ 'email_user' ] = $bl[ 'user_email' ];
							$_SESSION[ 'foto' ] = $bl[ 'user_foto' ];
							$_SESSION[ 'level' ] ='user';
							notif( '<b>Login Success!</b> Welcome <i>' . $bl[ 'user_nama' ] . '</i><br>You will be redirected to the Dashboard page in <span class="tm" style="font-weight:bold">3</span> sec...', 'success bg-blue-grey' );
							echo   '<div class="pace">
								  		<div class="pace-activity"></div>
									</div>
									<script>
										$(document).ready(function(e) {
											var i=3;
											setInterval(function(){
												i=i-1;
												if(i>=0){
													$(".tm").text(i);
												}
												if(i<2){
													document.location.reload();
												}
											},1000);
										 });
									  </script>';
							exit();
						}else{
							notif( '<b>Login Failed!</b> Sorry, your account has been disabled.<br>Please contact the administrator for more information.', 'danger' );
						}
					} else {
						notif( '<b>Login Failed!</b> Your password is wrong', 'danger' );
					}
				} else {
					$admin = mysqli_query($connect, "select * from admin where email='$email'" )or die( mysqli_error($connect) );
					$ja = mysqli_num_rows($admin);
					if ($ja) {
						$bla = mysqli_fetch_array( $admin );
						if ( $bla[ 'password' ] == $password ) {
							if($bla['status']=='1'){
								$_SESSION[ 'id_user' ] = $bla[ 'ID' ];
								$_SESSION[ 'nama_user' ] = $bla[ 'nama' ];
								$_SESSION[ 'email_user' ] = $bla[ 'email' ];
								$_SESSION[ 'foto' ] = $bl[ 'user_foto' ];
								$_SESSION[ 'level' ] ='admin';
								notif( '<b>Login Success!</b> Welcome <i>' . $bla[ 'nama' ] . '</i><br>You will be redirected to the Dashboard page in <span class="tm" style="font-weight:bold">3</span> sec...', 'success bg-blue-grey' );
								echo   '<div class="pace">
											<div class="pace-activity"></div>
										</div>
										<script>
											$(document).ready(function(e) {
												var i=3;
												setInterval(function(){
													i=i-1;
													if(i>=0){
														$(".tm").text(i);
													}
													if(i<2){
														document.location.reload();
													}
												},1000);
											 });
										  </script>';
								exit();
							}else{
								notif( '<b>Login Failed!</b> Sorry, your account has been disabled.<br>Please contact the administrator for more information.', 'danger' );
							}
						} else {
							notif( '<b>Login Failed!</b> Your password is wrong', 'danger' );
						}
					} else {
						notif( '<b>Login Failed!</b> Your email is unknown', 'danger' );
					}
				}
			} else {
				notif( '<b>Login Failed!</b> Incorrect Captcha code!', 'danger' );
			}
		} elseif ( $_POST[ 'submit' ] == 'sign-up' ) {
			if ( $_POST[ 'captcha_signup' ] == $_SESSION[ "captcha" ] ) {
				$email = $_POST[ 'email' ];
				$cek = mysqli_num_rows( mysqli_query($connect, "select ID from users where user_email='$email'" ) );
				if ( !$cek ) {
					$password = Password( $_POST[ 'password' ] );
					$now = date( 'Y-m-d H:i:s' );
					$q = mysqli_query($connect, "INSERT INTO `users` (`password`, `user_nama`, `user_email`, `user_hp`, `user_tgl`, `user_status`)
											 VALUES ('$password', '$_POST[nama]', '$email', '$_POST[no_hp]', '$now', '1');" )or die( mysqli_error($connect) );
					$bl = mysqli_fetch_array( mysqli_query($connect, "select * from users where user_email='$email' and password='$password'" ) );
					$_SESSION[ 'id_user' ] = $bl[ 'ID' ];
					$_SESSION[ 'nama_user' ] = $bl[ 'user_nama' ];
					$_SESSION[ 'email_user' ] = $bl[ 'user_email' ];
					$_SESSION[ 'email_user' ] = $bl[ 'user_email' ];
					$_SESSION[ 'foto' ] = $bl[ 'user_foto' ];
					notif( '<b>Login Success!</b> Welcome <i>' . $bl[ 'user_nama' ] . '</i><br>You will be redirected to the Dashboard page in <span class="tm" style="font-weight:bold">3</span> sec...', 'success bg-blue-grey' );
					echo '<div class="pace">
						  <div class="pace-activity"></div>
						</div>
						<script>
							$(document).ready(function(e) {
								var i=3;
								setInterval(function(){
									i=i-1;
									if(i>=0){
										$(".tm").text(i);
									}
									if(i<2){
										document.location.reload();
									}
								},1000);
							});
						</script>';
					exit();
				} else {
					notif( '<b>Sign up Failed!</b> Email address already registered!', 'danger' );
				}
			} else {
				notif( '<b>Sign up Failed!</b> Incorrect Captcha code!', 'danger' );
			}
			echo '<script>
				$(function(){
					setTimeout(function(){
						$(".bsignup").click();
						},700);
			   });</script>';
		}
	}
	?>
	<div class="login-box" style="display:none;">
		<div class="logo col-blue-grey" style="margin-bottom:0px !important;">
			<a href="javascript:void(0);" class="col-blue-grey ll">
      <img src="assets/images/logo_transparent.png" width="200px;">
    </a>
		
			<small style="color:#000000 !important">Backend Management</small>
		</div>
		<div class="card" id="flogin">
			<div class="body">
				<form id="sign_in" method="POST">
					<input type="hidden" name="token" value="<?php echo getToken();?>">
					<div class="msg">Sign in to start your session</div>
					<div class="input-group">
						<span class="input-group-addon">
          <i class="material-icons">mail</i>
          </span>
					

						<div class="form-line">
							<input type="email" tabindex="1" value="<?php echo $_POST['email'];?>" class="form-control login-email" name="email" placeholder="Email" required autofocus>
						</div>
					</div>
					<div class="input-group">
						<span class="input-group-addon">
          <i class="material-icons">lock</i>
          </span>
					

						<div class="form-line">
							<input type="password" minlength="6" tabindex="2" class="form-control" name="password" placeholder="Password" required>
						</div>
					</div>
					<div class="form-group text-center">
						<img alt="kode captcha" class="img-responsive img-captcha" style="float:left;">
						<button class="btn btn-xs waves-effect bg-blue-grey" onClick="reloadCaptcha();" type="button" style="float:right; padding:0px!important;margin-top:-60px !important;">
							<div class="material-icons">refresh</div>
						</button>
					</div>
					<div class="form-group">
						<div class="form-line">
							<input type="text" tabindex="3" maxlength="4" data-minlength="4" minlength="4" class="form-control kode_captcha" autocomplete="off" name="captcha_login" placeholder="Ketik kode Captcha" required>
						</div>
					</div>
					<div class="row">
						<div class="col-xs-12">
							<button tabindex="4" class="btn btn-lg btn-block bg-blue-grey waves-effect" name="submit" value="sign-in" type="submit">Sign in</button>
						</div>
					</div>
					<div class="row m-t-15">
						<div class="col-md-6">
							<a href="#" tabindex="6" class="bforgot">
              Forgot Password?
            </a>
						

						</div>
						<div class="col-md-6">
							<a href="#" tabindex="6" class="bsignup">
              Create Account?
            </a>
						

						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="card" id="fforgot" style="display:none">
			<div class="body">
				<form id="forgot_password" method="POST">
					<div class="msg">Enter your email address that you used to register. We'll send you link to reset your password.</div>
					<div class="input-group">
						<span class="input-group-addon">
          <i class="material-icons">email</i>
          </span>
					

						<div class="form-line">
							<input type="email" class="form-control" value="<?php echo $_POST['email'];?>" name="email" placeholder="Email" required autofocus>
						</div>
					</div>
					<button class="btn btn-block btn-lg bg-blue-grey waves-effect" name="submit" value="forgot" type="submit">Reset Password</button>
					<div class="row m-t-20 m-b--5 align-center">
						<a href="#" class="blogin">
            Login
          </a>
					

					</div>
				</form>
			</div>
		</div>
		<div class="card" id="fsignup" style="display:none">
			<div class="body">
				<form id="signup_form" method="POST">
					<input type="hidden" name="token" value="<?php echo getToken();?>">
					<div class="msg">Register a new account</div>
					<div class="input-group">
						<span class="input-group-addon">
				<i class="material-icons">person</i>
			</span>
					

						<div class="form-line">
							<input type="text" class="form-control" name="nama" placeholder="Full Name" required autofocus value="<?php echo $_POST['nama'];?>">
						</div>
					</div>
					<div class="input-group">
						<span class="input-group-addon">
				<i class="material-icons">email</i>
			</span>
					

						<div class="form-line">
							<input type="email" class="form-control" name="email" placeholder="Email Address" required value="<?php echo $_POST['email'];?>">
						</div>
					</div>
					<div class="input-group">
						<span class="input-group-addon">
				<i class="material-icons">phone</i>
			</span>
					

						<div class="form-line">
							<input type="tel" class="form-control" name="no_hp" placeholder="Phone Number" required value="<?php echo $_POST['no_hp'];?>">
						</div>
					</div>
					<div class="input-group">
						<span class="input-group-addon">
				<i class="material-icons">lock</i>
			</span>
					

						<div class="form-line">
							<input type="password" class="form-control" id="password" name="password" minlength="6" placeholder="Password" required>
						</div>
					</div>
					<div class="input-group">
						<span class="input-group-addon">
				<i class="material-icons">lock</i>
			</span>
					

						<div class="form-line">
							<input type="password" class="form-control" equalTo="#password" name="confirm" minlength="6" placeholder="Confirm Password" required>
						</div>
					</div>
					<div class="form-group text-center">
						<img alt="kode captcha" class="img-responsive img-captcha" style="float:left;">
						<button class="btn btn-xs waves-effect bg-blue-grey" onClick="reloadCaptcha();" type="button" style="float:right; padding:0px!important;margin-top:-60px !important;">
							<div class="material-icons">refresh</div>
						</button>
					</div>
					<div class="form-group">
						<div class="form-line">
							<input type="text" maxlength="4" data-minlength="4" minlength="4" class="form-control kode_captcha" autocomplete="off" name="captcha_signup" placeholder="Ketik kode Captcha" required>
						</div>
					</div>
					<div class="form-group">
						<input type="checkbox" name="terms" id="terms" class="filled-in chk-col-blue-grey" required>
						<label for="terms">I read and agree to the <a href="javascript:void(0);">terms of usage</a>.</label>
					</div>
					<button class="btn btn-block btn-lg bg-blue-grey waves-effect" name="submit" value="sign-up" type="submit">Create Account</button>
					<div class="row m-t-20 m-b--5 align-center">
						<a href="#" class="blogin">Login</a>
					</div>
				</form>
			</div>
		</div>
	</div>
	<p align="center" style="font-size:12px;">&copy; 2018-
		<?php echo date('Y');?> <a href="https://hmstudio.id">TIM Development</a> All rights reserved.<br>
		<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/bootstrap/js/bootstrap.min.js"></script>
		<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/node-waves/waves.min.js"></script>
		<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/jquery-validation/jquery.validate.min.js"></script>
		<script src="<?php echo getConfig($connect, 'base_url');?>assets/js/admin.js"></script>
		<script src="<?php echo getConfig($connect, 'base_url');?>assets/js/login.js"></script>
	</p>
</body>
</html>