<?php
include_once('../config.php');     
if(!isset($_SESSION['userId']))
{ 
header("Location: ../");
die;
}

          
            $stmt=$conn->prepare("select *   from  UI__Bns    where Rid =:userid   and PaidDate=''  ");   
            $stmt->bindValue(':userid',  $_SESSION['userId'] , PDO::PARAM_STR);  
            $stmt->execute(); 
            $UI__DailyROIGene = $stmt->fetchAll(PDO::FETCH_OBJ);    
            
             
            
            $totalTrade = 0; 
            foreach($UI__DailyROIGene as $mmt)
                $totalTrade += $mmt->Tamt;   
                
                
           
            $stmt=$conn->prepare("select *   from  UI__DailyROIGene    where custId =:custId   and PaidDate=''  ");   
            $stmt->bindValue(':custId',  $_SESSION['userId'] , PDO::PARAM_STR);  
            $stmt->execute(); 
            $trd = $stmt->fetchAll(PDO::FETCH_OBJ);     
            
            $totalTrade1 = 0; 
            foreach($trd as $mmt)
                $totalTrade1 += $mmt->amount;     
                
                
            
           
            $stmt=$conn->prepare("select *   from  UI__Tpm    where TopUpId =:TopUpId     ");   
            $stmt->bindValue(':TopUpId',  $_SESSION['userId'] , PDO::PARAM_STR);  
            $stmt->execute(); 
            $Drd = $stmt->fetchAll(PDO::FETCH_OBJ);     
            
            $totalTrade2 = 0; 
            foreach($Drd as $mmt)
                $totalTrade2 += $mmt->Amt;     
                    
                
                

			  
			$stmt=$conn->prepare("select *  from UI__WithDraw    where userid =:sId   ");   
            $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->orgAmt;
            }
              
                 
            if(isset($_POST['Proceed'])) 
            { 
                $FtAmt = (($totalTrade+$totalTrade1+$totalTrade2)-($totalWithdrwa)); 
                if($FtAmt>=$_POST['Fund']) 
                {
   
                $stmt=$conn->prepare("select id  from  UI__WithDraw where userid =:userid");   
                $stmt->bindValue(':userid',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute(); 
                 
                if($stmt->rowCount()==0)   
                {
                    if($_POST['Fund']!=120)  
                    {
                        $_SESSION['Regis'] = "Failed";     
                    }else
                    {
                        $Famt = ($_POST['Fund']*15)/100;
                        $_SESSION['Proceed']  = ($_POST['Fund']-$Famt);  
                        $_SESSION['OrigailProceed']  = $_POST['Fund'];
                    }
                }else if($stmt->rowCount()==1) 
                {         
                    $stmt=$conn->prepare("select cuurentId  from  UI__Rgst where refId =:refId");   
                    $stmt->bindValue(':refId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                    $stmt->execute(); 
                    
                    
                    if($stmt->rowCount()>=2)
                    {
                        $Drd1 = $stmt->fetchAll(PDO::FETCH_OBJ); 
                        $tp = 0;
                        foreach($Drd1 as $mpp)
                        { 
                            $stmt=$conn->prepare("select id  from  UI__DChstr where Rid=:Rid and typ=1");   
                            $stmt->bindValue(':Rid',  $mpp->cuurentId, PDO::PARAM_STR);  
                            $stmt->execute();   
                            
                            if($stmt->rowCount()>0)
                                $tp++;
                        }   
                        
                        if($tp>=2)
                        {
                            if(($_POST['Fund']%400)==0)
                            {
                                $Famt = ($_POST['Fund']*15)/100;
                                $_SESSION['Proceed']  = ($_POST['Fund']-$Famt);  
                                $_SESSION['OrigailProceed']  = $_POST['Fund']; 
                            }else
                                $_SESSION['Regis'] = "Failed";
                        }else {
                            
                             $_SESSION['Regis'] = "Failed";
                        }
                    }else
                        $_SESSION['Regis'] = "Failed";
                }else
                {
                 
                        $Famt = ($_POST['Fund']*15)/100;
                        $_SESSION['Proceed']  = ($_POST['Fund']-$Famt);  
                        $_SESSION['OrigailProceed']  = $_POST['Fund']; 
                }       
                header("Location: withdrw.php");
                die;
              }else
                {
                header("Location: withdrw.php");
                die;
                }
            }
 
            if(isset($_POST['submit'])) 
            {     
                 
                if( (date("H")>=7)  && (date("H")<=12))
                {     
                    $custIdResult2 =  (($totalTrade+$totalTrade1+$totalTrade2)-($totalWithdrwa));    
                    if($custIdResult2 >= $_POST['OrigailProceed']) 
                    {  
                        $stmt=$conn->prepare("insert into UI__WithDraw (userid,Tdate,amt,orgAmt) values(:userid,:Tdate,:amt,:orgAmt)  ");   
                        $stmt->bindValue(':userid', $_SESSION['userId'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i"), PDO::PARAM_STR); 
                        $stmt->bindValue(':amt', ($_POST['MyFinalWithdraw']) , PDO::PARAM_STR); 
                        $stmt->bindValue(':orgAmt', ($_POST['OrigailProceed']) , PDO::PARAM_STR);     
                        $stmt->execute();        
                        $_SESSION['Regis'] = 1;
                        header("Location: withdrw.php");
                        die;
                    } 
                }
                else{
                            $_SESSION['Regis'] = "Failed";  
                            header("Location: withdrw.php");
                            die;
                    }
             }        

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