<?php
include 'includes/config.php';

header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);

$id = $data['id'] ?? null;
$source = $data['source'] ?? null;
$type = $data['type'] ?? null;
$amount = $data['amount'] ?? null;

if ($id && $source && $type && $amount) {
    try {
        $column = ($type === 'debit') ? 'amount' : 'amount'; // Both debit and credit use the 'amount' column
        $table = ($source === 'bank_debit_amount') ? 'bank_debit_amount' : 'supplier_purchase_manual';

        $stmt = $conn->prepare("UPDATE $table SET $column = :amount WHERE id = :id");
        $stmt->execute(['amount' => $amount, 'id' => $id]);

        echo json_encode(['success' => true]);
    } catch (Exception $e) {
        echo json_encode(['success' => false, 'message' => $e->getMessage()]);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid request data.']);
}
?>
