<?php
session_start();
error_reporting(0);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


if (isset($_POST['logins']) && isset($_POST['password'])) {
  $logins = $_POST['logins'];
  $password = trim($_POST['password']);


  if ($password != '' && $logins != '') {

      require 'connect.php'; 
      //================== GET DATA VERIFIED ==================================
      $data = mysqli_query($connect, "SELECT * FROM customers WHERE Username = '$logins'");
      $numRows = mysqli_num_rows($data);

      if ($numRows > 0) {
        $fetchData = mysqli_fetch_assoc($data);
        $DBpassword = $fetchData['Password'];
        if ($DBpassword === $password) {
           $updateLastLogin = mysqli_query($connect, "UPDATE customers SET LastLogin = NOW() WHERE ID = ".$fetchData['ID']."");
           if ($updateLastLogin) {
            $_SESSION['UserID'] = $fetchData['ID'];
            $email = $fetchData['Email'];
            
            
            
            
            $sesh_id = $_SESSION['UserID'];
            
            $otp = rand(1000, 9000);
            
            // $con = mysqli_connect("localhost", "trustca1_admin", "boy510251023Aa", "trustca1_spark");
            
            $query = "UPDATE customers SET otp = '$otp' WHERE id='$sesh_id' ";

            $query_run = mysqli_query($connect, $query);
            
            echo "success";
             
// require 'PHPMailer-master/src/Exception.php';
// require 'PHPMailer-master/src/PHPMailer.php';
// require 'PHPMailer-master/src/SMTP.php';        
            
            
            
//             //  ==================== SEND MAIL =====================================
          
// // Instantiation and passing [ICODE]true[/ICODE] enables exceptions
// $mail = new PHPMailer(true);
 
// try {
//     //Server settings
//     $mail->SMTPDebug = 0;                                       // Enable verbose debug output
//     $mail->isSMTP();                                            // Set mailer to use SMTP
//     $mail->Host       = 'en.ascensiontrustgroup.com
// ';  // Specify main and backup SMTP servers
//     $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
//     $mail->Username   = 'info@en.ascensiontrustgroup.com';                     // SMTP username
//     $mail->Password   = 'supportadmin';                               // SMTP password
//     $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
//     $mail->Port       =  587;                                    // TCP port to connect to 587
 
//     //Recipients
//     $mail->setFrom('info@en.ascensiontrustgroup.com', 'PCB');
//     $mail->addAddress($email, $fullName);     // Add a recipient
//     $mail->addAddress($email);               // Name is optional
//     $mail->addReplyTo($email, 'Information');
//     $mail->addCC($email);
//     $mail->addBCC($email);
 
   
 
//     // Content
//     $mail->isHTML(true);                                  // Set email format to HTML
//     $mail->Subject = 'Welcome to SFB';
//     $mail->Body    = '<b>Thank you for choosing us, Your 0TP CODE is: '.$otp.'</b>';
//     $mail->AltBody = 'Thank you for choosing us, we are glad to have you. Please login to your account with the above code. ';
 
//     $mail->send();
 
// } catch (Exception $e) {
//     echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// }
//           //===================== SEND MAIL END =================================
            
            
            
            
            
            
            
            
            


           }else {
              echo '<p class="error">Error: Could not log you in at this moment</p>';
           }
                     
        }else {
          echo '<p class="error"> Error: Invalid credentials </p>';
        }

      }else {
        echo '<p class="error"> Error: Invalid credentials </p>';
      }
  }else {
    echo '<p class="error"> Both fields must not be empty.. </p>';
  }






}else {
  header("Location: /personal/");
  die();
}