<?php
// backend/api/update-withdrawal-status.php
header('Content-Type: application/json');
require_once '../config/db.php';

$data = json_decode(file_get_contents("php://input"), true);
$id = $data['id'] ?? 0;
$status = $data['status'] ?? ''; // 'completed' or 'failed'

if (!$id || !$status) {
    echo json_encode(["status" => "error", "message" => "Invalid Data"]);
    exit;
}

// 1. Withdrawal record check karein
$check = mysqli_query($conn, "SELECT * FROM withdrawals WHERE id = '$id'");
$wData = mysqli_fetch_assoc($check);

if (!$wData) {
    echo json_encode(["status" => "error", "message" => "Record not found"]);
    exit;
}

// 2. Status Update Query
// Note: Agar user dashboard par 'completed' status use kar raha hai toh yahan vahi rakhein
$update = mysqli_query($conn, "UPDATE withdrawals SET status = '$status', processed_at = NOW() WHERE id = '$id'");

if ($update) {
    // Agar status completed hai, toh user ko batane ke liye processed_at update kar diya
    echo json_encode(["status" => "success", "message" => "Status updated to $status"]);
} else {
    echo json_encode(["status" => "error", "message" => "Database update failed"]);
}
?>