<?php
	include_once('../config.php');  
	include_once('../recharges/recharge_api.php');  
    include_once(ABS_PATH.'/custom_function/default.php');       

	
	
	$stmt=$conn->prepare("select eml,nm,phm from UI__Rgst    where cuurentId=:cuurentId     ");   
        $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_INT);  
        $stmt->execute(); 
      $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);            
      $Email  = $UI__Rgst->eml;
  
        $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 nm  from UI__Rgst    where cuurentId=:cuurentId     ");   
    $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_INT);  
    $stmt->execute(); 
    $UI__nm = $stmt->fetch(PDO::FETCH_OBJ);     
    $BY = $UI__nm->nm;
    
    $PAYWAllet =  ($Tamnt+$LevelI+$directIncome)-($totalWithdrwa);      
        
 
	 
	if(!isset($_SESSION['userId']))
	{ 
		 header("Location: ../");
		 die;
	}    
	
	                //echo '<pre>'; print_r($_POST);
			        if( (isset($_POST['Recharge'])) && (($PAYWAllet)>=$_POST['amount']) ) 
			        {
			
        			        $mobile = $_POST['mobile_no'];
        			        $provider_id = $_POST['Operator'];
        			        $amount = $_POST['amount'];
        			        $payment_txnid  = mt_rand(100000, 999999);
        			        
                            
                            $client_id= mt_rand(100000, 999999);
                            $data_string = '{"number": "'.$mobile.'","amount": "'.$amount.'","provider_id": "'.$provider_id.'","client_id": "'.$client_id.'"}';
                            
                            $res=makeRechargeUsingAPI($data_string);
                            //$res = '{"status":0,"status_id":1,"orderid":"5277134","utr":"","report_id":5277134,"message":"Accepted"}';
                            
                            $Data = json_decode($res);   
                            $orderid=$Data->orderid;   
                            
                            $response_date=  date('Y-m-d H:i:s');
                            $stmt =  $conn->prepare("insert into recharge_response (request,response,response_date,txnId) values(:request,:response,:response_date,:txnId)");
                            $stmt->bindValue(':request', $data_string, PDO::PARAM_STR);
                            $stmt->bindValue(':response', $res, PDO::PARAM_STR);
                            $stmt->bindValue(':response_date', $response_date, PDO::PARAM_STR);
                            $stmt->bindValue(':txnId', $payment_txnid, PDO::PARAM_STR);  
                            $stmt->execute();
                            $UTR = $Data->utr;
                            
                            if($Data->status_id==1)
                            {
                            $recharge_status='Success';
                            $status=1;
                            $_SESSION['recharge_success'.$rand_no]=1;    
                            $_SESSION['Regis'] = "Recharge ".$recharge_status; 
                            }
                            else if( ($Data->status_id==2) || ($Data->status_id==4))
                            {
                            $recharge_status='Fail';
                            $status=0;
                            $_SESSION['recharge_success'.$rand_no]=2;
                            $_SESSION['Regis'] = "Recharge ".$recharge_status; 
                            }
                            else if($Data->status_id==3)
                            {
                            $recharge_status='Pending'; 
                            $status=2;
                            $_SESSION['recharge_success'.$rand_no]=3;
                            $_SESSION['Regis'] = "Recharge ".$recharge_status; 
                            }
                            
                            
                            $stmt =  $conn->prepare("insert into UI__Recharges (operator,amount,mobile,user_id,cust_id,type,status,payee_txn_id,user_type,url,orderid,report_id,last_update,client_id,utr) values(:operator,:amount,:mobile,:user_id,:cust_id,:type,:status,:payee_txn_id,:user_type,:url,:orderid,:report_id,:last_update,:client_id,:utr)");            
                            $stmt->bindValue(':operator', $provider_id, PDO::PARAM_STR);
                            $stmt->bindValue(':amount', $amount, PDO::PARAM_STR);
                            $stmt->bindValue(':mobile', $mobile, PDO::PARAM_STR);
                            $stmt->bindValue(':user_id', 1, PDO::PARAM_STR);
                            $stmt->bindValue(':cust_id', $_SESSION['userId'], PDO::PARAM_STR);
                            $stmt->bindValue(':type', 1, PDO::PARAM_INT);
                            $stmt->bindValue(':status', $status, PDO::PARAM_INT);
                            $stmt->bindValue(':payee_txn_id', $payment_txnid, PDO::PARAM_STR);
                            $stmt->bindValue(':user_type', 1, PDO::PARAM_INT);
                            $stmt->bindValue(':url', "NA", PDO::PARAM_STR);
                            $stmt->bindValue(':orderid', $orderid, PDO::PARAM_STR);
                            $stmt->bindValue(':report_id', $Data->report_id, PDO::PARAM_STR);
                            $stmt->bindValue(':last_update', date("d-M-Y H:i"), PDO::PARAM_STR);
                            $stmt->bindValue(':client_id', $client_id, PDO::PARAM_STR);
                            $stmt->bindValue(':utr', $UTR, PDO::PARAM_STR);
                            $stmt->execute();
                            $last_id  = $conn->lastInsertId();  
                            
                            
                            
                            $stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt,order_id,bank_remark,status,accept,recId,wtype) values(:userid,:Tdate,:amt,:orgAmt,:order_id,:bank_remark,:status,:accept,:recId,1)"); 
                            $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                            $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
                            $stmt->bindValue(':amt', $amount, PDO::PARAM_STR);
                            $stmt->bindValue(':orgAmt', "NA" , PDO::PARAM_STR); 
                            $stmt->bindValue(':order_id', $orderid , PDO::PARAM_STR); 
                            $stmt->bindValue(':bank_remark', "NA", PDO::PARAM_STR); 
                            $stmt->bindValue(':status',  $recharge_status, PDO::PARAM_STR); 
                            $stmt->bindValue(':accept', $status , PDO::PARAM_STR);      
                            $stmt->bindValue(':recId', $last_id , PDO::PARAM_STR);    
                            $stmt->execute();       
                            
                            
                                         
                            $subject = "Recharge ".$recharge_status." On ".$SITE_NAME;  
                            $template_type = 1;
                            $attachment = '';
                             
                            $message="
                            <tr>
                            <td align='left' valign='top'>
                            <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                            <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for making recharge with us. Your Details are as follows </h5> 
                            <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> Mobile: $mobile , Amount Rs : $amount </h5>
                            
                            </td>
                            </tr>";     
                            SendMail($Email,$subject,$message,$template_type,$attachment);             
        			        header("Location: mobile.php");
        			        die;
			         }
			         
                      
include_once("../view/dashboard/broadband.php");
?>