<?php
include 'include/db.php';

//  fetch user unique id instead of login id
$fetch_id="select unique_user_id, refferance_id, transaction_pwd from registration where login_session='".$_SESSION['login_id']."' ";
    $fetch_con=mysqli_query($conn,$fetch_id);
    if(mysqli_num_rows($fetch_con)>0)
    {
        while($ro=mysqli_fetch_array($fetch_con))
        {
            $got_unique_id=$ro['unique_user_id'];
			$refferance_id = $ro['refferance_id'];
			$transaction_pwd = $ro['transaction_pwd'];
        }
    }

//  fetch total balance,bv,member,register members
$qr="select * from wallet_balance where balance_user_id='$got_unique_id' ";
$connection=mysqli_query($conn,$qr);
if(mysqli_num_rows($connection)>0)
{
    while($rw=mysqli_fetch_array($connection))
    {
        $total_balance=$rw['total_balance'];
        $total_bv=$rw['bv_balance'];
    }
}


function fetch_left($got_unique_id,$position){
    $ab=array();
    $ab2=array();
    include 'include/db.php';
    $qr2="select * from tree_data where position_id='$got_unique_id' ";
    $connection2=mysqli_query($conn,$qr2);
    if(mysqli_num_rows($connection2)>0)
    {
        while($rw2=mysqli_fetch_array($connection2))
        {
            if($position=='left')
            {
                if(!EMPTY($rw2['position_left']))
                {
                  $left=$rw2['position_left'];
                  $ab=fetch_left($left,$position);
                  $ab[]=$rw2['position_left'];
                  return $ab;
                }
            }
            if($position=='right')
            {
                if(!EMPTY($rw2['position_right']))
                {
                  $right=$rw2['position_right'];
                  $ab2=fetch_left($right,$position);
                  $ab2[]=$rw2['position_right'];
                  return $ab2;
                }
            }
        }
    }
} 
function fetch_left_right($left_arr_id,$position){
    $nxt_arr=array();
    include 'include/db.php';
    
    for($i=0;$i<sizeof($left_arr_id);$i++)
    {
        $qr5="select * from tree_data where position_id='".$left_arr_id[$i]."' ";
        $connection5=mysqli_query($conn,$qr5);
        if(mysqli_num_rows($connection5)>0)
        {
            while($rw5=mysqli_fetch_array($connection5))
            {
                if($position=='right')
                {
                    if(!EMPTY($rw5['position_right']))
                    {
                       $right=$rw5['position_right'];
                       array_push($nxt_arr,$right);
                    }
                }
                if($position=='left')
                {
                    if(!EMPTY($rw5['position_left']))
                    {
                       $right=$rw5['position_left'];
                       array_push($nxt_arr,$right);
                    }
                }
            }
        }
    }
    return $nxt_arr;
}

//   count total active users
function users_info($all_id_array){
    $all_active_members=array();
    include 'include/db.php';
    for($i=0;$i<sizeof($all_id_array);$i++)
    {
        $query="select * from registration where unique_user_id='$all_id_array[$i]' AND active_deactive='1'  ";
        $connection=mysqli_query($conn,$query);
        if(mysqli_num_rows($connection)>0)
        {
            while($rw=mysqli_fetch_array($connection))
            {
                $all_active_members[]=$rw['unique_user_id'];
            }
        }
    }
    return $all_active_members;
}

// fetch all member
function all_members(){
    include 'include/db.php';
    $all_members=array();
    $slt_member="select unique_user_id from registration";
    $connection=mysqli_query($conn,$slt_member);
    if(mysqli_num_rows($connection))
    {
        while($rows=mysqli_fetch_array($connection))
        {
            $all_members[]=$rows['unique_user_id'];
        }
    }
     return $all_members;
}
//  get admin total balance
function get_admin_total_balance(){
    include 'include/db.php';
    $fetch_amount="select * from wallet_balance where balance_user_id='B00001'";
    $connection_amount=mysqli_query($conn,$fetch_amount);
    if(mysqli_num_rows($connection_amount)>0)
    {
        while($rows=mysqli_fetch_array($connection_amount))  
        {
            return $balance=$rows['total_balance'];
        }
    }
    
}

function get_admin_total_balance2(){
    include 'include/db.php';
    $fetch_amount="select * from wallet_balance where balance_user_id='B00001'";
    $connection_amount=mysqli_query($conn,$fetch_amount);
    if(mysqli_num_rows($connection_amount)>0)
    {
        while($rows=mysqli_fetch_array($connection_amount))  
        {
            return $balance=$rows['total_balance'];
        }
    }
    
}

// fetch total left member of root id
$val=fetch_left($got_unique_id,'left');  // left
if(isset($val))
{
    $reverse=array_reverse($val);
}
else
{
    $reverse=[];
}
$val12=fetch_left($got_unique_id,'right');  // right
if(isset($val12))
{
    $reverse12=array_reverse($val12);
}
else
{
    $reverse12=[];
}
$val2=fetch_left_right($reverse,'right');  //  total left right
$a=sizeof($reverse);
$b=sizeof($val2);
$total_right_user_join=$a+$b;

$val21=fetch_left_right($reverse12,'left');  //  total right left
$a1=sizeof($reverse12);
$b1=sizeof($val21);
$a1+$b1;
$total_left_user_join=$a1+$b1;
  //  if variable are not an array then define it as an array
if(!is_array($reverse))
{
    $reverse=[];
}
if(!is_array($val2))
{
    $val2=[];
}
if(!is_array($reverse12))
{
    $reverse12=[];
}
if(!is_array($val21))
{
    $val21=[];
}
$get_all_members=array_merge($reverse,$val2,$reverse12,$val21);  //  merge all array and got all members id

$rtn=users_info($get_all_members);
$member_total=$total_left_user_join+$total_right_user_join;
$total_active_member_id=$rtn;
$total_num_active_members=sizeof($total_active_member_id);
$total_deactive_member_id=array_diff($get_all_members,$total_active_member_id);
$total_num_deactive_members=sizeof($total_deactive_member_id);
$all_members=all_members();


?>