<?php

if (isset($_GET['phoneNumber'])) {
    require_once "connection.php";

    $phoneNumber = $_GET['phoneNumber'];
    $firstChar = substr($phoneNumber, 0, 1);

    // Tambahkan '0' di depan jika karakter pertama bukan '0'
    if ($firstChar == '0') {
        $phoneReal = $phoneNumber;
    } else {
        $phoneReal = '0' . $phoneNumber;
    }

    $sql = "SELECT * FROM nasabah WHERE phoneNumber='$phoneReal'";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            // Kirim data user jika ditemukan
            $response = array(
                "status" => "success",
                "data" => array(
                    "fullName" => $row["fullName"],
                    "phoneNumber" => $row["phoneNumber"],
                    "email" => $row["email"],
                    "address" => $row["address"]
                )
            );
            echo json_encode($response);
            exit;
        }
    } else {
        // Jika nomor telepon tidak ditemukan
        echo json_encode(array("status" => "failure", "message" => "Phone number not found"));
        exit;
    }
}
?>
