<?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_id=  explode(',', $_POST['member_id'] ? $_POST['member_id'] : '');
        $tribe_title =  mysqli_real_escape_string($link, trim(isset($_POST['tribe_title']) ? $_POST['tribe_title'] : ''));
        $Tribe_ID =  mysqli_real_escape_string($link, trim(isset($_POST['Tribe_ID']) ? $_POST['Tribe_ID'] : ''));
        
        
        
        
        $pros_verify_tribe = mysqli_query($link, "SELECT * FROM `minitribe` WHERE TribeID='$Tribe_ID' AND MiniTribeTitle='$tribe_title'");
        $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" => "Tribe already exist"
                             
                         );
                               
            
            
            
        }else
        {
            
            
            
                    $insert_tribe = mysqli_query($link, "INSERT INTO `minitribe`(`MiniTribeID`, `TribeID`, `MiniTribeLeaderID`, `MiniTribeTitle`, `MiniTribeDes`, `MiniTribeLocation`, `MiniTribeImage`, `DeleteSatus`)
                    VALUES (NULL,'$Tribe_ID','0','$tribe_title','','','','0')");
                    
                    
                    
                    
                    if($insert_tribe == true)
                    {
                        
                            $lastInsertId = mysqli_insert_id($link);
                            
                            
                            
                            
                            foreach($member_id as $member_idarr)
                            {
                                $member_idarr;
                                
                                
                                  $update =  mysqli_query($link, "UPDATE `Users` SET `MiniTribeAssigenedID`='$lastInsertId' WHERE `UserID`='$member_idarr'");
                            }
                            
                         
                           
                           
                          $response = array(
                            "requestSuccessful" => true, // Assuming the request was successful
                            "responseMessage" => "success",
                            "responseDescription" => "Tribe created successfully"
                             
                             );
                        
                    }else
                    {
                        
                          $response = array(
                                "requestSuccessful" => true, // Assuming the request was successful
                                "responseMessage" => "failed",
                                "responseDescription" => "Error!! tribe could not be created kindly try again later"
                                 
                             );
                        
                    }
            
            
            
        }
              
        
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>