<?php
             
     include('../../config/config.php');

    // $country = trim($_POST['country']);
     $lang = $_POST['lang'];
    
     $Email = $_POST['Email'];
     $mname= $_POST['mname'];
     $fname= $_POST['fname'];
     $lname = $_POST['lname'];            
     $WhatsappNumberfull = implode(',',$_POST['WhatsappNumberfull']);      
     $password = trim($_POST['password']); 
     
     $InstitutionID = $_POST['InstituteID']; 
   
     $UserType = $_POST['UserType']; 
     $StaffID = $_POST['StaffID']; 
     
     
     
   
     
        // 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($WhatsappNumberfull);


     
     $verfylogin = mysqli_query($link,"SELECT * FROM `userlogin` WHERE UserRegNumberOrUsername='$Email'");
     $verfylogincnt = mysqli_num_rows($verfylogin);
     
     
     if($verfylogincnt > 0)
     {
         
                   echo 'Email Already exist';
     }else
     {
      
               
                          $insertpat = mysqli_query($link," UPDATE `staff` SET `StaffFirstName`='$fname',`StaffMiddleName`='$mname',`StaffLastName`='$lname',`StaffMainNumber`='$WhatsappNumberfull',`StaffWhatsappNumber`='$WhatsappNumberfull'
                          WHERE `StaffID`='$StaffID' AND `InstitutionID`='$InstitutionID'");
                          
                          
                          
                         
                          
                          
                          if( $insertpat == true)
                          {
                                  
                                     echo 'Success';
                                  
                                    // $parentID = mysqli_insert_id($link);
                                    $md5password = md5($password);

                              
                                   $insertlogin = mysqli_query($link,"INSERT INTO `userlogin`(`InstitutionIDOrCampusID`,
                                  `UserID`, `UserRegNumberOrUsername`, 
                                  `UserPassword`, `UserType`,
                                  `VerificationStatus`, `LoginStatus`) VALUES ('$InstitutionID','$StaffID','$Email','$md5password','$UserType','1','0')");
                                  
                                  
                                    //   $explodestuden = explode(',', $child);
                              
                              
                                  
                                  
                                             $pros_selectappi_details = mysqli_query($link,"SELECT * FROM `whatsappapikey` WHERE InstitutionID='$InstitutionID'");
                                              $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_institutioncontent = mysqli_query($link,"SELECT * FROM `institution` WHERE InstitutionID='$InstitutionID'");
                                              $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 = $InstitutionID;
                                              
                                              
                                              
                                                  include('../messaging/whatsapp/send_whatapp_msg.php');
                                             
                                   
                                  
                                                 $msg = rawurlencode(
                                                    '*Login Details for ' . $fname . ' ' . $lname . '*' . "\n\n" .
                                                    'Dear ' . $fname . ' ' . $lname . ',' . "\n\n" .
                                                    'Welcome to ' . $prosfullschool . '! Your registration is successful.' . "\n\n" .
                                                    'Below are your login details:' . "\n\n" .
                                                    '*Username:* ' . $Email . "\n" .
                                                    '*Password:* '.$password. "\n\n" .
                                                    '*Url:* '.$schoollink. "\n\n" .
                                                    'Please keep your login information confidential. If you have any questions or concerns, feel free to contact us.' . "\n\n" .
                                                    'Thank you' . "\n\n" .
                                                    '*(powered by EduMESS)*');
                                                
                                                    send_whatsapp_msg($abba_institution_id, $number,$apikey, $msg, $file);      
                                         
                                      
                                  
                                  

                                   
                          }else
                          {
                               echo 'Failed! please try again later'; 
                          }
                                                  
                         

         
                 
     }
                    
                    
                    
                  
                                                          
?>