<?php
	include_once('../config.php');   
    include_once(ABS_PATH.'/custom_function/default.php');  
          
    	
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
         
    if(isset($_POST['submit'])) 
    {  
       
             
            $stmt=$conn->prepare("select * from UI__Crper");    
            $stmt->execute(); 
            $Cper = $stmt->fetch(PDO::FETCH_OBJ);      
            
            $_POST['cuurentId'] = trim($_POST['cuurentId']);        
            $stmt=$conn->prepare("select  *   from UI__Rgst where cuurentId =:cuurentId ");   
            $stmt->bindValue(':cuurentId', $_POST['cuurentId'] , PDO::PARAM_STR);  
            $stmt->execute(); 
            $affected_rows_UI__DChstr = $stmt->rowCount();  
            $FResult = $stmt->fetch(PDO::FETCH_OBJ);   
            $tNAM = $FResult->nm;
            $tId = $_POST['cuurentId'];
            $tPhm  = $FResult->phm;   
            
            $stmt=$conn->prepare("select id from  UI__DChstr where typ=1 and sId=:sId ");    
            $stmt->bindValue(':sId', $FResult->custId , PDO::PARAM_STR);  
            $stmt->execute(); 
            
         
            $MPin  = ($_POST['TopPin']);
            $stmt = $conn->prepare("SELECT AMMT,id FROM UI__PnMX WHERE MPin=:MPin and exp!=1");  
            $stmt->execute(array(':MPin'=>$MPin));
            $affected_rows = $stmt->rowCount();	 
            
              
            $data['Data'] =  $_POST['AMMT'];  
            $stmt=$conn->prepare("select  availableFund  from  UI__Fundtransfer   where userid=:cuurentId order by id desc");  
            $stmt->bindValue(':cuurentId', $_SESSION['userId']  , PDO::PARAM_STR);
            $stmt->execute();  
            $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
            $availableFund = 0;
             
            
            $AllUsersT = GetBalance(); 
            if($AllUsersT>=$data['Data']) 
            {
                $availableFund = 1;    
                $totalAvaib = $AllUsers->availableFund;
            }    
            
            $stmt=$conn->prepare("select id from UI__Rgst  where  cuurentId=:cuurentId  ");   
            $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
            $stmt->execute();
            $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);   
            
            $stmt =  $conn->prepare("select *  from  _pkCc__  where  Uxd=:Uxd ");   
            $stmt->bindValue(':Uxd', $UI__Rgst->id, PDO::PARAM_STR);
            $stmt->execute(); 
            $RstUxd = $stmt->fetch(PDO::FETCH_OBJ);
            $PAYWAllet =  GetBalance(); 
            $verify = password_verify($AllUsersT, $RstUxd->bl1); 
              
            if( ($AllUsersT >= $data['Data'])  && ($data['Data']>=1000) && ($affected_rows_UI__DChstr>0) )
            {    
                $Result = $stmt->fetch(PDO::FETCH_OBJ);    
                $stmt=$conn->prepare("update  UI__PnMX set exp=1 where id=:id ");    
                $stmt->bindValue(':id', $Result->id , PDO::PARAM_STR);  
               // $stmt->execute(); 
                     
                $PlanAMt = $data['Data'] ;    
                $stmt=$conn->prepare("select refId,custId,nm  from UI__Rgst where cuurentId =:cuurentId ");   
                $stmt->bindValue(':cuurentId', $_POST['cuurentId'] , PDO::PARAM_INT);  
                $stmt->execute(); 
                 
               
                if($stmt->rowCount() > 0)
                {     
                    $Result = $stmt->fetch(PDO::FETCH_OBJ);
                    $userNm = $Result->nm;
                    $CC = $Result->refId;
                    $MyCust =  $Result->custId;
                    $ard[] = $CC;
                    for($i=0;$i<=9;$i++)
                    {
                        $stmt=$conn->prepare("select refId from UI__Rgst where cuurentId =:cuurentId ");   
                        $stmt->bindValue(':cuurentId',$CC, PDO::PARAM_INT);  
                        $stmt->execute();  
                        if($stmt->rowCount() > 0)
                        { 
                            $Result = $stmt->fetch(PDO::FETCH_OBJ);   
                            $CC = $Result->refId;
                            $ard[] = $CC;
                        }
                    }  
                }   
                   
                
                $totalAvaib -= $data['Data'];  
                $stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund,topUpId) values(:userid,:Tdate,:amt,:availableFund,:topUpId)    "); 
                $stmt->bindValue(':userid', $_SESSION['userId'] , PDO::PARAM_STR); 
                $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                $stmt->bindValue(':amt',$data['Data'], PDO::PARAM_STR);  
                $stmt->bindValue(':availableFund', $totalAvaib , PDO::PARAM_STR);  
                $stmt->bindValue(':topUpId', $_POST['cuurentId'] , PDO::PARAM_STR);    
                $stmt->execute();  
                
                $stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt,order_id,bank_remark,status,accept,decucationType) values(:userid,:Tdate,:amt,:orgAmt,:order_id,:bank_remark,:status,:accept,4)"); 
                $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
                $stmt->bindValue(':amt', (0) , PDO::PARAM_STR);
                $stmt->bindValue(':orgAmt', (0) , PDO::PARAM_STR); 
                $stmt->bindValue(':order_id',  "NA" , PDO::PARAM_STR); 
                $stmt->bindValue(':bank_remark', "PA", PDO::PARAM_STR); 
                $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                $stmt->bindValue(':accept', 1 , PDO::PARAM_STR);      
                $stmt->execute();  
                
                $CoinVal = ($data['Data']);    
                $stmt=$conn->prepare("insert into  UI__DChstr (typ,amt,Tdate,Rid,sId,percet,coin,byId,dys,specialUser) values(:typ,:amt,:Tdate,:Rid,:sId,:percet,:coin,:byId,:dys,1) ");   
                $stmt->bindValue(':typ', 1, PDO::PARAM_INT); 
                $stmt->bindValue(':amt', $data['Data'], PDO::PARAM_INT); 
                $stmt->bindValue(':Tdate', date("d-M-Y H:i:s") , PDO::PARAM_STR); 
                $stmt->bindValue(':Rid', $_POST['cuurentId']  , PDO::PARAM_STR);
                $stmt->bindValue(':sId', $MyCust  , PDO::PARAM_STR);
                $stmt->bindValue(':percet', 0  , PDO::PARAM_STR);  
                $stmt->bindValue(':coin', 0  , PDO::PARAM_STR);   
                $stmt->bindValue(':byId', $_SESSION['userId']  , PDO::PARAM_STR);     
                $stmt->bindValue(':dys', 100  , PDO::PARAM_STR);     
                $stmt->execute();   
                     
				 
				$stmt=$conn->prepare("select refId,custId   from UI__Rgst where cuurentId =:cuurentId ");   
                $stmt->bindValue(':cuurentId', $_POST['cuurentId'] , PDO::PARAM_INT);  
                $stmt->execute(); 
				$Dref = $stmt->fetch(PDO::FETCH_OBJ);   
				
                 
				
				$stmt=$conn->prepare("select  custId from UI__Rgst where cuurentId =:cuurentId ");   
                $stmt->bindValue(':cuurentId', $Dref->refId , PDO::PARAM_INT);  
                $stmt->execute(); 
				$MdRef = $stmt->fetch(PDO::FETCH_OBJ);     
                
                LvBal($_SESSION['userId'],$_POST['AMMT'],2,"TopUp",$userNm." (".$_POST['cuurentId'].")");  
                
                
                $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute();
                $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);     	
                

                $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                $stmt->bindValue(':type',   6 , PDO::PARAM_STR);   
                $stmt->bindValue(':bank',  "Top Up" , PDO::PARAM_STR);   
                $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                $stmt->bindValue(':amountPaid', $_POST['AMMT']  , PDO::PARAM_STR); 
                $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                $stmt->bindValue(':custId', $_POST['cuurentId'] , PDO::PARAM_STR);  
                $stmt->bindValue(':cdId', $_SESSION['LastIntId']  , PDO::PARAM_STR); 
                $stmt->bindValue(':Topay',  $_POST['cuurentId']  , PDO::PARAM_STR); 
                $stmt->bindValue(':TopayName', $_POST['cuurentId'] , PDO::PARAM_STR);     
                $stmt->execute();    
                $MYLASTID = $_SESSION['LastIntId'];
                unset($_SESSION['LastIntId']);
                   
                               
                $stmt=$conn->prepare("insert into UI__TopWithDrw(custId,Tdate,status,type,amt) values(:custId,:Tdate,:status,:type,:amt)"); 
                $stmt->bindValue(':custId',  $_POST['cuurentId'] , PDO::PARAM_STR);   
                $stmt->bindValue(':Tdate',  date("d-M-Y") , PDO::PARAM_STR);   
                $stmt->bindValue(':status', 0 , PDO::PARAM_INT);     
                $stmt->bindValue(':type',  1 , PDO::PARAM_INT);
                $stmt->bindValue(':amt', $data['Data'] , PDO::PARAM_STR);
                $stmt->execute();      
                
                
                $stmt=$conn->prepare("select eml from UI__Rgst  where  cuurentId=:cuurentId    ");   
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute(); 
                $EMAIL = $stmt->fetch(PDO::FETCH_OBJ);   

                
                $subject = "Top Up  On ".$SITE_NAME;  
                $template_type = 1;
                $attachment = '';
                $name = $_POST['cuurentId'];
                $Amt = $data['Data'];
                
                $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 Wallet Transfer with us. Your Details are as follows </h5> 
                <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> To: $name , Amount Rs : $Amt </h5>
                
                </td>
                </tr>"; 
                
                  
                  
                $tNAM = $FResult->nm;
                $tId = $_POST['cuurentId'];  
                $CMB = '('.$tId.'),'.$tNAM;
                $am = $_POST['AMMT'];
                $mob = $tPhm;
                $msg = urlencode('Dear '.$CMB.', Congratulations, your TOPUP amount Rs. '.$am.' has been successfully done, DailyPay team.'); 
                $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169907140786246';
                
                
                $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);  

                
                SendMail($EMAIL->eml,$subject,$message,$template_type,$attachment);  
                SendMail("vsn.national@gmail.com",$subject,$message,$template_type,$attachment); 
                
                header("Location: ".ROOTPATH."/dashboard/viewRec.php?id=".$MYLASTID);
                die;

                $_SESSION['Regis'] = "Congratulations Top Up Successful!!";
                $_SESSION['topSuc'] =  1;
                header("Location: sptopup.php"); 
                die;
            }
            else 
			{ 
                 
                 $_SESSION['Regis'] = "Top Up Failed";
                 header("Location: sptopup.php");
                 die; 
            }
    }       
    
          
    $stmt=$conn->prepare("select * from  UI__PnMX   where Transfered=:custId ");    
    $stmt->bindValue(':custId',  $_SESSION['userCustId'], PDO::PARAM_STR);
    $stmt->execute(); 
    $AllIds = $stmt->fetchAll(PDO::FETCH_OBJ); 
    
    $PAYWAllet =  GetBalance();   

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