<?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
        
        
        $member_fname =  mysqli_real_escape_string($link, trim(isset($_POST['member_fname']) ? $_POST['member_fname'] : ''));
        $member_mname =  mysqli_real_escape_string($link, trim(isset($_POST['member_mname']) ? $_POST['member_mname'] : ''));
        $member_lname =  mysqli_real_escape_string($link, trim(isset($_POST['member_lname']) ? $_POST['member_lname'] : ''));
      
        
        
      
        $member_spouse_name =  mysqli_real_escape_string($link, trim(isset($_POST['member_spouse_name']) ? $_POST['member_spouse_name'] : ''));
        
        $member_id =  mysqli_real_escape_string($link, trim(isset($_POST['member_id']) ? $_POST['member_id'] : ''));
       
        
        
         $spouse_dateofbirth =  mysqli_real_escape_string($link, trim(isset($_POST['spouse_dateofbirth']) ? $_POST['spouse_dateofbirth'] : ''));
         $spouse_occupation =  mysqli_real_escape_string($link, trim(isset($_POST['spouse_occupation']) ? $_POST['spouse_occupation'] : ''));
         $member_anniversary =  mysqli_real_escape_string($link, trim(isset($_POST['member_anniversary']) ? $_POST['member_anniversary'] : ''));
         $member_nickname =  mysqli_real_escape_string($link, trim(isset($_POST['member_nickname']) ? $_POST['member_nickname'] : ''));
         
         
         
          $pros_member_datebirth =  mysqli_real_escape_string($link, trim(isset($_POST['pros_member_datebirth']) ? $_POST['pros_member_datebirth'] : ''));
          $member_gender =  mysqli_real_escape_string($link, trim(isset($_POST['member_gender']) ? $_POST['member_gender'] : ''));
          $prosload_occupation =  mysqli_real_escape_string($link, trim(isset($_POST['prosload_occupation']) ? $_POST['prosload_occupation'] : ''));
          $member_address =  mysqli_real_escape_string($link, trim(isset($_POST['member_address']) ? $_POST['member_address'] : ''));
          
          $pros_load_profile_image =  mysqli_real_escape_string($link, trim(isset($_POST['pros_load_profile_image']) ? $_POST['pros_load_profile_image'] : ''));
          
          
          
          
        
        
         
         $pros_childfull_name =  explode(',', trim(isset($_POST['pros_childfull_name']) ? $_POST['pros_childfull_name'] : ''));
         $pros_childdate_birth =  explode(',', trim(isset($_POST['pros_childdate_birth']) ? $_POST['pros_childdate_birth'] : ''));
         
         $currentDateTime = date('Y-m-d H:i:s');
         
         
         
       
        
      
            
            
                //  $member_nickname
                    $insert_tribe = mysqli_query($link, " UPDATE `Users` SET `UserFirstName`='$member_fname',`UserMiddleName`='$member_mname',`UserLastName`='$member_lname',`NickName`='$member_nickname',`Photo`='$pros_load_profile_image',
          `DOB`='$pros_member_datebirth',`Gender`='$member_gender',
          `Ocupation`='$prosload_occupation',`Address`='$member_address',`SpouseFullName`='$member_spouse_name',`SpouseDOB`='$spouse_dateofbirth',`SpouseOccupation`='$spouse_occupation',`Anniversary`='$member_anniversary' WHERE UserID='$member_id'");
                    
                    if($insert_tribe == true)
                    {
                        
                            $lastInsertId = $member_id;
                        
                           if($pros_childfull_name == '')
                           {
                                  foreach($pros_childfull_name as $key => $pros_childfull_name_arr)
                                  {
                                      
                                         $pros_childfull_name_arr;
                                         $pros_childdate_birtharr = $pros_childdate_birth[$key];
                                         
                                        $insert_child_details = mysqli_query($link,"INSERT INTO `childreninfo`(`ChildrenID`, `ParentID`, `ChildFullName`, `ChildDOB`, `DateJoined`, `DaleteStatus`, `DeleteDate`) 
                                       VALUES (NULL,'$lastInsertId','$pros_childfull_name_arr','$pros_childdate_birtharr','$currentDateTime','',''");
                                         
                                      
                                  }
                                       
                               
                           }else
                           {
                               
                           }
                            
                            
                            
                            
                            
                       
                           
                           
                          $response = array(
                            "requestSuccessful" => true, // Assuming the request was successful
                            "responseMessage" => "success",
                            "responseDescription" => "member details updated successfully"
                             
                             );
                        
                    }else
                    {
                        
                          $response = array(
                                "requestSuccessful" => true, // Assuming the request was successful
                                "responseMessage" => "failed",
                                "responseDescription" => "Error!! member details could not be created kindly try again later"
                                 
                             );
                        
                    }
            
            
            
           
        
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>