<?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'] : ''));
        $member_id =  mysqli_real_escape_string($link, trim(isset($_POST['member_id']) ? $_POST['member_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 UserID = '$member_id' ORDER BY UserFirstName ASC");
                            $select_user_cnt = mysqli_num_rows($select_user);
                            
                            $select_user_cnt_row = mysqli_fetch_assoc($select_user);
                           
                            $prosload_user_array = [];
                           
                           if($select_user_cnt > 0)
                           {
                               
                               
                               
                              
                               
                               
                                 
                                     
                                       
                                         $UserIDnew =  $select_user_cnt_row['UserID'];
                                        $UserFirstName =  $select_user_cnt_row['UserFirstName'];
                                        $UserLastName =  $select_user_cnt_row['UserLastName'];
                                        $UserMiddleName =  $select_user_cnt_row['UserMiddleName'];
                                        $Photo =  $select_user_cnt_row['Photo'];
                                        $NumberofInvite =  $select_user_cnt_row['NumberofInvite'];
                                        $UserTypenew=  $select_user_cnt_row['UserType'];
                                        $Phone =  $select_user_cnt_row['Phone'];
                                        
                                        
                                         
                                        $DateJoin=  $select_user_cnt_row['DateJoin'];
                                        $Email=  $select_user_cnt_row['Email'];
                                        $DOB=  $select_user_cnt_row['DOB'];
                                        $Gender =  $select_user_cnt_row['Gender'];
                                        $Ocupation =  $select_user_cnt_row['Ocupation'];
                                        $Address =  $select_user_cnt_row['Address'];
                                        $SpouseFullName =  $select_user_cnt_row['SpouseFullName'];
                                        $SpousePhone =  $select_user_cnt_row['SpousePhone'];
                                        $SpouseDOB =  $select_user_cnt_row['SpouseDOB'];
                                        $SpouseOccupation =  $select_user_cnt_row['SpouseOccupation'];
                                        $Anniversary =  $select_user_cnt_row['Anniversary'];
                                        $RefereeNameOrSource =  $select_user_cnt_row['RefereeNameOrSource'];
                                        $TribeAssigenedID =  $select_user_cnt_row['TribeAssigenedID'];
                                        $MiniTribeAssigenedID =  $select_user_cnt_row['MiniTribeAssigenedID'];
                                        $AcademicLevel =  $select_user_cnt_row['AcademicLevel'];
                                        
                                        
                                        $NickName_new =  $select_user_cnt_row['NickName'];
                                        
                                        
                                        
                                        
                                        
                                          $get_referee_info =  mysqli_query($link,"SELECT * FROM `Users` WHERE UserID='$RefereeNameOrSource'");
                                          $get_referee_info_cnt = mysqli_num_rows($get_referee_info);
                                          $get_referee_info_cnt_row = mysqli_fetch_assoc($get_referee_info);
                                          
                                          if($get_referee_info_cnt > 0)
                                          {
                                              
                                              
                                                $refereer_invitename = $get_referee_info_cnt_row['UserLastName'].' '.$get_referee_info_cnt_row['UserFirstName'];
                                                
                                          }else
                                          {
                                                 $refereer_invitename = ''; 
                                          }
                                          
                                        
                                          

                                        
                                        $pros_levels =  mysqli_query($link,"SELECT * FROM `academicslevels` WHERE LevelID='$AcademicLevel'");
                                        $pros_levels_cnt =  mysqli_num_rows($pros_levels);
                                        $pros_levels_cnt_row =  mysqli_fetch_assoc($pros_levels);
                                        
                                        if($pros_levels_cnt > 0)
                                        {
                                            $LevelTitle = $pros_levels_cnt_row['LevelTitle'];
                                            
                                            
                                        }else
                                        {
                                            $LevelTitle = '';
                                        }
                                        
                                        
                                        $pros_load_tribe = '';
                                        if($TribeAssigenedID == '0')
                                        {
                                            
                                           
                                            $pros_select_tribe = mysqli_query($link,"SELECT * FROM `minitribe` WHERE MiniTribeID='$MiniTribeAssigenedID'");
                                             $pros_select_tribe_cnt = mysqli_num_rows($pros_select_tribe);
                                            $pros_select_tribe_cnt_row = mysqli_fetch_assoc($pros_select_tribe);
                                            
                                            
                                            
                                            if($pros_select_tribe_cnt > 0)
                                            {
                                                

                                                  $pros_load_tribe.=$pros_select_tribe_cnt_row['MiniTribeTitle'];
                                            }
                                            
                                                   
                                        }else
                                        {
                                            
                                            $pros_select_tribe = mysqli_query($link,"SELECT * FROM `tribe` WHERE TribeID='$TribeAssigenedID'");
                                            $pros_select_tribe_cnt = mysqli_num_rows($pros_select_tribe);
                                            $pros_select_tribe_cnt_row = mysqli_fetch_assoc($pros_select_tribe);
                                            
                                            if($pros_select_tribe_cnt > 0)
                                            {
                                                $pros_load_tribe.=$pros_select_tribe_cnt_row['TribeTitle'];
                                            }
                                            
                                        }
                                        
                                           $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;
                                             }
                                                  
                                                    
                                        
                                        
                                          $prosloadchild = mysqli_query($link, "SELECT * FROM `childreninfo` WHERE  `ParentID`='$UserIDnew'");
                                          $prosloadchild_cnt = mysqli_num_rows($prosloadchild);
                                          
                                          
                                          
                                          $prosload_child_array = [];
                                          
                                          if($prosloadchild_cnt > 0)
                                          {
                                              
                                              
                                                  
                                              
                                                        while($prosloadchild_cnt_row = mysqli_fetch_assoc($prosloadchild))
                                                        {
                                                            
                                                            
                                                            
                                                             $ChildFullName =   $prosloadchild_cnt_row['ChildFullName'];
                                                             $ChildDOB =   $prosloadchild_cnt_row['ChildDOB'];
                                                             $ChildrenID =   $prosloadchild_cnt_row['ChildrenID'];
                                                             
                                                             	
                                                             
                                                              $prosload_child_array[] = array(
                                                                'child_id' => $ChildrenID,
                                                                'child_fullname' => $ChildFullName,
                                                                'child_dob'=> $ChildDOB);
                                                               
                                                            
                                                        }
                                              
                                          }else
                                          {
                                              
                                              $prosload_child_array[] = array(
                                                'child_id' => '',
                                                'child_fullname' => '',
                                                'child_dob'=> '');
                                              
                                          }
                                          
                                   
                                        // PaymentType Amount DatePaid
                                        // PROS LOAD PAYMENT CONTENT HERE 
                                        
                                        
                                        $getallmount_cont = mysqli_query($link, "SELECT SUM(Amount) AS Amountnew FROM `payment` WHERE UserID='$UserIDnew'");
                                        $getallmount_cont_row = mysqli_fetch_assoc($getallmount_cont);
                                        
                                        $prosloadamount = $getallmount_cont_row['Amountnew'];
                                        
                                        $prosload_full_amount = 0;
                                        
                                        if($prosloadamount == '' || $prosloadamount == '0' || $prosloadamount == 'NULL')
                                        {
                                             $prosload_full_amount+=0;
                                             
                                        }else
                                        {
                                            
                                              $prosload_full_amount+=$prosloadamount;
                                            
                                        }
                                        
                                          $prospayment_content = [];
                                     
                                          $select_paymentloop = mysqli_query($link, "SELECT * FROM `payment` WHERE UserID='$UserIDnew'");
                                          $select_paymentloop_cnt = mysqli_num_rows($select_paymentloop);
                                          
                                          if($select_paymentloop_cnt > 0)
                                          {
                                              
                                          
                                         
                                             while($select_paymentloop_cnt_row = mysqli_fetch_assoc($select_paymentloop))
                                             {
                                                 
                                                  $PaymentType = $select_paymentloop_cnt_row['PaymentType'];
                                                  $Amount = $select_paymentloop_cnt_row['Amount'];
                                                  $DatePaid = $select_paymentloop_cnt_row['DatePaid'];
                                                  
                                                 
                                                  $select_payment_type = mysqli_query($link, "SELECT * FROM `paymenttype` WHERE PaymentTypeID='$PaymentType'");
                                                  $select_payment_type_row = mysqli_fetch_assoc($select_payment_type);
                                                  
                                                 
                                                  
                                                 $PaymentTypename =  $select_payment_type_row['PaymentType'];
                                                  
                                                   $prospayment_content[] = array(
                                                    'payment_type' => $PaymentTypename,
                                                    'payment_amount' => $Amount,
                                                    'payment_date'=> $DatePaid);
                                                 
                                             }
                                        }else
                                        {
                                            
                                             $prospayment_content[] = array(
                                                    'payment_type' => '',
                                                    'payment_amount' => 0,
                                                    'payment_date'=> '');
                                            
                                        }
                                     // PROS LOAD PAYMENT CONTENT HERE 
                                        
                                        
                                       
                                        
                                        // PROS LOAD PRAYER REQUEST HERE
                                        
                                        
                                         $prayerrequest_content = [];
                                         
                                         $pros_select_prayer_request = mysqli_query($link, "SELECT * FROM `prayerrequest` WHERE UserID='$UserIDnew'");
                                         $pros_select_prayer_request_cnt = mysqli_num_rows($pros_select_prayer_request);
                                        
                                         
                                         if($pros_select_prayer_request_cnt > 0)
                                         {
                                             
                                                 while($pros_select_prayer_request_cnt_row = mysqli_fetch_assoc($pros_select_prayer_request))
                                                 {
                                                     
                                                     
                                                         
                                                    $DateRequested =  $pros_select_prayer_request_cnt_row['DateRequested'];
                                                    $PrayedSatus=  $pros_select_prayer_request_cnt_row['PrayedSatus'];
                                                    $Des =  $pros_select_prayer_request_cnt_row['Des'];
                                                    $PrayerHeader =  $pros_select_prayer_request_cnt_row['PrayerHeader'];
                                                     $PrayerID =  $pros_select_prayer_request_cnt_row['PrayerID'];
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    $prayerrequest_content[] = array(
                                                    'date_requested' => $DateRequested,
                                                    'prayer_status' => $PrayedSatus,
                                                    'prayer_title'=> $Des,
                                                    'prayer_herader'=> $PrayerHeader,
                                                    'prayer_id'=> $PrayerID 
                                                    );
                                                    
                                                    
                                                    
                                                    
                                                    
                                                     
                                                 }
                                                 
                                             
                                         }else
                                         {
                                             
                                             
                                               $prayerrequest_content[] = array(
                                                    'date_requested' => '',
                                                    'prayer_status' => '',
                                                    'prayer_title'=> '',
                                                    'prayer_herader'=> '' ,
                                                    'prayer_id'=> '' 
                                                    
                                                    );
                                                    
                                             
                                         }
                                         
                                        // PROS LOAD PRAYER REQUEST HERE
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                         $prosload_user_array[] = array(
                                                'user_id' => $UserIDnew,
                                                'user_fname' => $UserFirstName,
                                                'user_lname'=> $UserLastName,
                                                'photo'=> $Photo,
                                                'numofinvite'=> $NumberofInvite,
                                                'usertype'=> $UserTypenew,
                                                
                                                'date_joined' => $DateJoin,
                                                'email' => $Email,
                                                'dob' => $DOB,
                                                'gender' => $Gender,
                                                'occupation' => $Ocupation,
                                                'address' => $Address,
                                                'spousename' => $SpouseFullName,
                                                'spousephone' => $SpousePhone,
                                                'spousedob' => $SpouseDOB,
                                                'refname' =>  $refereer_invitename,
                                                'spouseoccupation' => $SpouseOccupation,
                                                'anniversary' => $Anniversary,
                                                'referrename' => $RefereeNameOrSource,
                                                 'phone' => $Phone,
                                                 'percentage' => $percentageAttendance,
                                                  'tribe_title' => $pros_load_tribe,
                                                   'academic_level' => $LevelTitle,
                                                   'tribe_assigned' => $TribeAssigenedID,
                                                'usermiddlename' => $UserMiddleName,
                                                 'nickname' => $NickName_new,
                                                
                                                 'total_amountfee' => $prosload_full_amount,
                                                 'childfullcontent' => $prosload_child_array,
                                                 'fullpaymentcontent' => $prospayment_content,
                                                 'fulprayercontentcontent' => $prayerrequest_content
                                                 
                                                 
                                                        
                                               
                                                                                        
                                            );
                                        
                                        
                                        
                                        
                                      
                             
                                 
                                 
                               
                           }else
                           {
                               
                               
                               
                               $prosload_user_array[] = array(
                                                'user_id' => '',
                                                'user_fname' => '',
                                                'user_lname'=> '',
                                                'photo'=> '',
                                                'numofinvite'=> '',
                                                'usertype'=> '',
                                                'date_joined' => '',
                                                'email' => '',
                                                'dob' => '',
                                                'gender' => '',
                                                'occupation' => '',
                                                'address' => '',
                                                'spousename' => '',
                                                'spousephone' => '',
                                                'spousedob' => '',
                                                'spouseoccupation' => '',
                                                'anniversary' => '',
                                                'referrename' => '',
                                                'percentage' => '',
                                                'academic_level' => '',
                                                
                                                  'tribe_title' => '',
                                                  'phone' => '',
                                                  'total_amountfee' => '',
                                                  'childfullcontent' => '',
                                                  'fullpaymentcontent' => '',
                                                  'fulprayercontentcontent' => ''
                                                  
                                                  
                                                
                                                                                        
                                            );
                               
                           }
                           
                           
                            $response = array(
                                "requestSuccessful" => true, // Assuming the request was successful
                                "responseMessage" => "success",
                                "responseDescription" => "member's record found",
                                "numberofmembers" => $select_user_cnt,
                                 "responseBody" => $prosload_user_array
                             );
                           
                       
                             
                   
              
                     
          }
          else
          {
              
             
                   $response = array(
                        "requestSuccessful" => true, // Assuming the request was successful
                        "responseMessage" => "failed",
                        "responseDescription" => "User not found",
                        "numberofmembers" => '',
                         "responseBody" => array(
                                'user_id' => '',
                                'user_fname' => '',
                                'user_lname'=> '',
                                'photo'=> ''
                                                                                
                            )
                        
                      );
              
          }
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>