<?php
// Include database connection
require_once('connectionSC.php');

// Cek koneksi database
if ($conn->connect_error) {
    die(json_encode([
        "status" => "error",
        "message" => "Error in connecting to Database: " . $conn->connect_error
    ]));
}

$query = "SELECT username,akses_level,cabang FROM admin WHERE akses_level in ('super_user','spv','kaop','kacab');
";

$result = $conn->query($query);

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

if ($result && $result->num_rows > 0) {
    $data = [];
    // Mengambil data untuk ranking
    while ($row = $result->fetch_assoc()) {
        $username = $row['username'];
        $akses_level = $row['akses_level'];
        $cabang = $row['cabang'];
        // Menambahkan data ranking ke dalam array
        $data[] = [
            "username" => $username,
            "akses_level" => $akses_level,
            "cabang" => $cabang,
 
        ];

    }

    // Output data sebagai JSON
    echo json_encode([
        "status" => "success",
        "userdata" => $data
    ]);
} else {
    echo json_encode([
        "status" => "empty",
        "message" => "No data found for the current month in branch $branch."
    ]);
}

// Menutup koneksi
$conn->close();
?>
