<?php
	include_once('../config.php');   
   
          
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }
    
    if(isset($_POST['submit']))
    {
        
            $CustId = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),0, 8);  
            $stmt=$conn->prepare("insert into  UI__Rgst (nm,phm,eml,pwd,custId,is_active) values(:nm,:phm,:eml,:pwd,:custId,:is_active) ");   
            $stmt->bindValue(':nm', $_POST['Fname'] , PDO::PARAM_STR); 
            $stmt->bindValue(':phm',  $_POST['ph_no'] , PDO::PARAM_INT); 
            $stmt->bindValue(':eml', $_POST['email'] , PDO::PARAM_STR); 
            $stmt->bindValue(':pwd', $_POST['passwprd'] , PDO::PARAM_STR); 
            $stmt->bindValue(':custId', $CustId, PDO::PARAM_STR);  
            $stmt->bindValue(':is_active', 1, PDO::PARAM_STR);    
            $stmt->execute(); 
            $_SESSION['Regis'] = "Registration Done";
            header("Location: registration.php");
            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);      
                $stepCt['step1']['id'][0] = $Result[0]->cuurentId;  
                
                    
                $myTT +=count($stepCt['step1']['id']);  
                $stepCt['step2']['id'][] =  Refcall($Result[0]->cuurentId); 
                
            }*/
            
            $myTT = 0;
            if($stmt->rowCount()>0)
            { 
                $Result = $stmt->fetchAll(PDO::FETCH_OBJ);     
                foreach($Result as $dtm) 
                    $stepCt['step1']['id'][] = $dtm->cuurentId;
                    
                    
                $myTT +=count($stepCt['step1']['id']);       
                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)
                {
                       $myTT +=count($stepCt['step2']['id'][$i]);     
                       
                   foreach($stepCt['step2']['id'][$i] as $dtm)
                     $stepCt['step3']['id'][] =  Refcall($dtm->cuurentId);  
                }
                
            } 
             
             
             
            
            
            for($i=0;$i<count($stepCt['step3']['id']);$i++)
            {
                 
                if(count($stepCt['step3']['id'][$i])>0)
                {
                     $myTT +=count($stepCt['step3']['id'][$i]);     
                     
                   foreach($stepCt['step3']['id'][$i] as $dtm)
                     $stepCt['step4']['id'][] =  Refcall($dtm->cuurentId);  
                }
            } 
            
            
            for($i=0;$i<count($stepCt['step4']['id']);$i++)
            {
                 
                if(count($stepCt['step4']['id'][$i])>0)
                {
                    
                   $myTT +=count($stepCt['step4']['id'][$i]);   
                   
                   foreach($stepCt['step4']['id'][$i] as $dtm)
                     $stepCt['step5']['id'][] =  Refcall($dtm->cuurentId);  
                }
            } 
            
            
            for($i=0;$i<count($stepCt['step5']['id']);$i++)
            {
                 
                if(count($stepCt['step5']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step5']['id'][$i]);   
                      
                   foreach($stepCt['step5']['id'][$i] as $dtm)
                     $stepCt['step6']['id'][] =  Refcall($dtm->cuurentId);  
                }
            } 
            
            
            for($i=0;$i<count($stepCt['step6']['id']);$i++)
            {
                 
                if(count($stepCt['step6']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step6']['id'][$i]);   
                      
                   foreach($stepCt['step6']['id'][$i] as $dtm)
                     $stepCt['step7']['id'][] =  Refcall($dtm->cuurentId);  
                }
            } 
            
            for($i=0;$i<count($stepCt['step7']['id']);$i++)
            {
                 
                if(count($stepCt['step7']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step7']['id'][$i]);   
                      
                   foreach($stepCt['step7']['id'][$i] as $dtm)
                     $stepCt['step8']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }
            
            
            for($i=0;$i<count($stepCt['step8']['id']);$i++)
            {
                 
                if(count($stepCt['step8']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step8']['id'][$i]);   
                      
                   foreach($stepCt['step8']['id'][$i] as $dtm)
                     $stepCt['step9']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }
            
            
            for($i=0;$i<count($stepCt['step9']['id']);$i++)
            {
                 
                if(count($stepCt['step9']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step9']['id'][$i]);   
                      
                   foreach($stepCt['step9']['id'][$i] as $dtm)
                     $stepCt['step10']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }   
            
            for($i=0;$i<count($stepCt['step10']['id']);$i++)
            {
                 
                if(count($stepCt['step10']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step10']['id'][$i]);   
                      
                   foreach($stepCt['step10']['id'][$i] as $dtm)
                     $stepCt['step11']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }   
            
            
            for($i=0;$i<count($stepCt['step11']['id']);$i++)
            {
                 
                if(count($stepCt['step11']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step11']['id'][$i]);   
                      
                   foreach($stepCt['step11']['id'][$i] as $dtm)
                     $stepCt['step12']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }   
            
            
            for($i=0;$i<count($stepCt['step12']['id']);$i++)
            {
                 
                if(count($stepCt['step12']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step12']['id'][$i]);   
                      
                   foreach($stepCt['step12']['id'][$i] as $dtm)
                     $stepCt['step13']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }   
            
            for($i=0;$i<count($stepCt['step13']['id']);$i++)
            {
                 
                if(count($stepCt['step13']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step13']['id'][$i]);   
                      
                   foreach($stepCt['step13']['id'][$i] as $dtm)
                     $stepCt['step14']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }   
            
            for($i=0;$i<count($stepCt['step14']['id']);$i++)
            {
                 
                if(count($stepCt['step14']['id'][$i])>0)
                {
                    $myTT +=count($stepCt['step14']['id'][$i]);   
                      
                   foreach($stepCt['step14']['id'][$i] as $dtm)
                     $stepCt['step15']['id'][] =  Refcall($dtm->cuurentId);  
                }
            }   
             
include_once("../view/dashboard/downlineTeam.php");
?>