<?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
        
        
         $Tribe_ID =  mysqli_real_escape_string($link, trim(isset($_POST['Tribe_ID']) ? $_POST['Tribe_ID'] : ''));
         
         
         $pros_get_tribe = mysqli_query($link,"SELECT t.`TribeID`, t.`TribeLeaderID`, t.`TribeTitle`, t.`TribeLocation`,  t.`TribeDes`, t.`TribeImage`, t.`DeleteStatus`, COUNT(u.`UserID`) AS num_users
                    FROM `tribe` t
                    LEFT JOIN `Users` u ON t.`TribeID` = u.`TribeAssigenedID` AND u.`DeleteStatus` = 0
                    WHERE t.`DeleteStatus` = 0 AND t.`TribeID`='$Tribe_ID'
                    GROUP BY t.`TribeID`
                    ORDER BY num_users DESC");
                   
                    $pros_get_tribe_cnt = mysqli_num_rows($pros_get_tribe);
                    $pros_get_tribe_cnt_row = mysqli_fetch_assoc($pros_get_tribe);
                                  
              if($pros_get_tribe_cnt > 0)
              {
                  
                  
                  
                 
                          $TribeID =  $pros_get_tribe_cnt_row['TribeID'];
                          $TribeLeaderID =  $pros_get_tribe_cnt_row['TribeLeaderID'];
                          $TribeTitle =  $pros_get_tribe_cnt_row['TribeTitle'];
                          $TribeDes =  $pros_get_tribe_cnt_row['TribeDes'];
                          $TribeImage =  $pros_get_tribe_cnt_row['TribeImage'];
                          $num_users =  $pros_get_tribe_cnt_row['num_users'];
                         $TribeLocation =  $pros_get_tribe_cnt_row['TribeLocation'];
                         
                         
                         $pros_load_total_num_users_mini_tribe = mysqli_query($link, "SELECT * FROM `minitribe` 
                         WHERE `TribeID`='$TribeID'");
                         $pros_load_total_num_users_mini_tribe_cnt = mysqli_num_rows($pros_load_total_num_users_mini_tribe);
                          
                          
                         $pros_get_leaders = mysqli_query($link, "SELECT * FROM `Users` WHERE  `UserID`='$TribeLeaderID'");
                         $pros_get_leaders_cnt = mysqli_num_rows($pros_get_leaders);
                         $pros_get_leaders_cnt_row = mysqli_fetch_assoc($pros_get_leaders);
                         
                         
                         
                         
                         
                        
                             if($pros_get_leaders_cnt > 0)
                             {
                                  
                                 
                                     $UserFirstName = $pros_get_leaders_cnt_row['UserFirstName'];
                                     $UserLastName = $pros_get_leaders_cnt_row['UserLastName'];
                                     
                                     $Full_Name = $UserFirstName.' '.$UserLastName;
    
                             }else
                             {
                                      $Full_Name = '';
                                 
                             }
                             
                             
                             
                           
                             
                             
                
                            
                            
                          
                             
                        $pros_get_tribe_details_mini = mysqli_query($link, "SELECT t.`TribeID`, t.`MiniTribeID`, t.`MiniTribeLeaderID`, t.`MiniTribeTitle`, t.`MiniTribeDes`, t.`MiniTribeLocation`, t.`MiniTribeImage`, t.`DeleteSatus`, COUNT(u.`UserID`) AS num_users FROM `minitribe` t LEFT JOIN `Users` u ON t.`MiniTribeID` = u.`MiniTribeAssigenedID` AND u.`DeleteStatus` = 0 WHERE t.`DeleteSatus` = 0 AND t.`TribeID`='$TribeID' GROUP BY t.`MiniTribeID` ORDER BY num_users DESC;");
                          
                        $pros_get_tribe_details_mini_cnt = mysqli_num_rows($pros_get_tribe_details_mini);
                             
                                  $prosload_mini_tribe_array = [];
                              
                                  if($pros_get_tribe_details_mini_cnt > 0)
                                  {
                                      
                                      
                                      
                                      
                                          while( $pros_get_tribe_details_mini_cnt_row = mysqli_fetch_assoc($pros_get_tribe_details_mini))
                                          {
                                              
                                          
                                             
                                             $MiniTribeID =  $pros_get_tribe_details_mini_cnt_row['MiniTribeID'];
                                             $MiniTribeTitle =  $pros_get_tribe_details_mini_cnt_row['MiniTribeTitle'];
                                             $MiniTribeLocation =  $pros_get_tribe_details_mini_cnt_row['MiniTribeLocation'];
                                             $MiniTribeImage =  $pros_get_tribe_details_mini_cnt_row['MiniTribeImage'];
                                             $num_users_mini =  $pros_get_tribe_details_mini_cnt_row['num_users'];
                                             
                                             $MiniTribeLeaderID =  $pros_get_tribe_details_mini_cnt_row['MiniTribeLeaderID'];
                                             
                                             
                                             
                                             
                                             
                                             
                                              $pros_get_mini_tribe_leader = mysqli_query($link, "SELECT * FROM `Users` WHERE `UserID`='$MiniTribeLeaderID'");
                                              $pros_get_mini_tribe_leader_cnt = mysqli_num_rows($pros_get_mini_tribe_leader);
                                              $pros_get_mini_tribe_leader_cnt_row = mysqli_fetch_assoc($pros_get_mini_tribe_leader);
                                              
                                              
                                                
                                              
                                               $UserFirstName_mini_leader_fname =  $pros_get_mini_tribe_leader_cnt_row['UserFirstName'];
                                               $UserMiddleName_mini_leader_mname =  $pros_get_mini_tribe_leader_cnt_row['UserMiddleName'];
                                               $UserLastName_mini_leader_lname =  $pros_get_mini_tribe_leader_cnt_row['UserLastName'];
                                               
                                               
                                               $mini_tribe_leader_name_full = $UserFirstName_mini_leader_fname.' '.$UserLastName_mini_leader_lname;
                                               
                                               
                                               
                                               
                                           // GET TRIBE MEETING DAYS MINI
                                    
                                    
                                            $pros_get_tribe_meeting_days_mini = mysqli_query($link, "SELECT * FROM `tribemeetingdays` WHERE `TribeID`='$MiniTribeID' AND `TribeType`='mini'");
                                            $pros_get_tribe_meeting_days_cnt_mini = mysqli_num_rows($pros_get_tribe_meeting_days_mini);
                                            
                                            
                                            
                                            $meetday_array_mini = [];
                                            
                                            
                                            
                                            
                                            if($pros_get_tribe_meeting_days_cnt_mini > 0)
                                            {
                                                
                                                
                                             
                                                    while($pros_get_tribe_meeting_days_cnt_row_mini = mysqli_fetch_assoc($pros_get_tribe_meeting_days_mini))
                                                    {
                                                     
                                                        $Meeting_Day_mini =  $pros_get_tribe_meeting_days_cnt_row_mini['Day'];
                                                        $StartDuration_mini =  $pros_get_tribe_meeting_days_cnt_row_mini['StartDuration'];
                                                        $EndDuration_mini =  $pros_get_tribe_meeting_days_cnt_row_mini['EndDuration'];
                                                        $TribeMeetingID_mini =  $pros_get_tribe_meeting_days_cnt_row_mini['TribeMeetingID'];
                                                        
                                                        
                                                        
                                                        
                                                         $meetday_array_mini[] = array(
                                                                'meting_days_mini' => $Meeting_Day_mini,
                                                                'meeting_start_duration_mini' => $StartDuration_mini,
                                                                'meeting_end_duration_mini'=> $EndDuration_mini,
                                                                'meeting_id_mini'=> $TribeMeetingID_mini
                                                                     
                                                            );
                                           
                                                        
                                                        
                                                    }
                                                  
                                                
                                            }else
                                            {
                                               
                                               
                                               
                                                      $meetday_array_mini[] = array(
                                                        'meting_days_mini' => '',
                                                        'meeting_start_duration_mini' => '',
                                                        'meeting_end_duration_mini'=> '',
                                                        'meeting_id_mini'=> ''
                                                             
                                                    );
                                                    
                                               
                                            }
                                          
                                          
                                     // GET TRIBE MEETING DAYS MINI
                                             
                                             
                                             
                                             
                                      //GET TRIBE MEMBERS MINI    
                                          
                                          $pros_load_mini_user_user = mysqli_query($link, "SELECT * FROM `Users` WHERE `MiniTribeAssigenedID`='$MiniTribeID'");
                                           $pros_load_mini_user_user_cnt = mysqli_num_rows($pros_load_mini_user_user);
                                          
                                          
                                           $mini_member_array = [];
                                  
                                  
                                  
                                          if($pros_load_mini_user_user_cnt > 0)
                                          {
                                              
                                              
                                              
                                              
                                               while( $pros_load_mini_user_user_cnt_row = mysqli_fetch_assoc($pros_load_mini_user_user))
                                               {
                                                   
                                                   
                                                   
                                                   
                                                    
                                                   
                                                       $UserMiddleName_mini = $pros_load_mini_user_user_cnt_row['UserMiddleName'];
                                                       $UserLastName_mini = $pros_load_mini_user_user_cnt_row['UserLastName'];
                                                        $UserFirstName_mini = $pros_load_mini_user_user_cnt_row['UserFirstName'];
                                                       
                                                       $Photo_mini = $pros_load_mini_user_user_cnt_row['Photo'];
                                                       $UserID_mini = $pros_load_mini_user_user_cnt_row['UserID'];
                                                       
                                                       
                                                        $select_check_rferrcnt_mini = mysqli_query($link, "SELECT * FROM `Users` WHERE `RefereeNameOrSource`='$UserID_mini'");
                                                        $select_check_rferrcnt_row_mini = mysqli_num_rows($select_check_rferrcnt_mini);
                                                       
                                                       
                                                        $mini_member_array[] = array(
                                                            'user_mini_member_fname' => $UserFirstName_mini,
                                                            'user_mini_member_mname' => $UserMiddleName_mini,
                                                            'user_mini_member_lname'=> $UserLastName_mini,
                                                            'user_mini_member_photo'=> $Photo_mini,
                                                            'user_mini_member_user_id'=> $UserID_mini,
                                                            'number_ofinvites'=> number_format($select_check_rferrcnt_row_mini)
        
                                                        ); 
                                                       
                                                   
                                                   
                                                   
                                                    
                                                   
                                               }
                                               
                                               
                                               
                                              
                                          }else
                                          {
                                            
                                               $mini_member_array[] = array(
                                                            'user_mini_member_fname' => '',
                                                            'user_mini_member_mname' => '',
                                                            'user_mini_member_lname'=> '',
                                                            'user_mini_member_photo'=> '',
                                                            'user_mini_member_user_id'=> '',
                                                             'number_ofinvites'=> '0'
        
                                                        ); 
                                              
                                          }
                                          
                                         //GET TRIBE MEMBERS MINI    
                                             
                                             
                                             
                                             
                                             
                                             
                                              $prosload_mini_tribe_array[] = array(
                                                    'minitribe_id' => $MiniTribeID,
                                                    'minitribe_title' => $MiniTribeTitle,
                                                    'minitribe_location'=> $MiniTribeLocation,
                                                    'minitribe_image'=> $MiniTribeImage,
                                                    'num_min_users'=> $num_users_mini,
                                                    'mini_tribe_leader_fullname' => $mini_tribe_leader_name_full,
                                                    'mini_tribe_full_member_content'=> $mini_member_array,
                                                    'mini_tribe_full_meetday_content'=> $meetday_array_mini
                                                    
                                                    

                                                ); 
                                                
                                         
                                             
                                              
                                              
                                          }
                                      
                                  }else
                                  {
                                      
                                      
                                      
                                      $prosload_mini_tribe_array[] = array(
                                            'minitribe_id' => '',
                                            'minitribe_title' => '',
                                            'minitribe_location' => '',
                                            'minitribe_image' => '',
                                            'num_min_users' => '',
                                            'mini_tribe_leader_fullname' => '',
                                            'mini_tribe_full_member_content' => array(
                                                'user_mini_member_fname' => '',
                                                'user_mini_member_mname' => '',
                                                'user_mini_member_lname' => '',
                                                'user_mini_member_photo' => '',
                                                'user_mini_member_user_id' => ''
                                            ),
                                            'mini_tribe_full_meetday_content' => array(
                                                'meting_days_mini' => '',
                                                'meeting_start_duration_mini' => '',
                                                'meeting_end_duration_mini' => '',
                                                'meeting_id_mini' => ''
                                            )
                                        );
                                        
                                      
                                  }
                                  
                                  
                                //   print_r($prosload_mini_tribe_array);
                                 
                             
                             
                                      // GET TRIBE MEETING DAYS
                                    
                                    
                                            $pros_get_tribe_meeting_days = mysqli_query($link, "SELECT * FROM `tribemeetingdays` WHERE `TribeID`='$TribeID' AND `TribeType`='main'");
                                            $pros_get_tribe_meeting_days_cnt = mysqli_num_rows($pros_get_tribe_meeting_days);
                                            
                                            
                                            
                                            $meetday_array = [];
                                            $members_array = [];
                                            
                                            
                                            
                                            if($pros_get_tribe_meeting_days_cnt > 0)
                                            {
                                                
                                                
                                             
                                                    while($pros_get_tribe_meeting_days_cnt_row = mysqli_fetch_assoc($pros_get_tribe_meeting_days))
                                                    {
                                                     
                                                        $Meeting_Day =  $pros_get_tribe_meeting_days_cnt_row['Day'];
                                                        $StartDuration =  $pros_get_tribe_meeting_days_cnt_row['StartDuration'];
                                                        $EndDuration =  $pros_get_tribe_meeting_days_cnt_row['EndDuration'];
                                                        $TribeMeetingID =  $pros_get_tribe_meeting_days_cnt_row['TribeMeetingID'];
                                                        
                                                        
                                                        
                                                        
                                                         $meetday_array[] = array(
                                                                'meting_days' => $Meeting_Day,
                                                                'meeting_start_duration' => $StartDuration,
                                                                'meeting_end_duration'=> $EndDuration,
                                                                'meeting_id'=> $TribeMeetingID
                                                                     
                                                            );
                                           
                                                        
                                                        
                                                    }
                                                  
                                                
                                            }else
                                            {
                                               
                                               
                                               
                                               $meetday_array[] = array(
                                                        'meting_days' => '',
                                                        'meeting_start_duration' => '',
                                                        'meeting_end_duration'=> '',
                                                        'meeting_id'=> ''
                                                             
                                                    ); 
                                               
                                            }
                                          
                                          
                                     // GET TRIBE MEETING DAYS
                                     
                                     
                                     
                                     // GET TRIBE MEMBERS
                                     
                                          $get_members = mysqli_query($link, "SELECT * FROM `Users` WHERE `TribeAssigenedID`='$TribeID'");
                                          $get_members_cnt = mysqli_num_rows($get_members);
                                          
                                          if($get_members_cnt > 0)
                                          {
                                              
                                              
                                                    while($get_members_cnt_row = mysqli_fetch_assoc($get_members))
                                                    {
                                                        
                                                    
                                                          $UserFirstName = $get_members_cnt_row['UserFirstName'];
                                                          $UserLastName = $get_members_cnt_row['UserLastName'];
                                                          $UserMiddleName = $get_members_cnt_row['UserMiddleName'];
                                                          $Photo = $get_members_cnt_row['Photo'];
                                                          $NumberofInvite = $get_members_cnt_row['NumberofInvite'];
                                                          
                                                          $TribeAssigenedID = $get_members_cnt_row['TribeAssigenedID'];
                                                          $UserIDNEW = $get_members_cnt_row['UserID'];
                                                          
                                                         $DateJoin=  $get_members_cnt_row['DateJoin'];
                                                          
                                                         
                                                            $select_check_rferrcnt = mysqli_query($link, "SELECT * FROM `Users` WHERE `RefereeNameOrSource`='$UserIDNEW'");
                                                             $select_check_rferrcnt_row = mysqli_num_rows($select_check_rferrcnt);
                                                          
                                                          
                                                          $DateJoinDateOnly = substr($DateJoin, 0, 10);
                                        
                                                        $count_num_event = mysqli_query($link,"SELECT DISTINCT(`event`.EventID) FROM `event` INNER JOIN
                                                        `attendance` ON  `event`.`EventID` =  `attendance`.`EventID` 
                                                        WHERE `event`.TribeID IN('church', '$TribeAssigenedID') AND `event`.`EventDate` >= '$DateJoinDateOnly'");
                                                      
                                                       $count_num_event_rows = mysqli_num_rows($count_num_event);
                                                    //   
                                                      
                                                      $select_attendance_count = mysqli_query($link, "SELECT * FROM `attendance` INNER JOIN `event` ON `attendance`.`EventID` = `event`.`EventID` WHERE `attendance`.TribeID IN('church', '$TribeAssigenedID')
                                                      
                                                         AND `attendance`.`AttendanceStatus`='present' AND `attendance`.UserID='$UserIDNEW' AND `event`.EventDate >= '$DateJoinDateOnly'");
                                                                 
                                                       $select_attendance_count_row = mysqli_num_rows($select_attendance_count);
                                                         
                                                         
                                                         if($count_num_event_rows == 0)
                                                         {
                                                              $percentageAttendance =0;
                                                         }else
                                                         {
                                                              $percentageAttendance = ($select_attendance_count_row / $count_num_event_rows) * 100;
                                                         }
                                                         
                                                        // echo $percentageAttendance;

                                                          
                                                          
                                                               $members_array[] = array(
                                                                    'meember_fname' => $UserFirstName,
                                                                    'meember_mname' => $UserMiddleName,
                                                                    'meember_lname' => $UserLastName,
                                                                    'member_photo'=> $Photo,
                                                                    'numinvites'=> number_format($select_check_rferrcnt_row),
                                                                     'attendance_per'=> $percentageAttendance,
                                                                      'member_id' => $UserIDNEW
                                                                    
                                                                    
                                                                    
                                                                    
                                                                ); 
                                                          
                                                          
                                                             
                                                        
                                                    }
                                                
                                              
                                          }else
                                          {
                                              
                                              
                                               $members_array[] = array(
                                                            'meember_fname' => '',
                                                            'meember_mname' => '',
                                                            'meember_lname' => '',
                                                            'member_photo'=> '',
                                                            'numinvites'=> '',
                                                            'member_id' => '',
                                                            'numinvites'=> '0',
                                                             'attendance_per'=> '0'
                                                            
                                                        ); 
                                                          
                                              
                                          }
                                        
                                     // GET TRIBE MEMBERS
                                     
                                     
                                      
                                      
                                      $response = array(
                                            "requestSuccessful" => true, // Assuming the request was successful
                                            "responseMessage" => "success",
                                            "responseDescription" => "Tribe record found",
                                             "tribe_id" => $TribeID,
                                             "tribe_leader_id" => $TribeLeaderID,
                                             "tribe_title" => $TribeTitle,
                                             "tribe_des" => $TribeDes,
                                             "tribe_image" => $TribeImage,
                                             "tribe_num_members" => $num_users,
                                             "tribe_leader_name" => $Full_Name,
                                             "tribe_location" => $TribeLocation,
                                             "mini_tribe_num" => $pros_load_total_num_users_mini_tribe_cnt,
                                             "memberscontent" => $members_array,
                                             "tribemeetingcontent" => $meetday_array,
                                             "loadfullminitribe_content" => $prosload_mini_tribe_array
                                            
                                             
                                           );
                                     
              }else
              {
               
                                $response = array(
                                    "requestSuccessful" => true, // Assuming the request was successful
                                    "responseMessage" => "failed",
                                    "responseDescription" => "Tribe record not found",
                                    "tribe_id" => '',
                                    "tribe_leader_id" => '',
                                    "tribe_title" => '',
                                    "tribe_des" => '',
                                    "tribe_image" => '',
                                    "tribe_num_members" => '',
                                    "tribe_leader_name" => '',
                                    "tribe_location" => '',
                                    "mini_tribe_num" => '0',
                                    "memberscontent" => '',
                                    "tribemeetingcontent" => '',
                                    "loadfullminitribe_content" => array(
                                        'minitribe_id' => '',
                                        'minitribe_title' => '',
                                        'minitribe_location' => '',
                                        'minitribe_image' => '',
                                        'num_min_users' => '',
                                        'mini_tribe_leader_fullname' => '',
                                        'mini_tribe_full_member_content' => array(
                                            'user_mini_member_fname' => '',
                                            'user_mini_member_mname' => '',
                                            'user_mini_member_lname' => '',
                                            'user_mini_member_photo' => '',
                                            'user_mini_member_user_id' => ''
                                        ),
                                        'mini_tribe_full_meetday_content' => array(
                                            'meting_days_mini' => '',
                                            'meeting_start_duration_mini' => '',
                                            'meeting_end_duration_mini' => '',
                                            'meeting_id_mini' => ''
                                        )
                                    )
                                );
                     
                       
                  
              }
       
       
       
        $utf8_string = mb_convert_encoding($response, 'UTF-8', 'UTF-8');
        
        $json_response = json_encode($utf8_string, JSON_UNESCAPED_UNICODE);
    
         // Output the JSON response
        echo $json_response;

        
      
        
       

?>