<?php
	include_once('../config.php');   
	             
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    } 
     
     
            
            function Refcall($ref)
            {
                global $conn;
                $stmt=$conn->prepare("select cuurentId   from UI__Rgst where refId =:cuurentId ");   
                $stmt->bindValue(':cuurentId', $ref , PDO::PARAM_INT);  
                $stmt->execute(); 
                $Result = $stmt->fetchAll(PDO::FETCH_OBJ);   
                return $Result;
            }
            
            $stmt=$conn->prepare("select cuurentId from UI__Rgst where custId =:custId ");   
            $stmt->bindValue(':custId', $_SESSION['userCustId']  , PDO::PARAM_STR);  
            $stmt->execute(); 
            $Result = $stmt->fetch(PDO::FETCH_OBJ);   
            
            $myRefId = $Result->cuurentId;  
            $stmt=$conn->prepare("select cuurentId   from UI__Rgst where refId =:cuurentId ");   
            $stmt->bindValue(':cuurentId', $Result->cuurentId  , PDO::PARAM_INT);  
            $stmt->execute(); 
            
 
            
            $myTT = 0;
            if($stmt->rowCount()>0)
            { 
                $Result = $stmt->fetchAll(PDO::FETCH_OBJ);     
                foreach($Result as $dtm) 
                    $stepCt['step1']['id'][] = $dtm->cuurentId;  
                      
                foreach($Result as $dtm) 
                    $stepCt['step2']['id'][] =  Refcall($dtm->cuurentId); 
                
            }
              
			  
			  
             
              
            for($i=0;$i<count($stepCt['step2']['id']);$i++)
            {
                 
                if(count($stepCt['step2']['id'][$i])>0)
                {       
                   foreach($stepCt['step2']['id'][$i] as $dtm)
                     $stepCt['step3']['id'][] =  Refcall($dtm->cuurentId);  
                } 
            }   
            
			
			echo '<pre>'; print_r($stepCt['step1']['id']);
			echo '<pre>'; print_r($stepCt['step2']['id']);
			die;
             
                $ActTeam = 0;
                $NBusinessAmt = 0;      
                foreach($stepCt['step1']['id'] as $dtm) 
                {      
                    
                    $stmt=$conn->prepare("select *  from UI__DChstr where Rid=:Rid and typ=1");   
                    $stmt->bindValue(':Rid', $dtm , PDO::PARAM_STR);  
                    $stmt->execute(); 
                    $Resultx = $stmt->fetchAll(PDO::FETCH_OBJ);   
                    
                    foreach($Resultx  as $mmt)
                    {
    					$NBusinessAmt += $mmt->amt;
    					 $ActTeam++;
                    }
                }  
                
                 
                
                
                foreach($stepCt['step2']['id'] as $dtm) 
                {    
                    foreach($dtm as $dtmx) 
                    {    
                        $stmt=$conn->prepare("select *  from UI__DChstr where Rid=:Rid and typ=1");   
                        $stmt->bindValue(':Rid', $dtmx->cuurentId , PDO::PARAM_STR);  
                        $stmt->execute(); 
                        if($stmt->rowCount()>0)
                        {
                            $Resultx = $stmt->fetchAll(PDO::FETCH_OBJ);   
                            foreach($Resultx as $MM)
                            {
                                $NBusinessAmt += $MM->amt; 
                                $ActTeam++;   
                            }
                        }
                        
                    }
                }
                
                
                foreach($stepCt['step3']['id'] as $dtm) 
                {    
                    foreach($dtm as $dtmx) 
                    {    
                        $stmt=$conn->prepare("select *  from UI__DChstr where Rid=:Rid and typ=1");   
                        $stmt->bindValue(':Rid', $dtmx->cuurentId , PDO::PARAM_STR);  
                        $stmt->execute(); 
                        if($stmt->rowCount()>0)
                        {
                            $Resultx = $stmt->fetchAll(PDO::FETCH_OBJ);   
                            foreach($Resultx as $MM)
                            {
                                $NBusinessAmt += $MM->amt; 
                                $ActTeam++;   
                            }
                        }
                        
                    }
                }  
                 
                
                echo $NBusinessAmt;die;
               
                
                
include_once("../view/dashboard/default.php");
?>