async function verifyStudentOtp() { const phone = document.getElementById('student_phone').value.trim(); const otp = document.getElementById('student_otp').value.trim(); if (!otp || otp.length < 4) { showError('يرجى إدخال رمز تحقق صالح.'); return; } try { const res = await fetch('/api/auth/otp/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ phone_number: phone, phone: phone, otp: otp, otp_code: otp, role: 'student' }) }); const data = await res.json(); if (res.ok && data.status === 'success') { if (data.data.requires_national_id) { localStorage.setItem('saqel_student_identity_token', data.data.identity_token); document.getElementById('step_otp_container').style.display = 'none'; document.getElementById('step_national_id_container').style.display = 'block'; document.getElementById('student_national_id_login').focus(); } else { const token = data.data.token; localStorage.setItem('saqel_student_jwt', token); renderDashboard(data.data.user); } } else { showError(data.message || 'رمز التحقق غير صحيح أو منتهي الصلاحية'); } } catch (err) { showError('حدث خطأ في الاتصال بالسيرفر.'); } } async function submitNationalId() { const nationalId = document.getElementById('student_national_id_login').value.trim(); const identityToken = localStorage.getItem('saqel_student_identity_token'); if (!nationalId) { showError('يرجى إدخال الرقم الوطني الخاص بك'); return; } try { const res = await fetch('/api/auth/student/login-national-id', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ national_id: nationalId, identity_token: identityToken }) }); const data = await res.json(); if (res.ok && data.status === 'success') { if (data.data && data.data.requires_onboarding) { document.getElementById('step_national_id_container').style.display = 'none'; document.getElementById('step_onboarding_container').style.display = 'block'; document.getElementById('onboard_student_national_id').value = nationalId; document.getElementById('onboard_student_national_id').disabled = true; } else { const token = data.data.token; localStorage.setItem('saqel_student_jwt', token); localStorage.setItem('saqel_student_user', JSON.stringify(data.data.user)); localStorage.removeItem('saqel_student_identity_token'); renderDashboard(data.data.user); initWebSocket(token); showLuxuryToast('أهلاً بك يا بطل! 🚀', 'تم تسجيل الدخول لملفك الخاص بنجاح.'); } } else { showError(data.message || 'خطأ في التحقق من الرقم الوطني'); } } catch (err) { showError('حدث خطأ في الاتصال بالخادم.'); } }