<?php
require("../config/koneksi.php");
require("../config/function.php");
require("../config/apk.php");

$username = mysqli_escape_string($koneksi, $_POST['username']);
$password = mysqli_escape_string($koneksi, $_POST['password']);
$query = mysqli_query($koneksi, "SELECT * FROM users WHERE username='$username'");
$cek = mysqli_num_rows($query);
$user = mysqli_fetch_array($query);
if ($cek <> 0) {
    if ($user['level'] == 'admin') {
        if (!password_verify($password, $user['password'])) {
            echo "nopass";
        } else {
            $_SESSION['id_user'] = $user['id_user'];
            $_SESSION['level'] = 'admin';
            echo "ok";
			mysqli_query($koneksi, "INSERT INTO log_guru (id_user,type,text,date) VALUES ('$user[id_user]','login','masuk','$tanggal $waktu')");
        }
    } elseif ($user['level'] == 'guru') {

        if ($password == $user['password']) {
            $_SESSION['id_user'] = $user['id_user'];
            $_SESSION['level'] = 'guru';
            echo "ok";
		mysqli_query($koneksi, "INSERT INTO log_guru (id_user,type,text,date) VALUES ('$user[id_user]','login','masuk','$tanggal $waktu')");
		} else {
            echo "nopass";
        }
		} elseif ($user['level'] == 'kepala') {

        if ($password == $user['password']) {
            $_SESSION['id_user'] = $user['id_user'];
            $_SESSION['level'] = 'kepala';
            echo "ok";
        } else {
            echo "nopass";
        }
		} elseif ($user['level'] == 'staff') {

        if ($password == $user['password']) {
            $_SESSION['id_user'] = $user['id_user'];
            $_SESSION['level'] = 'staff';
            echo "ok";
			mysqli_query($koneksi, "INSERT INTO log_guru (id_user,type,text,date) VALUES ('$user[id_user]','login','masuk','$tanggal $waktu')");
        } else {
            echo "nopass";
        }
    } elseif ($user['level'] == 'pengawas') {
        if (!password_verify($password, $user['password'])) {
            echo "nopass";
        } else {
            $_SESSION['id_user'] = $user['id_user'];
            $_SESSION['level'] = 'pengawas';
            echo "ok";
        }
    }
} elseif ($cek == 0) {
    echo "td";
}