<?php
	include_once('../config.php');      
	include_once(ABS_PATH.'/custom_function/default.php'); 
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
                        
                        
                 
                 
                $stmt=$conn->prepare("select * from  UI__Rgst    where cuurentId=:custId     ");   
                $stmt->bindValue(':custId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute(); 
                $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);    
                $beneficiary_name = $UI__Rgst->nm;
    
                $ctr=0;
                $stmt=$conn->prepare("select * from  UI__InstaLoan    where custId=:custId     ");   
                $stmt->bindValue(':custId',  $_SESSION['userId'] , PDO::PARAM_INT);  
                $stmt->execute();    
                $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);   
        
                                
                if(isset($_POST['submit']))
                {    
                    $stmt=$conn->prepare("select balance from   UI__InstaLoanRepay    where custId=:custId    order by id desc  ");   
                    $stmt->bindValue(':custId',  $_SESSION['userId'] , PDO::PARAM_INT);  
                    $stmt->execute(); 
                    if($stmt->rowCount()>0)  
                    {
                        $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);  
                        $bal = $UI__Rgst->balance;  
                    }else
                        $bal = $UI__Rgst->amt-($_POST['amount']);    
                     
                     
                     
                    if( ($_POST['amount'] <= $bal) && (GetBalance()>=$_POST['amount']) )
                    { 
                        if($stmt->rowCount()>0)  
                            $bal -=$_POST['amount'];
                            
                        $stmt=$conn->prepare("insert into  UI__InstaLoanRepay  (amt,Tdate,custId,balance) values(:amt,:Tdate,:custId,:balance)");   
                        $stmt->bindValue(':amt',  $_POST['amount'] , PDO::PARAM_STR);    
                        $stmt->bindValue(':Tdate',  date("d-M-Y") , PDO::PARAM_STR);
                        $stmt->bindValue(':custId', $_SESSION['userId']  , PDO::PARAM_STR); 
                        $stmt->bindValue(':balance', $bal , PDO::PARAM_STR);  
                        $stmt->execute();    
                         
                        $payee_txn_id = mt_rand(121212,232323);
                    	$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',  $payee_txn_id , PDO::PARAM_STR); 
						$stmt->bindValue(':bank_remark', "NA", PDO::PARAM_STR); 
						$stmt->bindValue(':status', 0 , PDO::PARAM_STR); 
						$stmt->bindValue(':accept', 1 , PDO::PARAM_STR);      
						$stmt->execute();          
						LvBal($_SESSION['userId'],$_POST['amount'],2,"Insta Credit Repay",$beneficiary_name);    
						
                        $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',   16 , PDO::PARAM_STR);   
                        $stmt->bindValue(':bank',  "Insta Credit Repay" , 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['amount']   , 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', $_SESSION['userId']  , PDO::PARAM_STR);  
                        $stmt->bindValue(':cdId', $_SESSION['LastIntId']  , PDO::PARAM_STR); 
                        $stmt->bindValue(':Topay',  'DP112233' , PDO::PARAM_STR); 
                        $stmt->bindValue(':TopayName',  $_SESSION['userId']  , PDO::PARAM_STR);     
                        $stmt->execute(); 

                        $_SESSION['Regis'] = "Amount transferred successfully";
                        $_SESSION['topSuc'] = 1;
                    }else if (GetBalance()<$_POST['amount'])
                        $_SESSION['Regis'] = "Low wallet error!!";    
                    else
                        $_SESSION['Regis'] = "Amount is greater than taken credit amount";   
                        
                    
                    header("Location: creditsummary.php");
                    die;
                }  
                
                    $stmt=$conn->prepare("select balance from   UI__InstaLoanRepay    where custId=:custId    order by id desc  ");   
                    $stmt->bindValue(':custId',  $_SESSION['userId'] , PDO::PARAM_INT);  
                    $stmt->execute();  
                    if($stmt->rowCount()>0)   
                    {
                        $UI__Rgstx = $stmt->fetch(PDO::FETCH_OBJ);  
                        $bal = $UI__Rgstx->balance;
                    }else
                        $bal = $UI__Rgst->amt;
                        
include_once("../view/dashboard/creditsummary.php");
?>