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

// Get the posted data
$userId = $_POST['user_id'];
$taxIndex = $_POST['tax_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 tax index exists
if (!isset($data[$userId]['user_tax_payments'][$taxIndex])) {
    echo json_encode(['success' => false, 'message' => 'Tax record not found']);
    exit;
}

// Update the tax record
$data[$userId]['user_tax_payments'][$taxIndex]['amount'] = $amount;
$data[$userId]['user_tax_payments'][$taxIndex]['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' => 'Tax updated successfully']);
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to save data']);
}
?>