<?php
include("login-check.php");
header('Content-Type: application/json');

$paymentDate = date('Y-m-d H:i:s');

// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Decode JSON input
    $data = json_decode(file_get_contents('php://input'), true);

    // Validate required fields
    if (isset($data['ids'], $data['releaseType']) && is_array($data['ids']) && count($data['ids']) > 0) {
        $ids = array_map('intval', $data['ids']);
        $releaseType = intval($data['releaseType']);
        $idList = implode(',', $ids);

        if ($releaseType === 1) {
            // Offline release logic
            $remarks = "Offline Release";
            $query = "UPDATE meddolic_user_wallet_withdrawal_crypto SET released = 1, remarks = '$remarks', payment_date = '$paymentDate' WHERE id IN ($idList)";
            if (mysqli_query($con, $query)) {
                echo json_encode(["success" => true]);
            } else {
                echo json_encode(["success" => false, "error" => mysqli_error($con)]);
            }

        } elseif ($releaseType === 4) {
            // Online release logic
            $remarks = "Online Release";

            foreach ($ids as $id) {
                $queryAmount = mysqli_query($con, "SELECT netAmount, paymentId,member_id FROM meddolic_user_wallet_withdrawal_crypto WHERE id='$id'");
                $valAmount = mysqli_fetch_assoc($queryAmount);

                $paymentId = $valAmount['paymentId'];
                 $member_id = $valAmount['member_id'];
                $amount = floatval($valAmount['netAmount']);

                if ($amount <= 0)
                    continue;

    		$queryAddress = mysqli_query($con, "SELECT walletAddress FROM meddolic_user_details WHERE member_id='$memberId' AND topupFlag=1");
    		$valAddress = mysqli_fetch_assoc($queryAddress);
    		$WalletAddress = $valAddress['walletAddress'];

                if (empty($WalletAddress) || strlen($WalletAddress) !== 42 || strpos($WalletAddress, '0x') !== 0)
                    continue;
                    
                $curl = curl_init();
                curl_setopt_array($curl, array(
                    CURLOPT_URL => 'https://faradpay.com/api/payout',
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => '',
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => 'POST',
                    CURLOPT_POSTFIELDS => '{
            "withdrawals": [
                {
                    "address": "' . $WalletAddress . '",
                    "currency": "USDTBSC",
                    "amount": ' . $amount . ',
                    "ipnCallbackUrl": "https://hopexglobal.com/withdrowalReturnFaradpayApi"
                }
            ]
        }',
                    CURLOPT_HTTPHEADER => array(
                       	'x-api-key: 98A7DE9D-1B5B-4244-B307-094FCF018B20',
                        'Content-Type: application/json'
                    ),
                ));

                $response = curl_exec($curl);
                curl_close($curl);

                $data = json_decode($response, true);
                $withdrawal = $data['data']['withdrawals'][0] ?? null;

                if ($withdrawal) {
                    $Trnxid = $withdrawal['id'];
                    $status = $withdrawal['status'];
                    mysqli_query($con, "UPDATE meddolic_user_wallet_withdrawal_crypto SET trnxId='$Trnxid', status='$status' WHERE id='$id'");
                }
            }


            echo json_encode(["success" => true, "message" => "Online release completed."]);

        } else {
            echo json_encode(["success" => false, "error" => "Unknown release type."]);
        }

    } else {
        echo json_encode(["success" => false, "error" => "Invalid or missing input data."]);
    }
} else {
    echo json_encode(["success" => false, "error" => "Invalid request method."]);
}
