<?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 = explode(',', isset($_POST['memberid_arr']) ? $_POST['memberid_arr'] : '');
       $tribeid_arr = explode(',', isset($_POST['tribeid_arr']) ? $_POST['tribeid_arr'] : '');

         
          $attendance_eventid = isset($_POST['attendance_eventid']) ? $_POST['attendance_eventid'] : '';
          
          
          $type_mark = explode(',',isset($_POST['type_mark']) ? $_POST['type_mark'] : '');

        
       

        
        $success = 0; //count num of successful innert
        $failure = 0; //count num of failure innert
        $userexist = 0; //num user attendance has been marked
        
        foreach($member_id as $key => $member_id_arr)
        {
            $member_id_arr;
            
            $tribe_id_sin = $tribeid_arr[$key];
            $type_mark_arr = $type_mark[$key];
            
            
            // 
          
            
            
            $veri_fy_attendance = mysqli_query($link, "SELECT * FROM `attendance`
            WHERE UserID='$member_id_arr'  AND  `EventID`='$attendance_eventid' AND `TribeID`='$tribe_id_sin'");
            
            
          
            $veri_fy_attendance_cnt = mysqli_num_rows($veri_fy_attendance);
            
            if($veri_fy_attendance_cnt > 0)
            {
                
                   
                
                //  $userexist++;
                 
                 $query = "UPDATE `attendance` SET `AttendanceStatus`='$type_mark_arr' WHERE `TribeID`='$tribe_id_sin' AND `UserID`='$member_id_arr' AND `EventID`='$attendance_eventid'";

                    // Execute the query
                    $insert_attendance = mysqli_query($link, $query);
            
                    // Check if the query was successful
                    if ($insert_attendance) {
                        $success++;
                    } else {
                        $failure++;
                    }
                
            }else
            {
                
                
            
                
                 
                
                $insert_attendance =  mysqli_query($link, "INSERT INTO `attendance`(`AttendanceID`, `TribeID`, `UserID`, `EventID`, `AttendanceStatus`)
                VALUES (NULL,'$tribe_id_sin','$member_id_arr', '$attendance_eventid', '$type_mark_arr')");
                
                
                if($insert_attendance == true)
                {
                     $success++; 
                }else
                {
                     $failure++;
                    
                }
                
                
            }
            
            
            
        }
        
        
        
        if($success > 0)
        {
            
            $msg_des = 'Attendance marked successfully';
            $response_msg = 'success';
            
        }else
        {
            
            
            if($failure > 0)
            {
                
                 $msg_des = 'Error!! attendance not marked successfully please try again later';
                $response_msg = 'failed';
                
                
            }else
            {
                
                 $msg_des = 'Attendance already marked for the selected user';
                 $response_msg = 'failed';
                
            }
            
           
            
        }
       
        
          
                  $response = array(
                    "requestSuccessful" => true, // Assuming the request was successful
                    "responseMessage" => $response_msg,
                    "responseDescription" => $msg_des
                    
                    );
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>