<?php
include '../../conection.php';

if (isset($_POST['addEditBulkRemark'])) {
    $ids = $_POST['ids'];
    $remarks = $_POST['remarks'];
    $withdrawStatus = $_POST['withdrawStatus'];
    $fromDate = $_POST['fromDate'];
    $toDate = $_POST['toDate'];
    $userId = $_POST['userId'];
    $d = date('Y-m-d H:i:s');

    foreach ($ids as $id) {
        $queryDetails = mysqli_query($con, "SELECT member_id,distributedFund, amount FROM meddolic_user_wallet_withdrawal_crypto WHERE id='$id'");
        if (!$queryDetails) continue;
        $data = mysqli_fetch_assoc($queryDetails);
        if (!$data) continue;

        $memberId = $data['member_id'];
        $amount = $data['amount'];
        $distributedFund = $data['distributedFund'];

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

        if ($withdrawStatus == 3) {
            mysqli_query($con, "UPDATE meddolic_user_details 
                                SET wallet = wallet + '$amount' , SET tradeWallet = tradeWallet + '$distributedFund' , SET splitWallet = splitWallet + '$distributedFund' , SET retopup_wallet = retopup_wallet + '$distributedFund'
                                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')");
            
            echo "<script>
                 alert('Action Completed Successfully!');
               </script>"; 
        }
        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'");
		mysqli_query($con, "UPDATE meddolic_user_details 
                                SET tradeWallet = tradeWallet + '$distributedFund' , SET splitWallet = splitWallet + '$distributedFund' , SET retopup_wallet = retopup_wallet + '$distributedFund'
                                WHERE member_id='$memberId'");
        }
         echo "<script>
                 alert('Action Completed Successfully!');
               </script>"; 
    }

    
header("Location: ../walletWithdrawStatus");
    exit;

} else {
 header("Location: ../walletWithdrawStatus");

}

 include('../../close-connection.php');
?>
<?php ?>
