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

// Get the posted data
$userId = $_POST['user_id'];
$inflowIndex = $_POST['inflow_index'];
$amount = $_POST['amount'];
$date = $_POST['date'];

// Path to your JSON file
$filePath = '../data.json';

// Read the JSON file
$jsonData = file_get_contents($filePath);
$data = json_decode($jsonData, true);

// Check if user exists
if (!isset($data[$userId])) {
    echo json_encode(['success' => false, 'message' => 'User not found']);
    exit;
}

// Check if inflow index exists
if (!isset($data[$userId]['user_inflows'][$inflowIndex])) {
    echo json_encode(['success' => false, 'message' => 'Inflow record not found']);
    exit;
}

// Update the inflow record
$data[$userId]['user_inflows'][$inflowIndex]['amount'] = $amount;
$data[$userId]['user_inflows'][$inflowIndex]['date'] = $date;

// Save the updated data back to the file
if (file_put_contents($filePath, json_encode($data, JSON_PRETTY_PRINT))) {
    echo json_encode(['success' => true, 'message' => 'Inflow updated successfully']);
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to save data']);
}
?>