<?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'] : ''));
       $minitribe_id =  mysqli_real_escape_string($link, trim(isset($_POST['minitribe_id']) ? $_POST['minitribe_id'] : ''));
       
        
        
            
            
                   
                    
                    
                    
                
        $currentDateTime = date('Y-m-d H:i:s');
        
        
        
          $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 MiniTribeAssigenedID='$minitribe_id'  ORDER BY UserFirstName ASC");
                           $select_user_cnt = mysqli_num_rows($select_user);
                           
                      
                             $select_user_leader = mysqli_query($link, "SELECT * FROM `minitribe` WHERE MiniTribeID='$minitribe_id'");
                           $select_user_leader_cnt = mysqli_num_rows($select_user_leader);
                           $select_user_leader_cnt_row = mysqli_fetch_assoc($select_user_leader);
                           
                           if($select_user_leader_cnt >0)
                           {
                               
                                   $TribeLeaderID =  $select_user_leader_cnt_row['MiniTribeLeaderID'];
                               
                           }else
                           {
                                   $TribeLeaderID =  '';
                               
                           }
                          
                           
                            $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",
                                 "tribe_leaderid_id" => $TribeLeaderID,
                                 "responseBody" => $prosload_user_array
                             );
                           
                       
                             
                  
                   
              
                     
          }
          else
          {
              
             
                   $response = array(
                        "requestSuccessful" => true, // Assuming the request was successful
                        "responseMessage" => "failed",
                        "responseDescription" => "User not found",
                          "tribe_leaderid_id" => '',
                         "responseBody" => array(
                                'user_id' => '',
                                'user_fname' => '',
                                'user_lname'=> '',
                                'tribeassigened'=> '',
                                'photo'=> ''
                                                                                
                            )
                        
                      );
              
          }
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;

?>