

<?php



                error_reporting(E_ALL);
                ini_set('display_errors', 1);
                
                include('../config/config.php');
                
                //collect the input details
                $username = trim($_POST['username']);
                $institution = trim($_POST['InstutionID']);
                
                
                $campus = mysqli_query($link,"SELECT * FROM `campus` WHERE InstitutionID = '$institution' ORDER BY CampusName");
                $campus_fetch = mysqli_fetch_assoc($campus);
                $campus_count = mysqli_num_rows($campus);

                
                   $campusnew = array();

                    do{
                        $CampusName = $campus_fetch['CampusName'];
                        $CampusID = $campus_fetch['CampusID'];

                         $campusnew[] =$CampusID;

                    }while($campus_fetch = mysqli_fetch_assoc($campus));

                     $campusidnew   = implode(',', $campusnew);
                           
                       
            
           
           
                $otp = rand(10000, 99999);
    
                // Ensure that the OTP is exactly 5 digits
                $otp = str_pad($otp, 5, '0', STR_PAD_LEFT);
            
                $sql = ("SELECT * FROM `userlogin` WHERE `UserRegNumberOrUsername`='$username' AND UserType != 'owner'
                AND InstitutionIDOrCampusID IN ('$institution', '$campusidnew')");
                $result = mysqli_query($link, $sql);
                $row = mysqli_fetch_assoc($result);
                $row_cnt = mysqli_num_rows($result);
                
                
                //PROS API KEY HERE
                 $pros_selectappi_details = mysqli_query($link,"SELECT * FROM `whatsappapikey` WHERE InstitutionID='$institution'");
                  $pros_selectappi_detailscnt = mysqli_num_rows($pros_selectappi_details);
                  $pros_selectappi_detailscnt_rows = mysqli_fetch_assoc($pros_selectappi_details);
                  
                  
                  if($pros_selectappi_detailscnt > 0)
                  {
                      
                        $apikey =  $pros_selectappi_detailscnt_rows['ApiKey'];
                      
                  }else 
                  {
                               $pros_selectappi_details_default = mysqli_query($link,"SELECT * FROM `whatsappapikey` WHERE InstitutionID='0' AND Purpose='Default'");
                              $pros_selectappi_detailscnt_default = mysqli_num_rows($pros_selectappi_details_default);
                              $pros_selectappi_detailscnt_rows_default = mysqli_fetch_assoc($pros_selectappi_details_default);
                              
                               $apikey= $pros_selectappi_detailscnt_rows_default['ApiKey'];
                         
                  }
                //PROS API KEY HERE
                
                $prosinsphonenumber = 0;
                
                
                if($row_cnt > 0)
                {
                    
                                     
                         $prosuserID = $row['UserID'];
                         $UserType = $row['UserType'];
                         
                         
                         
                         if($UserType == 'student')
                         {
                             
                            
                            $prosgetusernumbercontent = mysqli_query($link,"SELECT * FROM `student`
                            INNER JOIN `parent` ON `student`.`ParentID` = 
                            `parent`.`ParentID` WHERE `student`.StudentID='$prosuserID'");
                            
                            $prosgetusernumbercontentcnt = mysqli_num_rows($prosgetusernumbercontent);
                            $prosgetusernumbercontentcnt_row = mysqli_fetch_assoc($prosgetusernumbercontent);
                           
                            
                            if($prosgetusernumbercontentcnt > 0)
                            {
                                
                                
                                
                                
                                  $ParentOtherPhoneNumber = $prosgetusernumbercontentcnt_row['ParentOtherPhoneNumber'];
                                  $ParentMainPhoneNumber = $prosgetusernumbercontentcnt_row['ParentMainPhoneNumber'];
                                  $ParentWhatsappNumber = $prosgetusernumbercontentcnt_row['ParentWhatsappNumber'];
                                  
                                  
                                  $fullname = $prosgetusernumbercontentcnt_row['StudentFirstName'].' '.$prosgetusernumbercontentcnt_row['StudentLastName'];
                                   
                                  
                                  if($ParentMainPhoneNumber != '')
                                  {
                                      
                                      $prosvalidatephonenumber =  $ParentMainPhoneNumber;
                                      
                                       $prosinsphonenumber+=1;
                                  }
                                  
                                  else if($ParentWhatsappNumber == '' &&  $ParentMainPhoneNumber != '')
                                  {
                                    
                                       $prosvalidatephonenumber =   $ParentMainPhoneNumber;
                                       
                                       $prosinsphonenumber+=1;
                                       
                                       
                                  }else if($ParentWhatsappNumber == '' && $ParentMainPhoneNumber == '' && $ParentOtherPhoneNumber != '')
                                  {
                                      
                                      $prosvalidatephonenumber =  $ParentOtherPhoneNumber;
                                      
                                      $prosinsphonenumber+=1;
                                      
                                  }else
                                  {
                                      $prosinsphonenumber+=0;
                                      
                                  }
                                 
                                 
                                 
                                
                                  
                                
                                  
                                  
                                
                            }else
                            {
                                
                                echo 'noparent';
                                
                            }
                            
                               
                           
                         }else if($UserType == 'parent')
                         {
                            
                            
                            
                            
                            
                            
                                    $prosgetusernumbercontent = mysqli_query($link,"SELECT * FROM `parent` WHERE ParentID='$prosuserID'");
                                    
                                    $prosgetusernumbercontentcnt = mysqli_num_rows($prosgetusernumbercontent);
                                    $prosgetusernumbercontentcnt_row = mysqli_fetch_assoc($prosgetusernumbercontent);
                                    
                                    
                                    if($prosgetusernumbercontentcnt > 0)
                                    {
                                        
                                        
                                        
                                        
                                          $ParentOtherPhoneNumber = $prosgetusernumbercontentcnt_row['ParentOtherPhoneNumber'];
                                          $ParentMainPhoneNumber = $prosgetusernumbercontentcnt_row['ParentMainPhoneNumber'];
                                          $ParentWhatsappNumber = $prosgetusernumbercontentcnt_row['ParentWhatsappNumber'];
                                          
                                           
                                          
                                          $fullname = $prosgetusernumbercontentcnt_row['ParentFirstName'].' '.$prosgetusernumbercontentcnt_row['ParentLastName'];
                                          
                                          if($ParentMainPhoneNumber != '')
                                          {
                                              
                                              $prosvalidatephonenumber =  $ParentMainPhoneNumber;
                                              
                                               $prosinsphonenumber+=1;
                                          }
                                          
                                          else if($ParentWhatsappNumber == '' &&  $ParentMainPhoneNumber != '')
                                          {
                                            
                                               $prosvalidatephonenumber =   $ParentMainPhoneNumber;
                                               
                                               $prosinsphonenumber+=1;
                                               
                                               
                                          }else if($ParentWhatsappNumber == '' && $ParentMainPhoneNumber == '' && $ParentOtherPhoneNumber != '')
                                          {
                                              
                                              $prosvalidatephonenumber =  $ParentOtherPhoneNumber;
                                              
                                              $prosinsphonenumber+=1;
                                              
                                          }else
                                          {
                                              $prosinsphonenumber+=0;
                                              
                                          }
                                         
                                         
                                         
                                        
                                          
                                        
                                          
                                          
                                        
                                    }else
                                    {
                                        
                                          echo 'notexist';
                                        
                                    }
                            
                            
                            
                            
                            
                            
                            
                             
                         }else
                         {
                             
                             
                                   $prosgetusernumbercontent = mysqli_query($link,"SELECT * FROM `staff` WHERE StaffID='$prosuserID'");
                                    
                                    $prosgetusernumbercontentcnt = mysqli_num_rows($prosgetusernumbercontent);
                                    $prosgetusernumbercontentcnt_row = mysqli_fetch_assoc($prosgetusernumbercontent);
                                    
                                    
                                      
                                    
                                    if($prosgetusernumbercontentcnt > 0)
                                    {
                                        
                                        
                                        
                                        
                                          $ParentOtherPhoneNumber = $prosgetusernumbercontentcnt_row['StaffOtherNumber'];
                                          $ParentMainPhoneNumber = $prosgetusernumbercontentcnt_row['StaffMainNumber'];
                                          $ParentWhatsappNumber = $prosgetusernumbercontentcnt_row['StaffWhatsappNumber'];
                                          
                                           
                                          $fullname = $prosgetusernumbercontentcnt_row['StaffFirstName'].' '.$prosgetusernumbercontentcnt_row['StaffLastName'];
                                          
                                          if($ParentMainPhoneNumber != '')
                                          {
                                              
                                              $prosvalidatephonenumber =  $ParentMainPhoneNumber;
                                              
                                               $prosinsphonenumber+=1;
                                          }
                                          
                                          else if($ParentWhatsappNumber == '' &&  $ParentMainPhoneNumber != '')
                                          {
                                            
                                               $prosvalidatephonenumber =   $ParentMainPhoneNumber;
                                               
                                               $prosinsphonenumber+=1;
                                               
                                               
                                          }else if($ParentWhatsappNumber == '' && $ParentMainPhoneNumber == '' && $ParentOtherPhoneNumber != '')
                                          {
                                              
                                              $prosvalidatephonenumber =  $ParentOtherPhoneNumber;
                                              
                                              $prosinsphonenumber+=1;
                                              
                                          }else
                                          {
                                              $prosinsphonenumber+=0;
                                              
                                          }
                                         
                                         
                                         
                                        
                                          
                                        
                                          
                                          
                                        
                                    }else
                                    {
                                        
                                          echo 'notexist';
                                        
                                    }
                              
                             
                         }
                         
                         
                         //PROS PROCCESS WHATAPPS OTP HERE
                         
                         
                         
                         
                         
                                 if($prosgetusernumbercontentcnt_row > 0)
                                 {
                                    
                                    
                                    
                                               if($prosinsphonenumber == '0')
                                               {
                                                   
                                                   
                                                      echo 'nonumber'; 
                                                      
                                                      
                                               }else
                                               {
                                                   
                                                   
                                                   
                                                   
                                                   $currentDateTime = date('Y-m-d H:i:s');
                                                    $trimotp = trim($otp);
                                                   
                                                   if($UserType == 'student')
                                                   {
                                                    
                                                           $insertotp = mysqli_query($link,"UPDATE `student` SET  `OTP`='$trimotp', `OtpDate`='$currentDateTime' WHERE `StudentID`='$prosuserID'");
                                               
                                                       
                                                   }else if($UserType == 'parent')
                                                   {
                                                       
                                                       
                                                       
                                                       $insertotp = mysqli_query($link,"UPDATE `parent` SET `OTP`='$trimotp', `OtpDate`='$currentDateTime' WHERE `ParentID`='$prosuserID'");
                                                       
                                                   }else
                                                   {
                                                       
                                                       
                                                        $insertotp = mysqli_query($link,"UPDATE `staff` SET  `OTP`='$trimotp', `OtpDate`='$currentDateTime' WHERE `StaffID`='$prosuserID'");
                                                   }
                                                   
                                                   
                                              
                                               
                                                
                                                
                                                
                                                
                                                 // Function to format a phone number
                                                        function formatPhoneNumber($phoneNumber) {
                                                        // Remove non-numeric characters
                                                        $numericPhoneNumber = preg_replace('/[^0-9]/', '', $phoneNumber);
                                            
                                                        // Check if the number starts with '0' and add '234' in that case
                                                        if (substr($numericPhoneNumber, 0, 1) === '0') {
                                                                return '234' . substr($numericPhoneNumber, 1);
                                                        } else {
                                                                // Check if the number starts with '+', if yes, remove the '+'
                                                                if (substr($numericPhoneNumber, 0, 1) === '+') {
                                                                $numericPhoneNumber = substr($numericPhoneNumber, 1);
                                                                }
                                            
                                                                // Return the numeric phone number
                                                                return $numericPhoneNumber;
                                                        }
                                                        }
                                            
                                            
                                                        // Format the phone numbers
                                                        $number = formatPhoneNumber($prosvalidatephonenumber);
                                                        
                                                
                                                         $pros_institutioncontent = mysqli_query($link,"SELECT * FROM `institution` WHERE InstitutionID='$institution'");
                                                          $pros_institutioncontentcnt = mysqli_num_rows($pros_institutioncontent);
                                                          $pros_institutioncontentcnt_rows = mysqli_fetch_assoc($pros_institutioncontent);
                                                          
                                                          
                                                          $prosfullschool =  $pros_institutioncontentcnt_rows['InstitutionGeneralName'];
                                                          $schoollink = 'https://'.$pros_institutioncontentcnt_rows['CustomUrl'];
                                                          $file = '';  
                                                          $abba_institution_id = $institution;
                                              
                                              
                                              
                                                       include('../scripts/messaging/whatsapp/send_whatapp_msg.php');
                                             
                                                       $msg = rawurlencode(
                                                            '*OTP for ' .$fullname . '*' . "\n\n" .
                                                            'Dear ' .$fullname. ',' . "\n\n" .
                                                            'Your OTP code is: ' . $otp . "\n\n" .
                                                            'Please enter this OTP to complete your verification process.' . "\n\n" .
                                                            'If you did not request this OTP, please ignore this message.' . "\n\n" .
                                                            'Thank you' . "\n\n" .
                                                            '*(powered by EduMESS)*'
                                                        );

                                  

                                                
                                                     send_whatsapp_msg($abba_institution_id, $number,$apikey, $msg, $file);  
                                                     
                                                    // $data = array(
                                                    //     'UserID' => $prosuserID,
                                                    //     'usertype' => $UserType
                                                    // );
                                                    
                                                    // Encode data to JSON format
                                                    
                                                    
                                                   echo  '<input type="hidden" id="prosloaduserttype" value="'.$UserType.'">
                                                     <input type="hidden" id="prosloaduserid" value="'.$prosuserID.'">';
                                                    
                                                   
                                                   
                                                   
                                                                                                            
                                                    
                                                
                                                                    
                                                          
                                               } 
                                               
                                               
                                 }else
                                 {
                                     
                                 }
                                  
                                  
                                  
                                  
                         
                         //PROS PROCCESS WHATAPPS OTP HERE
                         
                         
                         
                         
                         
                         
                         
                         
                        
                     
                }else
                {
                    echo 'notexist';
                }
            
            
            
            
    
  ?>
