<?php

        include('../../config/config.php');
        date_default_timezone_set('Africa/Lagos');
       
        
      include ('../messaging/whatsapp/index.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'] : ''));
        $UserType =  mysqli_real_escape_string($link, trim(isset($_POST['UserType']) ? $_POST['UserType'] : ''));
        $Tribe_ID =  mysqli_real_escape_string($link, trim(isset($_POST['Tribe_ID']) ? $_POST['Tribe_ID'] : ''));
        
       
        
      
        $currentDateTime = date('Y-m-d H:i:s');
        
        // $Tribe_ID
        
          $sql = ("SELECT * FROM `Users` WHERE UserID='$userID'");
          $result = mysqli_query($link, $sql);
          $row = mysqli_fetch_assoc($result);
          $row_cnt = mysqli_num_rows($result);
    
          if ($row_cnt > 0) 
          {
             
                   $UserType_new = trim($row['UserType']);
                   
                 
                    
                   
                           $select_user = mysqli_query($link, "  
                               SELECT * 
                            FROM `Users` 
                            WHERE 
                                DeleteStatus = '0' 
                                AND (TribeAssigenedID = 0 OR TribeAssigenedID = $Tribe_ID)
                                AND MiniTribeAssigenedID = '0'  
                            ORDER BY 
                                UserFirstName ASC");
                            $select_user_cnt = mysqli_num_rows($select_user);
                           
                            $prosload_user_array = [];
                           
                           if($select_user_cnt > 0)
                           {
                               
                               
                               
                              
                               
                               
                                 while($select_user_cnt_row = mysqli_fetch_assoc($select_user))
                                 {
                                     
                                     
                                       
                                        $UserIDnew =  $select_user_cnt_row['UserID'];
                                        $UserFirstName =  $select_user_cnt_row['UserFirstName'];
                                        $UserLastName =  $select_user_cnt_row['UserLastName'];
                                        $Photo =  $select_user_cnt_row['Photo'];
                                        $TribeAssigenedID =  $select_user_cnt_row['TribeAssigenedID'];
                                        
                                        
                                         $prosload_user_array[] = array(
                                                'user_id' => $UserIDnew,
                                                'user_fname' => $UserFirstName,
                                                'user_lname'=> $UserLastName,
                                                'tribeassigened'=> $TribeAssigenedID,
                                                'photo'=> $Photo
                                                
                                                                                        
                                            );
                                        
                                        
                                        
                                        
                                      
                                 }
                                 
                                 
                               
                           }else
                           {
                               
                               
                               
                               $prosload_user_array[] = array(
                                                'user_id' => '',
                                                'user_fname' => '',
                                                'user_lname'=> '',
                                                'tribeassigened'=> '',
                                                'photo'=> ''
                                                
                                                                                        
                                            );
                               
                           }
                           
                           
                            $response = array(
                                "requestSuccessful" => true, // Assuming the request was successful
                                "responseMessage" => "success",
                                "responseDescription" => "member's record found",
                                 "responseBody" => $prosload_user_array
                             );
                           
                       
                             
                  
                   
              
                     
          }
          else
          {
              
             
                   $response = array(
                        "requestSuccessful" => true, // Assuming the request was successful
                        "responseMessage" => "failed",
                        "responseDescription" => "User not found",
                         "responseBody" => array(
                                'user_id' => '',
                                'user_fname' => '',
                                'user_lname'=> '',
                                'tribeassigened'=> '',
                                'photo'=> ''
                                                                                
                            )
                        
                      );
              
          }
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>