<?php

            include('../../config/config.php');
            
            
            
               // Set CORS headers to allow requests from any origin (*)
        header("Access-Control-Allow-Origin: *");
        
        // Allow specific HTTP methods
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
        
        // Allow specific HTTP headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        
        // Allow cookies to be included in the request (if needed)
        header("Access-Control-Allow-Credentials:true");
        
        // Retrieve the raw POST data
        
        
        
      
        $UserID =  mysqli_real_escape_string($link, trim(isset($_POST['UserID']) ? $_POST['UserID'] : ''));
        $UsertType = mysqli_real_escape_string($link,trim(isset($_POST['UserType']) ? $_POST['UserType'] : ''));
       
      $sql = ("SELECT * FROM `Users` WHERE `UserID`='$UserID' AND `UserType`='$UsertType'");
      $result = mysqli_query($link, $sql);
      $row = mysqli_fetch_assoc($result);
      $row_cnt = mysqli_num_rows($result);

      if ($row_cnt > 0) 
      {
          
          
          
               $UserFirstName = $row['UserFirstName'];
    		   $UserMiddleName = $row['UserMiddleName'];
    		   $UserLastName = $row['UserLastName'];
    		   $Email = $row['Email'];
    		   $Phone = $row['Phone'];
	           $UserTypenew = $row['UserType'];
               $Photo = $row['Photo'];
               
               
               
               $check_tribe_leader = mysqli_query($link, "SELECT * FROM `tribe` WHERE `TribeLeaderID`='$UserID'");
		       $check_tribe_leader_cnt = mysqli_num_rows($check_tribe_leader);
		       
		       
		       if($UserTypenew == 'pastor')
		       {
		           
		           $final_usertpe = $UserTypenew;
		           
		       }else
		       {
		           
    		           if($check_tribe_leader_cnt > 0)
        		       {
        		           
        		                $final_usertpe = 'Tribe Leader';
        		           
        		       }else
        		       {
        		           
        		           
        		           
        		           $pros_get_mini_tribe_leader = mysqli_query($link, "SELECT * FROM `minitribe` WHERE MiniTribeLeaderID='$UserID'");
        		           $pros_get_mini_tribe_leader_cnt = mysqli_num_rows($pros_get_mini_tribe_leader);
        		           
        		           
            		           if($pros_get_mini_tribe_leader_cnt > 0)
            		           {
            		               
            		                  $final_usertpe = 'Mini Tribe Leader';
            		               
            		           }else
            		           {
            		               
            		                  $final_usertpe = 'Member';
            		           }
            		           
        		           
        		           
        		       }
    		           
		        }
		       
		       
		   
		   
    		   $response = array(
                    "requestSuccessful" => true, // Assuming the request was successful
                    "responseMessage" => "success",
                    "responseDescription" => "User record found",
                    "fname" => $UserFirstName,
                    "mname" => $UserMiddleName,
                    "lname" => $UserLastName,
                    "email" => $Email,
                    "phone" => $Phone,
                    "usertype" => $final_usertpe,
                    "photo" => $Photo
                     
                ); 
                

         

      }
      else
      {
          
                   $response = array(
                    "requestSuccessful" => true, // Assuming the request was successful
                    "responseMessage" => "failed",
                    "responseDescription" => "User record not found",
                    "fname" => 'NULL',
                    "mname" => 'NULL',
                    "lname" => 'NULL',
                    "email" => 'NULL',
                    "phone" => 'NULL',
                    "usertype" => 'NULL',
                    "photo" => 'NULL'
                     
                  ); 
          
				
      }
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
            
            
            
        
        



?>