<?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'] : ''));
        $pros_msg_title =  mysqli_real_escape_string($link, trim(isset($_POST['pros_msg_title']) ? $_POST['pros_msg_title'] : ''));
       $pros_msg_des =  mysqli_real_escape_string($link, trim(isset($_POST['pros_msg_des']) ? $_POST['pros_msg_des'] : ''));
        
        
                                         
        
            
            
                    
                           $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)
                           {
                               
                               
                               
                              
                               
                                        if($pros_msg_title == '')
                                        {
                                            
                                             $msgnew = str_replace('rn', "\r\n", $pros_msg_des);
                                            
                                        }else
                                        {
                                           $msgnew = '*' . $pros_msg_title . '*' . PHP_EOL . PHP_EOL . str_replace('rn', "\r\n", $pros_msg_des);  
                                        }
                                    
                                       
                                        
                                        
                                        $msg = rawurlencode($msgnew);    
                     
                                       
                                         $UserIDnew =  $select_user_cnt_row['UserID'];
                                        $UserFirstName =  $select_user_cnt_row['UserFirstName'];
                                        $UserLastName =  $select_user_cnt_row['UserLastName'];
                                        $Photo =  $select_user_cnt_row['Photo'];
                                        $NumberofInvite =  $select_user_cnt_row['NumberofInvite'];
                                        $UserTypenew=  $select_user_cnt_row['UserType'];
                                         $number =  $select_user_cnt_row['Phone'];
                                        
                                         $file = '';
                                        $apikey = '7yVA8yWyHtgs';

                                   send_whatsapp_msg($number,$apikey, $msg, $file);
                        
                            
                           
                           
                          $response = array(
                            "requestSuccessful" => true, // Assuming the request was successful
                            "responseMessage" => "success",
                            "responseDescription" => "Prayer notification sent successfully"
                             
                             );
                        
                    }else
                    {
                        
                              $response = array(
                                    "requestSuccessful" => true, // Assuming the request was successful
                                    "responseMessage" => "failed",
                                    "responseDescription" => "Error!! prayer notification could not be sent, kindly try again later."
                                     
                                 );
                        
                    }
            
            
            
      
              
        
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>