 <?php
ob_start();
header("Content-Type: application/json");

// DB CONNECTION
$conn = mysqli_connect("localhost", "aksoftwarecoimba_yugam", "9994613878@@", "aksoftwarecoimba_yugam");

if (!$conn) {
    echo json_encode([
        "status" => "error",
        "message" => "Database connection failed"
    ]);
    exit;
}

mysqli_set_charset($conn, "utf8mb4");

// GET PARAM
$agent = isset($_GET['agent']) ? trim($_GET['agent']) : "";

if ($agent == "") {
    echo json_encode([
        "status" => "error",
        "message" => "Agent required"
    ]);
    exit;
}

// QUERY
$stmt = mysqli_prepare($conn, "SELECT * FROM master WHERE ag_name = ? LIMIT 100");

if (!$stmt) {
    echo json_encode(["status" => "error", "message" => "Query error"]);
    exit;
}

mysqli_stmt_bind_param($stmt, "s", $agent);
mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);

$data = [];

while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// RESPONSE
echo json_encode([
    "status" => count($data) ? "success" : "empty",
    "count" => count($data),
    "data" => $data
]);

mysqli_close($conn);