<?php
error_reporting(E_ALL ^ E_NOTICE);
include("loginCheck.php");
if (isset($_POST['addRemark'])) {
    $memberId = $_POST['member_id'];
    $id = $_POST['id'];
    $mainStatus = $_POST['mainStatus'];
    $fromDate = $_POST['fromDate'];
    $toDate = $_POST['toDate'];
    $userId = $_POST['userId'];
    $withdrawStatus = $_POST['withdrawStatus'];
    $remarks = $_POST['remarks'];
    $d = date('Y-m-d H:i:s');

    $queryUpdate = mysqli_query($con, "UPDATE meddolic_user_wallet_withdrawal_crypto SET remarks='$remarks',payment_date='$d',released='$withdrawStatus' WHERE id='$id'");

    if ($withdrawStatus == 3) {
        $queryAmount = mysqli_query($con, "SELECT amount FROM meddolic_user_wallet_withdrawal_crypto WHERE id='$id'");
        $valAmount = mysqli_fetch_assoc($queryAmount);
        $amount = $valAmount['amount'];

        mysqli_query($con, "UPDATE meddolic_user_details SET wallet=wallet+'$amount' WHERE member_id='$memberId'");
        mysqli_query($con, "INSERT INTO meddolic_user_wallet_statement (`member_id`,`wallet_statement_id`,`deb_cr`,`amount`,`date_time`,`trn_id`) VALUES ('$memberId',11,1,'$amount','$d','$id')");
    } elseif($withdrawStatus == 4){      
        $queryAmount = mysqli_query($con, "SELECT netAmount,paymentId FROM meddolic_user_wallet_withdrawal_crypto WHERE id='$id'");
		$valAmount = mysqli_fetch_assoc($queryAmount);
		$paymentId = $valAmount['paymentId'];
		$amount = floatval($valAmount['netAmount']);
		if ($amount <= 0) {
			die("Invalid amount: $amount");
		}

		$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) {
			die("Invalid wallet address: $WalletAddress");
		}

		$curl = curl_init();
		curl_setopt_array($curl, array(
			CURLOPT_URL => 'https://gateway.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);
// 		 print_r($data);
		$withdrawal = $data['data']['withdrawals'][0];
		$Trnxid = $withdrawal['id'];
		$status = $withdrawal['status'];
		
		mysqli_query($con, "UPDATE  meddolic_user_wallet_withdrawal_crypto SET trnxId='$Trnxid',status='$status' WHERE id='$id'");
        }
    
    if ($queryUpdate) { ?>
        <script>
            alert("Action Taken Successfully!!!");
            window.top.location.href = 'walletWithdrawStatus?user_id=<?= $userId ?>&withdrawStatus=<?= $withdrawStatus ?>&from_date=<?= $fromDate ?>&to_date=<?= $toDate ?>';
        </script>
    <?php } else { ?>
        <script>
            alert("Action Taken Successfully!!!");
            window.top.location.href = 'walletWithdrawStatus?user_id=<?= $userId ?>&withdrawStatus=<?= $withdrawStatus ?>&from_date=<?= $fromDate ?>&to_date=<?= $toDate ?>';
        </script>
<?php }
} ?>
<?php include("../close-connection.php"); ?>