<?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'] : ''));
        $event_id =  mysqli_real_escape_string($link, trim(isset($_POST['attendance_eventid']) ? $_POST['attendance_eventid'] : ''));
        
        // $event_date =  mysqli_real_escape_string($link, trim(isset($_POST['attendance_date']) ? $_POST['attendance_date'] : ''));
        
       
        
        $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']);
                   
                   
                //   if($UserType_new == 'pastor')
                //   {
                     
                           if($tribe_id == 'church')
                           {
                               
                                $select_user = mysqli_query($link, "SELECT * FROM `Users` WHERE `TribeAssigenedID`!='0' 
                                   AND DeleteStatus='0'  ORDER BY UserFirstName ASC");
                                   $select_user_cnt = mysqli_num_rows($select_user);
                                       
                           }else
                           {
                               
                                     $select_user = mysqli_query($link, "SELECT * FROM `Users` WHERE `TribeAssigenedID`!='0' 
                               AND DeleteStatus='0' AND `TribeAssigenedID`='$tribe_id' 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'];
                                        $UserType =  $select_user_cnt_row['UserType'];
                                        
                                        
                                            if($tribe_id == 'church')
                                            {
                                                
                                                          $pros_load_atten_event = mysqli_query($link,"SELECT * FROM `attendance` WHERE 
                                                          `TribeID`='$tribe_id' AND `EventID`='$event_id' AND `UserID`='$UserIDnew'");
                                                     
                                                     
                                                     
                                                           $newassigned = $tribe_id;
                                                
                                                
                                            }else
                                            {
                                                
                                                
                                                
                                                              $pros_load_atten_event = mysqli_query($link,"SELECT * FROM `attendance` WHERE 
                                                           `TribeID`='$TribeAssigenedID' AND `EventID`='$event_id' AND `UserID`='$UserIDnew'");
                                                           
                                                 $newassigned = $TribeAssigenedID;
                                            }
                              
                                        
                                      
                                        $pros_load_atten_event_cnt = mysqli_num_rows($pros_load_atten_event);
                                        
                                         $pros_load_atten_event_cnt_row = mysqli_fetch_assoc($pros_load_atten_event);
                                        
                                        
                                        
                                        
                                        
                                        if($pros_load_atten_event_cnt > 0)
                                        {
                                            
                                            
                                             $AttendanceStatus = $pros_load_atten_event_cnt_row['AttendanceStatus'];
                                            
                                              $check_count_title = $AttendanceStatus;  
                                            
                                        }else
                                        {
                                        
                                             $check_count_title = '';      
                                            
                                        }
                                       
                                       
                                       
                                        
                                         $prosload_user_array[] = array(
                                                'user_id' => $UserIDnew,
                                                'user_fname' => $UserFirstName,
                                                'user_lname'=> $UserLastName,
                                                'photo'=> $Photo,
                                                'tribe_assigned'=> $newassigned,
                                                'usertype'=> $UserType,
                                                'checked_option'=> $check_count_title,
                                                 'event_id'=> $event_id
                                                                              
                                            );
                                        
                                        
                                        
                                        
                                      
                                 }
                                 
                                 
                                  $response = array(
                                "requestSuccessful" => true, // Assuming the request was successful
                                "responseMessage" => "success",
                                "responseDescription" => "member's record found",
                                 "responseBody" => $prosload_user_array
                                );
                               
                           }else
                           {
                               
                               
                               
                               $prosload_user_array[] = array(
                                                'user_id' => '',
                                                'user_fname' => '',
                                                'user_lname'=> '',
                                                'photo'=> '',
                                                'tribe_assigned'=> '',
                                                'usertype'=> '',
                                                'checked_option'=> '',
                                                  'event_id'=> ''
                                                
                                                                                        
                                            );
                                            
                                            
                                            
                                             $response = array(
                                        "requestSuccessful" => true, // Assuming the request was successful
                                        "responseMessage" => "failed",
                                        "responseDescription" => "member's record not 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'=> '',
                                'photo'=> '',
                                'tribe_assigned'=> '',
                                'usertype'=> ''
                                                                                
                            )
                        
                      );
              
          }
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>