<?php
	require_once 'inserts.php';
	$with = array('method1'=>'bitcoin','method2'=>'ethereum','method3'=>'litecoin','method4'=>'tether','method5'=>'bank Transfer');
// cryptos
	if(isset($_POST['withdraw_method_id'])){
		if(isset($_POST['method_wallet'])){
			if(isset($_POST['amount'])){

				$method_id = trim(mysqli_real_escape_string($conn,$_POST['withdraw_method_id']));
				$wallet_address = trim(mysqli_real_escape_string($conn,$_POST['method_wallet']));
				$amount = trim(mysqli_real_escape_string($conn,$_POST['amount']));

				if(!empty($method_id)){
					if(!empty($wallet_address)){
						if(!empty($amount)){
							// alert("$method_id  $wallet_address $amount");

								if((($infos['bal']+$infos['profit'])>$amount) && ($amount>0) ){
								    
								    if( $amount > $infos['profit']){
								        $new_profit = 0;
								        $current_bal  = $infos['bal']+$infos['profit'];
								        $newbal = $current_bal -$amount;
								        
								        $new_bals  = $newbal;
									$current_user_email = $mail;
									$d_new_bal1 =$new_bals;
									$d_new_profit1 =$new_profit;
								        
								    }else{
								        $new_profit = $infos['profit'] - $amount;
								        $newbal = $infos['bal'];
								        
									$new_bals  = $newbal;
									$current_user_email = $mail;
									$d_new_bal1 =$new_bals;
									$d_new_profit1 =$new_profit;
								        
								    }
								    
									
									
									
									if($stmt_update_my_bal1->execute()){
									    $stmt_update_my_profit1->execute();
									    
				$trans_id =	$_SESSION['trans_id']	= strtoupper('TRX'.substr(md5(rand(1,99999999)),rand(1,15),10));
				$new_trans_id = $trans_id;
				$new_description = 'Withdraw With '.$with['method'.$method_id] ;
				$new_email = $mail;
				$new_amount = $amount;
				$new_fee = $amount*0;
				$new_status = 'pending';
				$new_method = $with['method'.$method_id];
				$new_type ='withdraw';
				$created_at;
				$updated_at;

$gateway = $with['method'.$method_id];
$details = $wallet_address;
				if($stmt_trans->execute()){
    require_once('with_mail.php');

					$_SESSION['withdraw_state'] = true;
					$_SESSION['withdraw_amount'] = $amount;
					$_SESSION['withdraw_method'] = $with['method'.$method_id];
					note_msg('Withdrawal Request sent','../withdraw',1);

				}else{
					note_msg('Database error occurred','../withdraw',2);
				}

						}else{
							note_msg('Database error occurred','../withdraw',2);
						}
								}else{
									note_msg('Insufficient balance','../withdraw',2);
								}

						}else{
							note_msg('All fields required','../withdraw',2);
						}
					}else{
						note_msg('All fields required','../withdraw',2);
					}
				}else{
					note_msg('All fields required','../withdraw',2);
				}

			}
		}	
	}


// usdt
	if(isset($_POST['withdraw_method_id'])){
		if(isset($_POST['method_wallet'])){
			if(isset($_POST['amount2'])){
				if(isset($_POST['method_wallet_network'])){
					if(isset($_POST['wallet_tag'])){

						
				$method_id = trim(mysqli_real_escape_string($conn,$_POST['withdraw_method_id']));
				$wallet_address = trim(mysqli_real_escape_string($conn,$_POST['method_wallet']));
				$amount = trim(mysqli_real_escape_string($conn,$_POST['amount2']));
				$method_wallet_network = trim(mysqli_real_escape_string($conn,$_POST['method_wallet_network']));
				$wallet_tag = trim(mysqli_real_escape_string($conn,$_POST['wallet_tag']));

				if(!empty($method_id)){
					if(!empty($wallet_address)){
						if(!empty($amount)){	
						if(!empty($wallet_address)){
							
						if((($infos['bal']+$infos['profit'])>=$amount) && ($amount>0) ){
								    
								    if( $amount > $infos['profit']){
								        $new_profit = 0;
								        $current_bal  = $infos['bal']+$infos['profit'];
								        $newbal = $current_bal -$amount;
								        
								        $new_bals  = $newbal;
									$current_user_email = $mail;
									$d_new_bal1 =$new_bals;
									$d_new_profit1 =abs($new_profit);
								        
								    }else{
								        $new_profit = $infos['profit'] - $amount;
								        $newbal = $infos['bal'];
								        
								        
								        
									$new_bals  = $newbal;
									$current_user_email = $mail;
									$d_new_bal1 =abs($new_bals);
									$d_new_profit1 =abs($new_profit);
									
									
									
								        
								    }
								    
							if($stmt_update_my_bal1->execute()){
							      $stmt_update_my_profit1->execute();
		$trans_id =	$_SESSION['trans_id']	= strtoupper('TRX'.substr(md5(rand(1,99999999)),rand(1,15),10));
		$new_trans_id = $trans_id;
		$new_description = 'Withdraw With '.$with['method'.$method_id] ;
		$new_email = $mail;
		$new_amount = $amount;
		$new_fee = $amount*0;
		$new_status = 'pending';
		$new_method = $with['method'.$method_id];
		$new_type ='withdraw';
		$created_at;
		$updated_at;
$gateway = $with['method'.$method_id];
$details = $wallet_address.strtoupper("($method_wallet_network)");

		if($stmt_trans->execute()){

    require_once('with_mail.php');

			$_SESSION['withdraw_state'] = true;
			$_SESSION['withdraw_amount'] = $amount;
			$_SESSION['withdraw_method'] = $with['method'.$method_id];
			
			note_msg('Withdrawal pending','../withdraw',1);

		}else{
			note_msg('A Database error occurred','../withdraw',2);
		}

								}else{
									note_msg('A Database error occurred','../withdraw',2);
								}
						}else{
							note_msg('Insufficient balance','../withdraw',2);

						}



						}else{
							note_msg('All fields required','../withdraw',2);
						}
					}else{
						note_msg('All fields required','../withdraw',2);
					}
				}else{
					note_msg('All fields required','../withdraw',2);
				}
			}else{
				note_msg('All fields required','../withdraw',2);
			}
						
					}
				}
			}
		}	
	}


// bank 
	if(isset($_POST['withdraw_method_id'])){
		if(isset($_POST['bank_name'])){
			if(isset($_POST['amount3'])){
				if(isset($_POST['account_number'])){
					if(isset($_POST['bank_country'])){
						if(isset($_POST['bank_holder_name'])){
							if(isset($_POST['bank_branch'])){
								if(isset($_POST['routing_number'])){
									if(isset($_POST['iban_number'])){

										$method_id = trim(mysqli_real_escape_string($conn,$_POST['withdraw_method_id']));
										$bank_name = trim(mysqli_real_escape_string($conn,$_POST['bank_name']));
										$amount = trim(mysqli_real_escape_string($conn,$_POST['amount3']));
										$account_number = trim(mysqli_real_escape_string($conn,$_POST['account_number']));
										$bank_country = trim(mysqli_real_escape_string($conn,$_POST['bank_country']));
										$bank_holder_name = trim(mysqli_real_escape_string($conn,$_POST['bank_holder_name']));
										$bank_branch = trim(mysqli_real_escape_string($conn,$_POST['bank_branch']));
										$routing_number = trim(mysqli_real_escape_string($conn,$_POST['routing_number']));
										$iban_number = trim(mysqli_real_escape_string($conn,$_POST['iban_number']));
						
										if(!empty($method_id)){
											if(!empty($bank_name)){
												if(!empty($amount)){
												if(!empty($account_number)){
												if(!empty($bank_country)){
												if(!empty($bank_holder_name)){
													if(!empty($bank_branch)){
												if(!empty($routing_number)){
													if(!empty($iban_number)){
		

// action

if((($infos['bal']+$infos['profit'])>$amount) && ($amount>0) ){
								    
								    if( $amount > $infos['profit']){
								        $new_profit = 0;
								        $current_bal  = $infos['bal']+$infos['profit'];
								        $newbal = $current_bal -$amount;
								        
								        $new_bals  = $newbal;
									$current_user_email = $mail;
									$d_new_bal1 =$new_bals;
									$d_new_profit1 =$new_profit;
								        
								    }else{
								        $new_profit = $infos['profit'] - $amount;
								        $newbal = $infos['bal'];
								        
									$new_bals  = $newbal;
									$current_user_email = $mail;
									$d_new_bal1 =$new_bals;
									$d_new_profit1 =$new_profit;
								        
								    }
if($stmt_update_my_bal1->execute()){
      $stmt_update_my_profit1->execute();
$trans_id =	$_SESSION['trans_id']	= strtoupper('TRX'.substr(md5(rand(1,99999999)),rand(1,15),10));
$new_trans_id = $trans_id;
$new_description = 'Withdraw With '.$with['method'.$method_id] ;
$new_email = $mail;
$new_amount = $amount;
$new_fee = $amount*0;
$new_status = 'pending';
$new_method = $with['method'.$method_id];
$new_type ='withdraw';
$created_at;
$updated_at;

$gateway = $with['method'.$method_id];
$details = ucwords("$bank_name $account_number $bank_country $bank_holder_name $routing_number");
if($stmt_trans->execute()){

    require_once('with_mail.php');

$_SESSION['withdraw_state'] = true;
$_SESSION['withdraw_amount'] = $amount;
$_SESSION['withdraw_method'] = $with['method'.$method_id];

note_msg('Withdrawal pending','../withdraw',1);

}else{
note_msg('A Database error occurred','../withdraw',2);
}

	}else{
		note_msg('A Database error occurred','../withdraw',2);
	}
}else{
note_msg('Insufficient balance','../withdraw',2);

}

// end action 




													}else{
														note_msg('All fields required','../withdraw',2);
													}
												}else{
													note_msg('All fields required','../withdraw',2);
												}
											}else{
												note_msg('All fields required','../withdraw',2);
											}
										}else{
											note_msg('All fields required','../withdraw',2);
										}
									}else{
										note_msg('All fields required','../withdraw',2);
									}
								}else{
									note_msg('All fields required','../withdraw',2);
								}
							}else{
								note_msg('All fields required','../withdraw',2);
							}
						}else{
							note_msg('All fields required','../withdraw',2);
						}
					}else{
						note_msg('All fields required','../withdraw',2);
					}
									}
								}
							}
						}
					}
				}
			}
		}	
	}

// note_msg('All fields required','../withdraw',2);

?>