<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);

include_once "config/koneksi.php";
include_once "config/library.php";

// Proses login
if (isset($_POST['login'])) {
    $conn = dblink();

    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    // Untuk admin: password di-hash SHA512
    $pass_md5  = md5($password);
    $pass_sha  = hash("sha512", $pass_md5);

    // Query admin
    $admin = mysqli_query($conn, "SELECT * FROM rb_users WHERE username='".mysqli_real_escape_string($conn,$username)."' AND password='".$pass_sha."'");

    // Query guru
    $guru = mysqli_query($conn, "SELECT * FROM rb_guru WHERE nip='".mysqli_real_escape_string($conn,$username)."' AND password='".mysqli_real_escape_string($conn,$password)."'");

    // Query siswa
    $siswa = mysqli_query($conn, "SELECT * FROM rb_siswa WHERE nisn='".mysqli_real_escape_string($conn,$username)."' AND password='".mysqli_real_escape_string($conn,$password)."'");

    if (mysqli_num_rows($admin) >= 1) {
        $r = mysqli_fetch_assoc($admin);
        $_SESSION['id'] = $r['id_user'];
        $_SESSION['level'] = $r['level'];
        $_SESSION['nama_lengkap'] = $r['nama_lengkap'];
        header("Location: index.php");
        exit;
    } elseif (mysqli_num_rows($guru) >= 1) {
        $r = mysqli_fetch_assoc($guru);
        $_SESSION['id'] = $r['nip'];
        $_SESSION['level'] = 'guru';
        $_SESSION['nama_lengkap'] = $r['nama_guru'];
        header("Location: index.php");
        exit;
    } elseif (mysqli_num_rows($siswa) >= 1) {
        $r = mysqli_fetch_assoc($siswa);
        $_SESSION['id'] = $r['nisn'];
        $_SESSION['level'] = 'siswa';
        $_SESSION['nama_lengkap'] = $r['nama'];
        header("Location: index.php");
        exit;
    } else {
        $error = "Username atau password salah!";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Login SISFO Akademik</title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <link rel="stylesheet" href="plugins/iCheck/square/blue.css">
</head>
<body class="hold-transition login-page">
<div class="login-box">
    <div class="login-logo">
        <b>SISFO</b> Akademik
    </div>
    <div class="login-box-body">
        <p class="login-box-msg">Silahkan login</p>

        <?php if (!empty($error)) { echo "<div class='alert alert-danger'>$error</div>"; } ?>

        <form action="" method="post">
            <div class="form-group has-feedback">
                <input type="text" name="username" class="form-control" placeholder="Username" required>
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password" name="password" class="form-control" placeholder="Password" required>
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="row">
                <div class="col-xs-8">
                    <div class="checkbox icheck">
                        <label>
                            <input type="checkbox"> Remember Me
                        </label>
                    </div>
                </div>
                <div class="col-xs-4">
                    <button type="submit" name="login" class="btn btn-primary btn-block btn-flat">Sign In</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="plugins/iCheck/icheck.min.js"></script>
<script>
$(function () {
    $('input').iCheck({
        checkboxClass: 'icheckbox_square-blue',
        radioClass: 'iradio_square-blue',
        increaseArea: '20%'
    });
});
</script>
</body>
</html>
