<?php
session_start();
include "../koneksi.php";

$username = $_POST["username"];
$password = $_POST["password"];
$akses = $_POST["akses"];

// echo $username;
// echo $password;
// echo $akses;
if (
    empty($username) || empty($password)
) {
    header("Location: ../login.php?error=kosong");
    exit;
}

$sql = mysqli_query($konek, "SELECT * FROM pegawai WHERE username='$username'");
$data = mysqli_fetch_assoc($sql);

//CEK USERNAME
if (!$data) {
    header("Location: ../login.php?error=user");
    exit;
}

//cek pass
if ($password !== $data["passe"]) {
    header("Location: ../login.php?error=pass");
    exit;
}

if ($akses == '') {
    header("Location: ../login.php?error=pilih");
    exit;
}

if ($data["akses"] == "Semua") {
    $_SESSION['login'] = true;
    $_SESSION['nama_pegawai'] = $data['nama_pegawai'];
    $_SESSION['username'] = $data['username'];
    $_SESSION['handle'] = $data['handle'];
    $_SESSION['akses'] = $akses;
    if ($akses == "Raxi") {
        header("Location: ../dashboard.php");
    } elseif ($akses == "Bisman") {
        header("Location: ../dashboard-bisman.php");
    }
} else {
    if ($akses == $data["akses"]) {
        // echo $akses;

        $_SESSION['login'] = true;
        $_SESSION['nama_pegawai'] = $data['nama_pegawai'];
        $_SESSION['username'] = $data['username'];
        $_SESSION['akses'] = $data['akses'];
        $_SESSION['handle'] = $data['handle'];

        if ($akses == "Raxi") {
            header("Location: ../dashboard.php");
        } elseif ($akses == "Bisman") {
            header("Location: ../dashboard-bisman.php");
        }
    } else {
        header("Location: ../login.php?error=akses");
        exit;
    }
}
