<?php
	include_once('../config.php');      
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
                      
    if(isset($_POST['submit'])) 
    {              
        
                
					$stmt=$conn->prepare("select *  from UI__DailyROIGene    where custId=:sId     ");   
					$stmt->bindValue(':sId', $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$XUI__DailyROIGene = $stmt->fetchAll(PDO::FETCH_OBJ);  

					$Tamnt = 0;
					foreach($XUI__DailyROIGene as $xx)
					{
					$Tamnt +=$xx->amount;
					}      


					$stmt=$conn->prepare("select *  from UI__Bns    where Rid =:sId   ");   
					$stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$UI__Bns = $stmt->fetchAll(PDO::FETCH_OBJ); 

					$LevelI = 0;
					foreach($UI__Bns as $dddm)
					{
					$LevelI +=$dddm->Tamt;
					}


					$stmt=$conn->prepare("select Amt   from UI__Tpm where TopUpId=:refId   ");   
					$stmt->bindValue(':refId',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$MdirectB = $stmt->fetchAll(PDO::FETCH_OBJ);  

					$directIncome = 0;
					foreach($MdirectB as $dmm)
					$directIncome += $dmm->Amt;                




					$stmt=$conn->prepare("select *  from UI__WithDraw    where userid=:sId and (accept=1 ||accept=2)  ");   
					$stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$UI__WithDraw = $stmt->fetchAll(PDO::FETCH_OBJ); 


					$totalWithdrwa = 0;
					foreach($UI__WithDraw as $tl)
					{
					$totalWithdrwa +=$tl->amt;
					}  


					$stmt=$conn->prepare("select availableFund from UI__Fundtransfer  where  userid=:userid  order by id desc  ");   
					$stmt->bindValue(':userid',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$UI__Fundtransfer = $stmt->fetch(PDO::FETCH_OBJ);     
					$PAYWAllet =  ($Tamnt+$LevelI+$directIncome+$UI__Fundtransfer->availableFund)-($totalWithdrwa);        

                    
                    if($PAYWAllet<$_POST['amount'])
                    {
                        $_SESSION['Regis'] = "Transaction Fail Due To Low Balance";
                        header("Location: Wllat_transf.php");
                        die; 
                    }
                
                if($_POST['amount']>=10 && $_POST['amount']<=25000) 
                { 
                 
                    $txnid = mt_rand(234553,343553);
                    $amount= $_POST['amount'].".00";
                    $apisecret = "yZe6SQavyVPe4kVfMjebHnbfKTNPyFz98PJvKT6qgeRR4sNr"; 
                    $api_user_id= "z3dz5LVEYBw34AUXAcaUr7V8jNATpPcfbJpMfZbd48tJNzNA";
                    $concat = $amount."|".$api_user_id."|".$txnid."|".$apisecret; 
                    $hash = hash("sha512",$concat);    
                    
                    $mobile = $_POST['mobile']; 
                    $Operator = $_POST['Operator'];
                    $url = 'https://api.komparify.com/api/v2/recharge?search_type=voucher&number='.$mobile.'&operator='.$Operator.'&txnid='.$txnid.'&securehash='.$hash.'&decimal_value='.$amount.'&method=GET&api_user_id=z3dz5LVEYBw34AUXAcaUr7V8jNATpPcfbJpMfZbd48tJNzNA';   
                       
                    $crl = curl_init();
                    curl_setopt($crl, CURLOPT_URL, $url);
                    curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                    $response = curl_exec($crl);   
                    curl_close($crl);
    
                    //    $Res = '{ "error" : "false", "order": "true", "sid": "9762811448", "operator": "PayTM Wallet", "amount": "12", "decimal_amount" : "12.0", "type": "Voucher", "rechargetype": "R", "orderid": "17809-38277-02596", "txnid": "332750", "status":"Transaction In Progress" ,"deducted_amount" : 12.1416 ,"remaining_balance" : 1795.7168 }';
                    $Data = (array)json_decode($response);
    
                    if($Data['error']=='false') 
                    {
                        $stmt =  $conn->prepare("insert into UI__WalletTransferData (custId,TxnId,ph,status,orderId,Tdate) values(:custId,:TxnId,:ph,:status,:orderId,:Tdate)");            
                        $stmt->bindValue(':custId', $_SESSION['userId'], PDO::PARAM_STR);
                        $stmt->bindValue(':TxnId', $Data['txnid'], PDO::PARAM_STR);
                        $stmt->bindValue(':ph', $Data['sid'], PDO::PARAM_STR);
                        $stmt->bindValue(':status', $Data['status'], PDO::PARAM_STR);
                        $stmt->bindValue(':orderId', $Data['orderid'], PDO::PARAM_STR); 
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i"), PDO::PARAM_STR);   
                        $stmt->execute();
                         
                         
    					$stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt,order_id,bank_remark,status,accept) values(:userid,:Tdate,:amt,:orgAmt,:order_id,:bank_remark,:status,:accept)"); 
                        $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                        $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
                        $stmt->bindValue(':amt', $_POST['amount'] , PDO::PARAM_STR);
                        $stmt->bindValue(':orgAmt', $_POST['amount'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':order_id',  $Data['orderid'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':bank_remark', "NA", PDO::PARAM_STR); 
                        $stmt->bindValue(':status', "NA" , PDO::PARAM_STR); 
                        $stmt->bindValue(':accept', 1 , PDO::PARAM_STR);      
                        $stmt->execute();      
                        LvBal($_SESSION['userId'],$_POST['amount'],2,"Gifts Transfer",$Data['sid']);
                        
                        $_SESSION['Regis'] = "Transaction successful";
                        $_SESSION['topSuc'] = 1;
                        
                    }else
                        $_SESSION['Regis'] = "Transaction Fail";  
                }
                else
                    $_SESSION['Regis'] = "Amount should be between 10 and 25000"; 
                header("Location: Wllat_transf.php");
                die; 
    }
    
    $stmt=$conn->prepare("select * from  UI__Rgst where cuurentId=:cuurentId");    
    $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
    $stmt->execute(); 
    $Result = $stmt->fetch(PDO::FETCH_OBJ);      
	

include_once("../view/dashboard/Gifts_transf.php");
?>