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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $fullname = '%' . trim($_POST['fullname']) . '%';
    $state = '%' . trim($_POST['state']) . '%';

    $stmt = $conn->prepare("SELECT userid, fullname, state FROM usermaster WHERE fullname LIKE ? AND state LIKE ? AND Userstatus = 'active'");
    $stmt->bind_param("ss", $fullname, $state);
    $stmt->execute();
    $result = $stmt->get_result();

    $users = [];

    while ($row = $result->fetch_assoc()) {
        $users[] = [
            'userid' => $row['userid'],
            'fullname' => $row['fullname'],
            'state' => $row['state']
        ];
    }

    if (count($users) > 0) {
        echo json_encode(['success' => true, 'data' => $users]);
    } else {
        echo json_encode(['success' => false, 'message' => 'No users found.']);
    }

    $stmt->close();
    $conn->close();
}
?>
