<?php
// Start session
session_start();

// Include database connection
require_once('../Connections/ebanking.php');

// Include PHPMailer files if not using Composer
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Handle login form submission
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['username'])) {
    $loginUsername = mysqli_real_escape_string($ebanking, $_POST['username']);
    $password = mysqli_real_escape_string($ebanking, $_POST['password']);

    $query = "SELECT * FROM customer WHERE accountno='$loginUsername' AND password='$password'";
    $result = mysqli_query($ebanking, $query);
    $user = mysqli_fetch_assoc($result);

    if ($user) {
        // Generate OTP code
        $otp = rand(1000, 9999);
        $_SESSION['otp'] = $otp;
        $_SESSION['accountno'] = $user['accountno'];

        // Send OTP code via email
        $mail = new PHPMailer(true);
        try {
            $mail->isSMTP();
            $mail->Host       = 'mail.al-shajarahislamicbank.com';
            $mail->SMTPAuth   = true;
            $mail->Username   = 'noreply@al-shajarahislamicbank.com';
            $mail->Password   = 'yc1]f2FqB{6J';
            $mail->SMTPSecure = 'ssl';
            $mail->Port       = 465;
            $mail->setFrom('noreply@al-shajarahislamicbank.com', 'Al Shajarah Islamic Bank');
            $mail->addAddress($user['email'], $user['accountno']);
            $mail->isHTML(true);
            $mail->Subject = 'Login OTP Code';
            $mail->Body    = '<h1>Your Secured Login OTP Code Is:</h1><br><h2>' . $otp . '</h2>';
            $mail->AltBody = 'Your OTP Code Is: ' . $otp;
            $mail->send();

            // Redirect to OTP verification page
            header("Location: otp_verification.php");
            exit();
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}<br>";
        }
    } else {
        // Redirect to failed login page
        header("Location: failedlogin.php");
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Online Banking Login</title>

<!-- Bootstrap Core CSS -->
<link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="../css/adminnine_classic.css" rel="stylesheet">
<link href="../vendor/roc/jbclient.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="../vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>
<body>
    <div id="content">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 col-xs-12">
                    <div class="form-field">
                        <h3 class="text-center"><img src="../img/grand-logo.png" width="416" height="70" alt=""/><br>
                          <br>
                          <br>
<br>
                          <br>
                          <br>
                        LOGIN</h3>
                        <p>Please enter your credentials in the following form</p>
                        <form aaction="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" name="userlogin" id="userlogin">
                            <fieldset>
                                <div class="form-group">
                                    <label for="username">User ID</label>
                                    <input type="username" class="form-control" name="username" autofocus>
                                </div>
                                <div class="form-group">
                                    <label for="password">Password</label>
                                    <input type="password" class="form-control" name="password" value="">
                                </div>
                                <div class="checkbox">
                                    <label><input type="checkbox" value="Remember Me" placeholder="User ID" name="remember">Remember Me</label>
                                </div>
                                <br>
                                <input name="Login" type="submit" class="btn btn-primary" value="Login">
                            </fieldset>

                            <br>
                            <br>
                            <div class="row">
                            <div class="col-md-6 "> <a href="forgotpassword.php">Forgot password?</a> </div>
                            
                            </div>
                        </form>

                        <hr>

                        <h5>Beware of fraudulent attacks</h5>
                        <p>We will never contact you by telephone, e-mail nor internet asking you to disclose or to perform a confirmation by entering your Password or One Time Password (OTP)</p>
                  </div>
                </div>
            </div>
        </div>
    </div>

    <div id="jbfooter">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <hr>
                    <div class="row">
                        <div class="col-md-8 col-sm-12 col-xs-12">
                            <a class="bjb-link-arrow text-nowrap" href="#">About Us</a>
                            <a class="bjb-link-arrow text-nowrap" href="#">Terms Of Use</a>
                            <a class="bjb-link-arrow text-nowrap" href="#">Privacy Policy</a>
                        </div>
                        <div class="col-md-4 col-sm-12 col-xs-12">
                            <p class="copyright">© 2024 Al Shajarah Islamic Bank</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script src="../vendor/jquery/jquery.min.js"></script> 

<!-- Bootstrap Core JavaScript --> 
<script src="../vendor/bootstrap/js/bootstrap.min.js"></script> 

<!-- Custom Theme JavaScript --> 
<script src="../js/adminnine.js"></script>
</body>
</html>