<?php
  session_start();
  include ('config/conn.php');
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="fonts-login/icomoon/style.css">
    <link rel="stylesheet" href="css-login/owl.carousel.min.css">
    <link rel="stylesheet" href="css-login/bootstrap.min.css">
    <link rel="stylesheet" href="css-login/style.css">
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <link rel="icon" type="image/png" href="images-login/logo-intens-baru.png"/><title>Inventaris INTENS</title>
  </head>
  <body>
  <div class="content">
    <div class="container">
      <div class="row">
        <div class="col-md-6">
          <img src="images-login/undraw_data_processing_yrrv.svg" alt="Image" class="img-fluid"> 
        </div>
        <div class="col-md-6 contents">
          <div class="row justify-content-center">
            <div class="col-md-8">
              <div class="mb-4">
              <h4 style="margin-top: 25px; text-align: center;">Masuk Ke Halaman Utama!</h4>
              <p class="mb-4" style="text-align: center;">Aplikasi Inventaris Barang PT. Inti Konten Indonesia.</p>
            </div>

            <form method="post">
              <div class="form-group first">
                <label for="username">Nama Pengguna</label>
                <input type="text" class="form-control" name ="username" id="username" minlength="6" maxlength="15" required
                oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)" >
              </div>

              <div class="form-group last mb-4">
                <label for="password">Kata Sandi</label>
                <input type="password" class="form-control" name="password" id="password" minlength="6" maxlength="15" required 
                oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)" >
              </div>
              
              <div class="d-flex mb-5 align-items-center">
                <label class="control control--checkbox mb-0"><span class="caption">Tampilkan Kata Sandi</span>
                  <input type="checkbox" onclick="myFunction()"/>
                  
                  <div class="control__indicator"></div>
                </label>
                <span class="ml-auto"><a href="#" class="forgot-pass">Lupa Kata Sandi?</a></span> 
              </div>

              <input type="submit" name="submit" value="Masuk" class="btn btn-block btn-primary">              
            </form>

            <script type="text/javascript">
                function myAlertError() {
                    swal({
                    title: "Gagal!",
                    text: "Nama Pengguna atau Kata Sandi Salah!",
                    icon: "error",
                    button: true
                    });
                }
            </script>

            <?php
              if(isset($_POST['submit'])){
                $username = $_POST['username'];
                $password = $_POST['password'];
                $qry = mysqli_query($con, "SELECT * FROM users WHERE nama_pengguna = '$username' AND kata_sandi = md5('$password')");
                $cek = mysqli_num_rows($qry);
                if($cek == 1){
                  $_SESSION['userweb']=$username;
                  header ("location:dashboard.php");
                  exit;
                } else {
                  echo '<script type="text/javascript">',
                        'myAlertError();',
                        '</script>';
                }
              }
            ?>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script>
    function InvalidMsg(textbox) {
        if (textbox.value === '') {
            textbox.setCustomValidity
                  ('Tidak Boleh Kosong!');
        } else if (textbox.validity.tooShort) {
            textbox.setCustomValidity
                  ('Isi Data 6 Karakter atau Lebih!');
        } else {
            textbox.setCustomValidity('');
        }
        return true;
    }
  </script>
  
  <script>
      function myFunction() {
      var x = document.getElementById("password");
      if (x.type === "password") {
        x.type = "text";
      } else {
        x.type = "password";
      }
    }
    </script>
    	
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
        crossorigin="anonymous"></script>  
    <script src="js-login/jquery-3.3.1.min.js"></script>
    <script src="js-login/popper.min.js"></script>
    <script src="js-login/bootstrap.min.js"></script>
    <script src="js-login/main.js"></script>
  </body>
</html>
