<?php

 require 'config/conn.php';
   header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json");
    header("Accept: application/json");


   
    
    $uId = $_POST['uId'];
    $levelId;

    // $response = "hi";

    $getUserData = "SELECT * FROM `users` WHERE `uId` = '$uId'";
    $fetchUserData = mysqli_query($conn,$getUserData);

    if($fetchUserData)
    {
        while($row=$fetchUserData->fetch_assoc())
        {
            $response['id']= $row['id'];
            $response['uId'] = $row['uId'];
            $response['name'] = $row['name'];
            $response['email'] = $row['email'];
            $response['avatar']= $row['avatar'];
            $response['coins'] = $row['coins'];
            $response['totalCoins'] = $row['totalCoins'];
            $response['levelId'] = $row['levelId'];
            $response['quizPlayed'] = $row['quizPlayed'];
            $response['inviteCode'] = $row['inviteCode'];
            $response['invitedBy'] = $row['invitedBy'];
            $response['accountCreated'] = $row['accountCreated'];
            $levelId = $row['levelId'];

            $getUserLevel= "SELECT `id`,`name`, `icon` FROM `earninglevels` WHERE `id` = '$levelId'";
            $fetchLevel = mysqli_query($conn,$getUserLevel);

            if($fetchLevel)
            {
                $response['statusCode'] = 0;
                $response['message'] = "Successful";

                while($row=$fetchLevel->fetch_assoc())
                {
                    $response['userLevel'] = $row['name'];
                    $response['userLevelIcon'] = $row['icon'];

                    if($levelId == $row['id'])
                    {
                        $response['isLevelChanged'] = 1;
                    }
                    else
                    {
                        $response['isLevelChanged'] = 1; 
                    }
                }
            }
            else
            {
                $response['statusCode'] = 4;
                $response['message'] = "Error in fetching user level";
            }

        }


    }
    else
    {
        $response['statusCode'] = 4;
        $response['message'] = "Error in fetching user data";
    }
    
    
 


    global $response;
    echo json_encode($response);



?>