<?php

header('Content-Type: application/json'); // Menetapkan header JSON

if (isset($_POST['username']) && isset($_POST['password'])) {

    require_once "connectionSC.php";

    $username = $_POST['username'];
    $password = md5($_POST['password']); // Hash password

    // Gunakan prepared statement untuk keamanan
    $stmt = $conn->prepare("SELECT * FROM admin WHERE username = ?");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        if ($password === $row['password']) {
            $response = array(
                "akses_level" => $row['akses_level'],
                "nama" => $row['nama'],
                "cabang" => $row['cabang'],
                "status" => "sukses"
            );
        } else {
            $response = array("status" => "failure", "message" => "Password salah");
        }
    } else {
        $response = array("status" => "failure", "message" => "Username tidak ditemukan");
    }

    echo json_encode($response);
    $stmt->close();
} else {
    echo json_encode(array("status" => "failure", "message" => "Parameter tidak lengkap"));
}

$conn->close();
?>
