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

session_start();

// DATABASE CONNECTION
$conn = mysqli_connect("localhost", "root", "", "dubai_treasury_bank");

if (!$conn) {
    die("Database connection failed: " . mysqli_connect_error());
}

// ADMIN LOGIN PROCESS
if (isset($_POST['adminLogin'])) {

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

    $query = "SELECT * FROM admin WHERE username = '$username' LIMIT 1";
    $result = mysqli_query($conn, $query);

    if (!$result) {
        die("Query failed: " . mysqli_error($conn));
    }

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

        $admin = mysqli_fetch_assoc($result);

        // DEBUG: check fetched admin
        // echo '<pre>'; print_r($admin); exit;

        if (password_verify($password, $admin['password'])) {

            $_SESSION['admin_id']   = $admin['id'];
            $_SESSION['admin_name'] = $admin['full_name'];
            $_SESSION['admin_role'] = $admin['role'];

            header("Location: admin-dashboard.php");
            exit;
        } else {
            $error = "Password is incorrect";
        }

// echo "<pre>";
// echo "Entered Password:\n";
// var_dump($password);

// echo "\nStored Hash From DB:\n";
// var_dump($admin['password']);

// echo "\nPassword Verify Result:\n";
// var_dump(password_verify($password, $admin['password']));
// echo "</pre>";
// exit;





    } else {
        $error = "Admin username not found";
    }
}
?>
