<?php

include 'includes/conn.php'; 
$currentDates = date('Y-m-d');


$select_query = "SELECT * FROM users WHERE status='Active' and rank='mfs_member'";

$result = $conn->query($select_query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){

    $level_bonus_date = $row['level_bonus_date'];
    $name=$row['name'];
    $mfs_royality=$row['mfs_royality'];
    $got_level_bonus=$row['got_level_bonus'];

    if ($level_bonus_date == $currentDates) {
        echo '<script type="text/javascript">
            alert("Already Added!!!");
            window.location.href = "home.php";
        </script>';
       // $m="Already Added";
        //header("location: adminindex.php?error=".$m);
    }

         else {
             $count_level_one_refer="SELECT COUNT(*) FROM `users` WHERE level_one_parent='$name' AND status='Active'";
             
                $result = $conn->query($count_level_one_refer);
                $row = $result->fetch_assoc();
                $count= $row['COUNT(*)'];
           
            
                 

                $user_update = "UPDATE users SET mfs_royality = mfs_royality + ($count*0.25), level_bonus_date='$currentDates' WHERE name='$name'";
            if (mysqli_query($conn, $user_update)) {
                 echo '<script type="text/javascript">
            alert("Royality Bonus Added!!!");
            window.location.href = "home.php";
        </script>';
                 
           
                
            }
            

           
            
        }

    }
}
else{
      echo '<script type="text/javascript">
            alert("No one reach this level!!!");
            window.location.href = "home.php";
        </script>';
    
}
 
    



?>