<?php
  include("common/header2.php");
include("common/connection.php");
include("assets/sweetalert/alert.php");
 
    function random_strings($length_of_string) 

{ 



 

  $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 

 

  return substr(str_shuffle($str_result), 

          0, $length_of_string); 

} 
 
$userid=$_GET['id'];

 $idpin=random_strings(12);

 // $idpin   = mysqli_real_escape_string($db,$_POST['request_pin']);
  // $userid   = mysqli_real_escape_string($db,$_POST['userid']);
  //     $userid=substr($userid,2);
      
      
       $query ="SELECT * FROM tbl_signup WHERE sponsor_code = '" . $userid . "' and status='0'";
 $resultnew=mysqli_query($db,$query);  
  if(mysqli_num_rows($resultnew)>0)
  { 
 while($row = mysqli_fetch_assoc($resultnew)) { 
       
 $sqlpin="SELECT * from tbl_pin where pin='$idpin' and used_status='0' ";
$resultpin=mysqli_query($db,$sqlpin);
$countpin = mysqli_num_rows($resultpin);



      
     $sql = "update tbl_signup set status='1',active_date=CURDATE(),pinused='$idpin'  where sponsor_code='$userid'";
        

    
if ($db->query($sql) === TRUE) {
 
 if(function_exists('date_default_timezone_set')) {
date_default_timezone_set("Asia/Kolkata");
}
$date=date('Y-m-d');
$time=date('h:i a');
        
    $sql3 = "INSERT INTO tbl_active_user (userid,date,time)
        
VALUES ( '$userid','$date','$time')";
        
        
   

if ($db->query($sql3) === TRUE) {
 

}


   $success="Your Id Activated Successfully";  
   $sql2 = "update tbl_pin set used_by='$userid',used_date=CURDATE(),used_status='1'  where pin='$idpin'" ; 
   $db->query($sql2);



$pinfinal= $userid;


 for($i=1;$i<=4;$i++)
{
//get sponser code by user id
$mysponserid = getmysponserid($pinfinal);
//get user data code by user id
$sponserdetails =  getuserdatabysponserid($mysponserid);
if($pinfinal !== '1111')
{
echo  $spcode= $sponserdetails['sponsor_code'];
echo $spamont= $sponserdetails['wallet_amount'];
if($i=='1')
{
$transactionamount=1100*10/100;
echo $spamont= $spamont+$transactionamount;
 $messagenext='Referral Income Added of DC'.$userid;
}
else if($i=='2')
{
$transactionamount=1100*5/100;
  $spamont= $spamont+$transactionamount;
   $messagenext='MY LEVEL 2 Income Added of DC'.$userid;
}
else if ( $i=='3')
{
$transactionamount=1100*2/100;
  $spamont= $spamont+$transactionamount;
   $messagenext='MY LEVEL 3 Income Added of DC'.$userid;
}
else if ( $i=='4')
{
$transactionamount=1100*1/100;
   $spamont= $spamont+$transactionamount;
    $messagenext='MY LEVEL 4 Income Added of DC'.$userid;
}
updatedatabysponserid($spcode,$spamont);
$pinfinal =$spcode;
if(isset($transactionamount))
{
if($spcode == "" )
{ }
else {
   
 
insert_transction($spcode,$transactionamount,$messagenext,'CREDIT');
}
}
}

// $mobileno=$mobile;
// $messagenew ="Hello !!! Thank You For Join Global Vine :</br> User Name :-".$pin."</br> Password :".$password;
//  sendsms($mobileno, $messagenew); 
?>
 <!-- <meta http-equiv="refresh" content="0;url=index" /> -->
 
<?php

}

}

else {
    
echo "Error: " . $sql . "<br>" . $db->error;
}



}
}else
{
    
      $error1=1;
 $success="Sponsor ID Already Active OR Invalid Sponsor ID";  
  alertmessage($success);
    
}


?>
<script type="text/javascript">
  alert('Id Activated Successfully');
</script>
<meta http-equiv="refresh" content="0;url=adminindex.php" />

