<?php 
    require_once 'inserts.php';
$balance = array('bal1'=>'Main wallet','bal2'=>'Profit Wallet');
$balance2 = array('bal1'=>'Main ','bal2'=>'Profit ');

        if(
            isset($_POST['wallet_to']) &&
            isset($_POST['wallet_from']) &&
            isset($_POST['amount']) &&
            isset($_POST['fee'])
        ){

            $wallet_from =trim(mysqli_real_escape_string($conn,$_POST['wallet_from']));
            $wallet_to = trim(mysqli_real_escape_string($conn,$_POST['wallet_to']));
            $amount = trim(mysqli_real_escape_string($conn,$_POST['amount']));
            $fee = trim(mysqli_real_escape_string($conn,$_POST['fee']));
                if($fee==0){$fees=true;}else{$fees =$fee;}
            if(
                !empty($wallet_from)&&
                !empty($wallet_to)&&
                !empty($amount)&&
                !empty($fees)
                ){

                        if($infos["bal$wallet_from"] != ($infos["bal$wallet_to"])){
                            if($wallet_from == 1){ $wallet_from0 ="";}
                            if(($infos["bal$wallet_from0"] >= ($amount+$fee))  ){
                            if( ($amount+$fee) > 0){
                                $_SESSION['trans_id']	= strtoupper('TRX'.substr(md5(rand(1,99999999)),rand(1,15),10));
                                $_SESSION['pay_amount_usd']=$amount;
                                $status = 'success';

                        $_SESSION['exchange_success']=true;
                        $_SESSION['xc_msg']=ucwords($balance2["bal$wallet_from"].' to '.$balance2["bal$wallet_to"].' Wallet Exchanged');


                                $new_email = $mail;
 $new_amount = $amount;
 $new_fee = $fee;
 $new_trans_id = $_SESSION['trans_id'];
 $new_status = $status;
 $new_method = 'system';
 $new_type = 'exchange';
 $new_description = $balance2["bal$wallet_from"].' to '.$balance2["bal$wallet_to"].' Wallet Exchange';
 // $created_at ;
 // $updated_at;			
			if($stmt_trans->execute()){
                $stmt_trans->close();
                $new_balan1 =   ($wallet_from == 1) ? ($infos['bal'] - ($amount+$fee)) :  ($infos['bal'] + ($amount)) ;
                $new_balan2 =   ($wallet_from == 2) ?  $infos['bal2'] - ($amount+$fee)  : $infos['bal2'] + ($amount) ;
                
                $new_balan1 = ($new_balan1 < 0) ? 0 : $new_balan1;
                $new_balan2 = ($new_balan2 < 0) ? 0 : $new_balan2;
                    
                $q= "update info set bal ='$new_balan1' where email ='$mail'";
                mysqli_query($conn,$q);                
                $q= "update info set bal2 ='$new_balan2' where email ='$mail'";
                mysqli_query($conn,$q);
         
                note_msg(' Wallet Exchange Sucessfull.','../wallet-exchange/',1);          
            }else{
                note_msg('Database error occurred. Try again.','../wallet-exchange/',2);           

            }
			 

                            }else{
                     note_msg('Invalid amount','../wallet-exchange/',2);           
                            }
                        }else{
                     note_msg('Insufficient balance in '.$balance["bal$wallet_from"],'../wallet-exchange/',2);
                        }

                }else{
                    note_msg(' The from wallet and to wallet must be different.','../wallet-exchange/',2);           

                }

            }else{
                     note_msg('Invalid amount','../wallet-exchange/',2);           

            }
            
        }else{
            note_msg('Wallet and amount is required','../wallet-exchange/',2);           
  
        }

 
// $conn->close();
				
		
		

?>