<?php 
@session_start();
include "control/activity.php";
$dd = new activity();

$reqKey = $_REQUEST['key'];
$table = "funds_request";
$condition = " where funds_key='$reqKey' AND status='pending'";
$checkStatus = $dd->check($table,$condition);
if($checkStatus['status'] == "success")
{
    $userKey = $checkStatus['data']['userKey'];
    if($checkStatus['data']['request_type'] == "USDT")
    {
       $amm = $checkStatus['data']['amount'];
    }
    else{
        $amm = $checkStatus['data']['amount']/90;
    }
  


    $table = "userdetails";
    $value = "investment=investment+$amm";
    $condition = " user_key='$userKey'";
    $result = $dd->updateData($table,$value,$condition);
    if($result)
    {
        $txnNumber = date("h:i d/m/Y");
        $date = date("h:i d/m/Y");
        $table = "account_details";
        $value = "(`username_key`, `action_date`, `txn_number`, `amount`, `avl_balance`, `status`, `remark`) 
        SELECT '$userKey','$date','$txnNumber','$amm',investment,'credit','Transfer to investment wallet' FROM userdetails WHERE user_key='$userKey'";
        $result = $dd->insertData($table,$value);
        if($result)
        {
            $table = "funds_request";
            $value = "status='Approved'";
            $condition = " funds_key='$reqKey'";
            $resultUpdate = $dd->updateData($table,$value,$condition);
            if($resultUpdate)
            {
                $table = "userdetails";
                $condition = " where user_key='$userKey'";
                $userStatus = $dd->check($table,$condition);
                if($userStatus['status'] == "success")
                {
                  if($userStatus['data']['status'] == "INAVTIVE" AND $userStatus['data']['investment'] >= 12)
                  {
                    goActivation($dd,$userKey,$amm);
                  }
                  else{
                   echo "<script>alert('Transfer successfully..!!'),window.open('request.php','_self')</script>";
                  }
                }
            }
        }
    }
}
else{
    echo "<script>alert('Sorry Internal error..!!'),window.open('request.php','_self')</script>";
}





function goActivation($dd,$userKey,$amm)
{
    $table = "userdetails";
    $value = "status='ACTIVE'";
    $condition = " user_key='$userKey'";
    $result = $dd->updateData($table,$value,$condition);
    if($result){
        $level = 1;
        distributoLevels($dd,$userKey,$level);
    }
}





function distributoLevels($dd,$userKey,$level)
{
   if($level <= 5){

    if($level == 1)
    {
        $paid = "1.25";
    }

    if($level == 2)
    {
        $paid = "1";
    }

    if($level == 3)
    {
        $paid = "0.75";
    }


    if($level == 4)
    {
        $paid = "0.6";
    }


    if($level == 5)
    {
        $paid = "1.5";
    }
    
      
    $levelColom = "level_$level";
    $table = "userdetails";
    $condition = " where user_key='$userKey'"; 
    $result = $dd->check($table,$condition);
    if($result['status'] == "success")
    {
       $lvlColm = $result['data'][$levelColom];
       $value = "main_wallet=main_wallet+$paid,team_income=team_income+$paid";
       $condition = " username='$lvlColm'";
       $result2 = $dd->updateData($table,$value,$condition);
       if($result2){
            $txnNumber = date("h:i d/m/Y");
            $date = date("h:i d/m/Y");
            $table = "account_details";
            $value = "(`username_key`, `action_date`, `txn_number`, `amount`, `avl_balance`, `status`, `remark`) 
            SELECT '$userKey','$date','$txnNumber','$paid',main_wallet,'credit','Transfer to main_wallet wallet' FROM userdetails WHERE username='$lvlColm'";
            $result = $dd->insertData($table,$value);
       }
    }
    $level = $level+1;
    distributoLevels($dd,$userKey,$level);
   }
   else{
    echo "<script>alert('Transfer successfully..!!'),window.open('request.php','_self')</script>";
   }
   
}
?>