<?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'] : ''));
        $pros_meeber_referee =  mysqli_real_escape_string($link, trim(isset($_POST['pros_meeber_referee']) ? $_POST['pros_meeber_referee'] : ''));
        
        
        
        
        $formatted_my_number =  mysqli_real_escape_string($link, trim(isset($_POST['formatted_my_number']) ? $_POST['formatted_my_number'] : ''));
        $member_spouse_name =  mysqli_real_escape_string($link, trim(isset($_POST['member_spouse_name']) ? $_POST['member_spouse_name'] : ''));
        $spouse_phone_number =  mysqli_real_escape_string($link, trim(isset($_POST['formatted_my_number_spouse']) ? $_POST['formatted_my_number_spouse'] : ''));
        
        
         $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_load_tribe =  mysqli_real_escape_string($link, trim(isset($_POST['pros_load_tribe']) ? $_POST['pros_load_tribe'] : ''));
         $pros_load_minitribe =  mysqli_real_escape_string($link, trim(isset($_POST['pros_load_minitribe']) ? $_POST['pros_load_minitribe'] : ''));
         
         if($pros_load_tribe == '' || $pros_load_tribe == 'NULL' || $pros_load_tribe == '0')
         {
                  $pros_newtribe_id = 0;
         }else
         {
             
              $pros_newtribe_id = $pros_load_tribe;
             
         }
         
         
         
          if($pros_load_minitribe == '' || $pros_load_minitribe == 'NULL' || $pros_load_minitribe == '0')
         {
                  $pros_newminitribe_id = 0;
         }else
         {
             
              $pros_newminitribe_id = $pros_load_minitribe;
             
         }
         
         $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');
         
         $hashedPassword = md5(1234);
         
       
        
        $pros_verify_tribe = mysqli_query($link, "SELECT * FROM `Users` WHERE Phone='$formatted_my_number'");
        $pros_verify_tribe_cnt = mysqli_num_rows($pros_verify_tribe);
        
        
        if($pros_verify_tribe_cnt > 0)
        {
            
                $response = array(
                    "requestSuccessful" => true, // Assuming the request was successful
                    "responseMessage" => "failed",
                    "responseDescription" => "Member already exist"
                     
                 );
                               
            
            
            
        }else
        {
            
            
                //  $member_nickname
                    $insert_tribe = mysqli_query($link, $insert_member = "INSERT INTO `Users`(`UserID`, `UserFirstName`, `UserMiddleName`, `UserLastName`, `NickName`, `Email`, `Phone`, `DateJoin`, `UserType`, `Photo`, `DeleteStatus`, `OTP`, `OTPDuration`, `AccValidStatus`,
                     `TribeAssigenedID`, `MiniTribeAssigenedID`,
                     `NumberofInvite`, `DOB`,`Gender`, `Ocupation`, `Address`, `SpouseFullName`, `SpousePhone`, `SpouseDOB`, `SpouseOccupation`, `Anniversary`, `RefereeNameOrSource`) VALUES
                     (NULL,'$member_fname','$member_mname','$member_lname', '$member_nickname','','$formatted_my_number','$currentDateTime','member','$pros_load_profile_image','0',
                     '','','validated','$pros_newtribe_id','$pros_newminitribe_id','0','$pros_member_datebirth', '$member_gender','$prosload_occupation','$member_address',
                     '$member_spouse_name','$spouse_phone_number','$spouse_dateofbirth','$spouse_occupation','$member_anniversary','$pros_meeber_referee')");
                    
                    if($insert_tribe == true)
                    {
                        
                            $lastInsertId = mysqli_insert_id($link);
                        
                           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
                           {
                               
                           }
                            
                            
                             $insert_login_here = mysqli_query($link, "INSERT INTO `userlogin`(`UserLoginID`, `UserID`, `UserName`, `Password`, `UserType`, `LastLoginDate`) 
                            VALUES (NULL,'$lastInsertId','$formatted_my_number','$hashedPassword','member','')");
                            
                            
                            
                       
                           
                           
                          $response = array(
                            "requestSuccessful" => true, // Assuming the request was successful
                            "responseMessage" => "success",
                            "responseDescription" => "member created successfully"
                             
                             );
                        
                    }else
                    {
                        
                          $response = array(
                                "requestSuccessful" => true, // Assuming the request was successful
                                "responseMessage" => "failed",
                                "responseDescription" => "Error!! member could not be created kindly try again later"
                                 
                             );
                        
                    }
            
            
            
        }
              
        
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>