<?php ob_start();session_start();error_reporting(0);?>
<?php include "../config.php"; ?>
<?php include "../assets/var.php"; ?>
<?php include "../assets/function.php"; ?>
<!DOCTYPE html>

<!-- =========================================================
* Sneat - Bootstrap 5 HTML Admin Template - Pro | v1.0.0
==============================================================

* Product Page: https://themeselection.com/products/sneat-bootstrap-html-admin-template/
* Created by: ThemeSelection
* License: You must have a valid license purchased in order to legally use the theme for your project.
* Copyright ThemeSelection (https://themeselection.com)

=========================================================
 -->
<!-- beautify ignore:start -->
<html
  lang="en"
  class="light-style customizer-hide"
  dir="ltr"
  data-theme="theme-default"
  data-assets-path="assets/"
  data-template="vertical-menu-template-free"
>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
    />

    <title>Login</title>

    <meta name="description" content="" />

    <!-- Favicon -->
    <link rel="icon" type="image/x-icon" href="<?php echo $base_url; ?>/img/logo.png" />

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap"
      rel="stylesheet"
    />

    <!-- Icons. Uncomment required icon fonts -->
    <link rel="stylesheet" href="assets/vendor/fonts/boxicons.css" />

    <!-- Core CSS -->
    <link rel="stylesheet" href="assets/vendor/css/core.css" class="template-customizer-core-css" />
    <link rel="stylesheet" href="assets/vendor/css/theme-default.css" class="template-customizer-theme-css" />
    <link rel="stylesheet" href="assets/css/demo.css" />

    <!-- Vendors CSS -->
    <link rel="stylesheet" href="assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.css" />

    <!-- Page CSS -->
    <!-- Page -->
    <link rel="stylesheet" href="assets/vendor/css/pages/page-auth.css" />
    <!-- Helpers -->
    <script src="assets/vendor/js/helpers.js"></script>

    <!--! Template customizer & Theme config files MUST be included after core stylesheets and helpers.js in the <head> section -->
    <!--? Config:  Mandatory theme config file contain global vars & default theme options, Set your preferred theme option in this file.  -->
    <script src="assets/js/config.js"></script>
  </head>

  <body>
  
    <?php
$errorMessage = '';
if(isset($_POST["login"]))
{
	if($_POST["loginId"]!=''&& $_POST["loginPass"]!='') {
		
	$loginId = $_POST['loginId'];
	$password = $_POST['loginPass'];
	$sqlQuery = $pdo->query("SELECT * FROM users WHERE username='".$loginId."' AND password='".md5($password)."'");
	$isValidLogin = $sqlQuery->rowCount();	
	if($isValidLogin > 0){
		if(!empty($_POST["remember"])) {
			setcookie ("loginId", $loginId, time()+ (10 * 365 * 24 * 60 * 60));  
			setcookie ("loginPass",	$password,	time()+ (10 * 365 * 24 * 60 * 60));
		} else {
			setcookie ("loginId",""); 
			setcookie ("loginPass","");
		}
		$userDetails = $sqlQuery->fetch(PDO::FETCH_ASSOC);
		$_SESSION["username"] = $userDetails['username'];
		$_SESSION["id_user"] = $userDetails['id'];
		//$_SESSION["level"] = $userDetails['level'];

		header("location:".BASE_URL."/dashboard"); 		
	} else {		
		$errorMessage = "Invalid login!";		 
	}
} 
else
{
	$errorMessage = "Masukkan username dan password!";	
}
}	
?>
    <!-- Content -->

    <div class="container-xxl">
      <div class="authentication-wrapper authentication-basic container-p-y">
        <div class="authentication-inner">
          <!-- Register -->
          <div class="card">
            <div class="card-body">
              <!-- Logo -->
              <div class="text-center mb-3 justify-content-center" href="https://edelweissspa.id">
                  
                  
                  <!--<img src="/img/logo.png" width="70" class="rounded"><br>-->
                  <span class="app-brand-text demo text-body fw-bolder">Login</span>
                
              </div>
              <!-- /Logo -->
			  <?php if ($errorMessage != '') { ?>
			  <div id="login-alert" class="alert alert-danger alert-dismissible" role="alert">
                <?php echo $errorMessage; ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                      </div>
				                         
			  <?php } ?>
              <!--<h4 class="mb-2 text-center">Sistem Operasional Edelweiss</h4>-->
              <p class="mb-4 text-center">Silakan Masuk Akun Admin Anda</p>

              <form id="formAuthentication" class="" action="" method="POST">
                <div class="mb-3">
                  <label for="email" class="form-label">Username</label>
                  <input
                    type="text"
                    class="form-control"
                    id="loginId" 
					name="loginId"  
					value="<?php if(isset($_COOKIE["loginId"])) { echo $_COOKIE["loginId"]; } ?>"
                    placeholder="Masukkan nama pengguna"
                    autofocus required
                  />
                </div>
                <div class="mb-3 form-password-toggle">
                  <div class="d-flex justify-content-between">
                    <label class="form-label" for="password">Password</label>
                    <!--<a href="auth-forgot-password-basic.html">
                      <small>Lupa Password?</small>
                    </a>-->
                  </div>
                  <div class="input-group input-group-merge">
                    <input
                      type="password"
                      class="form-control"
                      id="loginPass" 
					  name="loginPass" 
					  value="<?php if(isset($_COOKIE["loginPass"])) { echo $_COOKIE["loginPass"]; } ?>"
                      placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;"
                      aria-describedby="password"
                      required
                    />
                    <span class="input-group-text cursor-pointer"><i class="bx bx-hide"></i></span>
                  </div>
                </div>
                <div class="mb-3">
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" id="remember" name="remember" <?php if(isset($_COOKIE["loginId"])) { ?> checked <?php } ?> />
                    <label class="form-check-label" for="remember-me"> Remember Me </label>
                  </div>
                </div>
                <div>
                    
				  <input type="hidden" name="login" value="login">
				  <button type="submit" class="btn btn-primary w-100" name="login" onClick="this.form.submit();$('#spin1').show();">
								<span id="spin" class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="display:none;"></span>
                                <span class="visually-hidden">Loading...</span>
								Sign In
							</button>
                </div>
              </form>
              
            </div>
          </div>
          <!-- /Register -->
          
          
          
        </div>
      </div>
    </div>

    <!-- / Content -->

    <!-- Core JS -->
    <!-- build:js assets/vendor/js/core.js -->
    <script src="assets/vendor/libs/jquery/jquery.js"></script>
    <script src="assets/vendor/libs/popper/popper.js"></script>
    <script src="assets/vendor/js/bootstrap.js"></script>
    <script src="assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.js"></script>

    <script src="assets/vendor/js/menu.js"></script>
    <!-- endbuild -->

    <!-- Vendors JS -->

    <!-- Main JS -->
    <script src="assets/js/main.js"></script>

    <!-- Page JS -->
    
    <script>
	$("form").submit(function(){
	    if($('form > :input[required]:visible').val() != ""){
            $('#spin',this).show();	
        }
	});

	</script>

  </body>
</html>
