<?php
session_start();
require("../../mainconfig.php");
$msg_type = "nothing";

if (isset($_SESSION['user'])) {
	header("Location: ".$cfg_baseurl);
} else {
	if (isset($_POST['login'])) {
		$post_username = mysqli_real_escape_string($db, trim($_POST['username']));
		$post_password = mysqli_real_escape_string($db, trim($_POST['password']));
		$ip = $_SERVER['REMOTE_ADDR'];

		if (empty($post_username) || empty($post_password)) {
			$msg_type = "error";
			$msg_content = '<b>Gagal:</b> Mohon mengisi semua input.<script>swal("Error!", "Mohon mengisi semua input.", "error");</script>';
		} else {
			$check_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$post_username'");
			if (mysqli_num_rows($check_user) == 0) {
				$msg_type = "error";
				$msg_content = '<b>Gagal:</b> Username atau password salah.<script>swal("Error!", "Username/Password salah.", "error");</script>';
			} else {
				$data_user = mysqli_fetch_assoc($check_user);
				if ($post_password <> $data_user['password']) {
					$msg_type = "error";
					$msg_content = '<b>Gagal:</b> Username atau password salah.<script>swal("Error!", "Username/Password salah.", "error");</script>';
				} else if ($data_user['status'] == "Suspended") {
					$msg_type = "error";
					$msg_content = '<b>Gagal:</b> Akun nonaktif.<script>swal("Error!", "Akun telah dinonaktifkan.", "error");</script>';
				} else {
					$_SESSION['user'] = $data_user;
					$insert_user = mysqli_query($db, "INSERT INTO catatan (username, note, waktu) VALUES ('$post_username', 'Kamu telah melakukan aktifitas Login dengan Ip $ip', '$date $time')");
					if ($insert_user == TRUE) {
					header("Location: ".$cfg_baseurl);
				}
				}
			}
		}
	}
}

?>
          <!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="SMM Panel Indonesia yang menyediakan Followers, Likes, Views, Subscriber, untuk beragam social media: Instagram, Youtube, Facebook, Twitter dengan harga Termurah." name="description"/>
    <title>Borneo Media - SMM</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="SMM Panel Indonesia, Followers, Likes, Views, Subscriber, untuk beragam social media: Instagram, Youtube, Facebook, Twitter, harga Termurah">
    <meta name="author" content="</CODINGINAN>">
    <link rel="icon" href="<?= $cfg_baseurl ?>assets/images/fav.png" type="image/x-icon">
    <link rel="shortcut icon" href="<?= $cfg_baseurl ?>assets/images/favicon.png" type="image/x-icon">
    <!-- Google font-->
    <link href="https://fonts.googleapis.com/css?family=Rubik:400,400i,500,500i,700,700i&amp;display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i,900&amp;display=swap" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../../assets/css/font-awesome.css">
    <!-- ico-font-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/icofont.css">
    <!-- Themify icon-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/themify.css">
    <!-- Flag icon-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/flag-icon.css">
    <!-- Feather icon-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/feather-icon.css">
    <!-- Plugins css start-->
    <!-- Plugins css Ends-->
    <!-- Bootstrap css-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/vendors/bootstrap.css">
    <!-- App css-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/style.css">
    <link id="color" rel="stylesheet" href="../../assets/css/color-1.css" media="screen">
    <!-- Responsive css-->
    <link rel="stylesheet" type="text/css" href="../../assets/css/responsive.css">
  </head>
  <body>
    <!-- login page start-->
    <div class="container-fluid p-0">
      <div class="row m-0">
        <div class="col-12 p-0">    
          <div class="login-card login-dark">
            <div>
              <div><a class="logo" href="index.html"><img class="img-fluid for-light" src="../../assets/images/logo/BMSMM.png" alt="looginpage"><img class="img-fluid for-dark" src="../../assets/images/logo/BMSMM.png" alt="looginpage"></a></div>
              <div class="login-main"> 
                <?php if ($msg_type == "error") {
											?>
											<div class="alert alert-secondary dark" role="alert">
                          <p><?php echo $msg_content; ?></p>
                        </div>
											<?php
											}
											?>
                <form class="theme-form" method="POST">
                  <h4>Silahkan Login</h4>
                  <p>Masukkan username & password</p>
                  <div class="form-group">
                    <label class="col-form-label">username</label>
                    <input class="form-control" type="text" name="username" required placeholder="Your Username">
                  </div>
                  <div class="form-group">
                    <label class="col-form-label">Password</label>
                    <div class="form-input position-relative">
                      <input class="form-control" type="password" name="password" required placeholder="*********" id="myInput">
                      <div onclick="myFunction()" class="show-hide"><span class="show">                         </span></div>
                    </div>
                  </div>
                  <div class="form-group mb-0">
                    <!--<div class="checkbox p-0">-->
                    <!--  <input id="checkbox1" type="checkbox">-->
                    <!--  <label class="text-muted" for="checkbox1">Remember password</label>-->
                    <!--</div><a class="link" href="forget-password.html">Forgot password?</a>-->
                    <div class="text-end mt-3">
                      <button class="btn btn-primary btn-block w-100" type="submit" name="login">Login</button>
                    </div>
                  </div>
                  <p class="mt-4 mb-0 text-center">Belum memiliki akun?<a class="ms-2" href="register">Daftar Sekarang</a></p>
                  <p class="mt-4 mb-0 text-center">Lupa password?<a class="ms-2" href="forgot">Reset Password</a></p>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- latest jquery-->
      <script src="../../assets/js/jquery.min.js"></script>
      <!-- Bootstrap js-->
      <script src="../../assets/js/bootstrap/bootstrap.bundle.min.js"></script>
      <!-- feather icon js-->
      <script src="../../assets/js/icons/feather-icon/feather.min.js"></script>
      <script src="../../assets/js/icons/feather-icon/feather-icon.js"></script>
      <!-- scrollbar js-->
      <!-- Sidebar jquery-->
      <script src="../../assets/js/config.js"></script>
      <!-- Plugins JS start-->
      <!-- Plugins JS Ends-->
      <!-- Theme js-->
      <script src="../../assets/js/script.js"></script>
      <!-- Plugin used-->
      <script>
    function myFunction() {
      var x = document.getElementById("myInput");
      if (x.type === "password") {
        x.type = "text";
      } else {
        x.type = "password";
      }
    }
    </script>
    </div>
  </body>
</html>