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

// Cek apakah form dikirim
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = $_POST['password'];

    // Ambil data user berdasarkan username
    $sql = "SELECT * FROM users
            WHERE username='$username'
            AND status='Aktif'";

    $query = mysqli_query($conn, $sql);

    if (!$query) {
        die("Query Error : " . mysqli_error($conn));
    }

    if (mysqli_num_rows($query) == 1) {

        $user = mysqli_fetch_assoc($query);

        // Verifikasi password
        if (password_verify($password, $user['password'])) {

            $_SESSION['login'] = true;
            $_SESSION['id'] = $user['id'];
            $_SESSION['nama'] = $user['nama'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['level'] = $user['level'];

           header("Location: modules/dashboard/index.php");
exit;

            exit;

        } else {

            echo "<script>
            alert('Password salah!');
            window.location='login.php';
            </script>";

        }

    } else {

        echo "<script>
        alert('Username tidak ditemukan!');
        window.location='login.php';
        </script>";

    }

} else {

    header("Location: login.php");
    exit;

}
?>