<?php
include("../config.php");
header("Content-Type: application/json");

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


$user_id = $_SESSION['user_id']; // Get user ID from session

// Fetch user details based on session user_id
$sql = "SELECT id, name, email, phone, role FROM users WHERE client_id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("i", $user_id); // Bind parameter
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $users = [];

    while ($row = $result->fetch_assoc()) {
        $users[] = [
            "id" => $row["id"],
            "name" => $row["name"],
            "email" => $row["email"],
            "phone" => $row["phone"],
            "role" => $row["role"]
        ];
    }

    echo json_encode(["status" => "success", "users" => $users]);
} else {
    echo json_encode(["status" => "error", "message" => "No users found"]);
}

$stmt->close();
$db->close();
?>
