<?php
/**
 * Load saved beneficiaries for the logged-in user.
 * Returns JSON array — called via AJAX from transfer forms.
 */
session_start();
include "../../connect/db.php";

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

if (!isset($_SESSION['chap'])) {
    echo json_encode([]);
    exit;
}

$email = mysqli_real_escape_string($con, $_SESSION['chap']);
$result = mysqli_query($con, "SELECT name, account_num, bank, routing FROM beneficiary WHERE user = '$email' ORDER BY id DESC");

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

echo json_encode($beneficiaries);
