<?php
session_start();
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
$koneksi = new mysqli("localhost","bpjw9637_inventori","inventori123","bpjw9637_inventori");

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login Sistem</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: url(img/gedungkcmjk.jpg) no-repeat fixed;
            -webkit-background-size: 100% 100%;
            -moz-background-size: 100% 100%;
            -o-background-size: 100% 100%;
            background-size: 100% 100%;
        }
        .row {
            margin: 100px auto;
            width: 300px;
            text-align: center;
        }
        .login {
            background-color: #FFFFFF;
            padding: 20px;
            margin-top: 20px;
        }
    </style>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<div class="container">
    <div class="row">
        <div class="center">
            <div class="login">
                <form role="form" action="" method="post">
                    <h2> Log In</h2>
                    <br>
                    <div class="form-group">
                        <input type="text" name="username" class="form-control" value="" placeholder="Username" required autofocus />
                    </div>
                    <div class="form-group">
                        <input type="password" name="password" class="form-control" value="" placeholder="Password" required />
                    </div>
                    <div class="form-group">
                        <select name="level" class="form-control" required>
                            <option value="">Pilih Level User</option>
                            <option value="superadmin">Superadmin</option>
                            <option value="petugas">Pegawai</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <input type="submit" name="login" class="btn btn-primary btn-block" value="Log in" />
                    </div>
                    <br>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<?php
if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $level = $_POST['level'];

    // Pastikan password tidak null sebelum meng-hash
    if (!is_null($password) && $password !== '') {
        $hashedPassword = md5($password);
    } else {
        echo '<center><div class="alert alert-danger">Password tidak boleh kosong</div></center>';
        exit;
    }

    $sql = $koneksi->query("SELECT * FROM users WHERE username='$username' AND password='$hashedPassword'");
    $ketemu = $sql->num_rows;
    $data = $sql->fetch_assoc();

    if ($ketemu >= 1) {
        if ($data['level'] == $level) {
            if ($level == 'superadmin') {
                $_SESSION['superadmin'] = $data['id'];
                header("Location: index3.php");
            } elseif ($level == 'petugas') {
                $_SESSION['petugas'] = $data['id'];
                header("Location: index2.php");
            }
        } else {
            echo '<center><div class="alert alert-danger">Level user tidak sesuai</div></center>';
        }
    } else {
        echo '<center><div class="alert alert-danger">Upss...!!! Login gagal. Silakan Coba Kembali</div></center>';
    }
}
?>
