أداة الطباعة الذكية عبر Wi-Fi والمستندات من الهاتف

`); printWindow.document.close(); showStatus('success', '✅', 'تم إرسال المستند للطباعة!'); } catch (error) { showStatus('error', '❌', 'خطأ: ' + error.message); } } // ====== الطباعة عبر Wi-Fi ====== function printViaWiFi() { showStatus('info', '⏳', 'جاري البحث عن الطابعات...'); // عرض خيار IP للمستخدم document.getElementById('ipInputGroup').style.display = 'block'; setTimeout(() => { showStatus('warning', 'ℹ️', 'استخدم "طباعة عبر النظام" للطباعة التلقائية، أو أدخل IP الطابعة يدوياً.'); }, 2000); } // ====== إظهار حقل IP ====== function printWithIP() { const ipGroup = document.getElementById('ipInputGroup'); if (ipGroup.style.display === 'none') { ipGroup.style.display = 'block'; showStatus('info', 'ℹ️', 'أدخل عنوان IP الخاص بالطابعة ثم اضغط "طباعة"'); } else { ipGroup.style.display = 'none'; } } // ====== الطباعة عبر IP ====== function printToIP() { const ip = document.getElementById('printerIP').value.trim(); if (!ip) { showStatus('warning', '⚠️', 'الرجاء إدخال عنوان IP صحيح للطابعة.'); return; } // التحقق من صحة الـ IP const ipPattern = /^(\d{1,3}\.){3}\d{1,3}$/; if (!ipPattern.test(ip)) { showStatus('error', '❌', 'عنوان IP غير صحيح. استخدم格式: 192.168.1.100'); return; } showStatus('info', '⏳', `محاولة الاتصال بـ ${ip}...`); // محاولة الاتصال بالطابعة عبر IP // هذه محاولة تقريبية، قد لا تعمل مع جميع الطابعات try { // إنشاء iframe مخفي للاتصال بالطابعة const iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = `http://${ip}`; document.body.appendChild(iframe); setTimeout(() => { showStatus('success', '✅', `تم إرسال الطلب إلى ${ip}. تحقق من الطابعة.`); document.body.removeChild(iframe); }, 3000); } catch (e) { showStatus('error', '❌', 'تعذر الاتصال بالطابعة. تأكد من IP وأن الطابعة على نفس الشبكة.'); } } // ====== معاينة ====== function previewPrint() { const content = document.getElementById('printContent').value; const hasFile = uploadedFileUrl !== null; if (!content.trim() && !hasFile) { showStatus('warning', '⚠️', 'الرجاء إدخال نص أو رفع ملف للمعاينة!'); return; } const previewWindow = window.open('', '_blank', 'width=800,height=600'); if (!previewWindow) { showStatus('error', '❌', 'الرجاء السماح للنوافذ المنبثقة.'); return; } let displayContent = ''; const isImage = hasFile && uploadedFileType && uploadedFileType.startsWith('image/'); if (isImage) { displayContent = ``; } else if (hasFile) { displayContent = `

📄 ${uploadedFileName}

ملف جاهز للطباعة

`; } else { displayContent = content.replace(/\n/g, '
'); } previewWindow.document.write(` معاينة - Smart Portal DZ

📄 معاينة المستند

${displayContent}
`); previewWindow.document.close(); showStatus('success', '✅', 'تم فتح المعاينة!'); } // ====== عرض الحالة ====== function showStatus(type, icon, message) { const statusEl = document.getElementById('printStatus'); const iconEl = document.getElementById('printStatusIcon'); const textEl = document.getElementById('printStatusText'); statusEl.className = 'print-status show ' + type; iconEl.textContent = icon; textEl.textContent = message; } // ====== كشف الشبكة ====== function refreshNetwork() { const networkNameEl = document.getElementById('networkName'); const networkStatusEl = document.getElementById('networkStatus'); // محاولة كشف الشبكة عبر واجهة Network Information API if ('connection' in navigator) { const conn = navigator.connection; if (conn && conn.type) { networkNameEl.textContent = conn.type || 'غير معروف'; networkStatusEl.className = 'network-status connected'; networkStatusEl.textContent = '✅ متصل'; showStatus('success', '✅', 'تم تحديث معلومات الشبكة بنجاح'); } else { networkNameEl.textContent = 'غير معروف'; networkStatusEl.className = 'network-status unknown'; networkStatusEl.textContent = '❓ غير معروف'; } } else { // محاولة بديلة عبر كشف النطاق try { // محاولة جلب صفحة بسيطة للتحقق من الاتصال fetch('/') .then(() => { networkNameEl.textContent = 'متصل بالإنترنت'; networkStatusEl.className = 'network-status connected'; networkStatusEl.textContent = '✅ متصل'; }) .catch(() => { networkNameEl.textContent = 'غير متصل'; networkStatusEl.className = 'network-status disconnected'; networkStatusEl.textContent = '❌ غير متصل'; }); } catch (e) { networkNameEl.textContent = 'غير معروف'; networkStatusEl.className = 'network-status unknown'; networkStatusEl.textContent = '❓ غير معروف'; } } // تذكير المستخدم بتحديث الصفحة إذا تغيرت الشبكة showStatus('warning', '💡', 'إذا كنت قد غيرت شبكة Wi-Fi، أعد تحميل الصفحة (Refresh) لتحديث الاتصال.'); } // ====== التهيئة ====== document.addEventListener('DOMContentLoaded', function() { // استرجاع النص المحفوظ const savedContent = localStorage.getItem('smartPortalPrintContent'); if (savedContent) { document.getElementById('printContent').value = savedContent; } updatePreview(); // كشف الشبكة refreshNetwork(); // دعم اختصار Ctrl+Enter document.addEventListener('keydown', function(e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { const active = document.activeElement; if (active && (active.id === 'printContent' || active.id === 'printerIP')) { e.preventDefault(); printViaSystem(); } } }); });
Scroll to Top