Files
product-slip/slips/static/slips/js/print.js
2026-10-07 08:16:16 +03:30

71 lines
2.6 KiB
JavaScript

function printOrder(id) {
// Cache-bust so a second print of the same order is not a stale HTML document
const win = window.open(`/print-page/${id}/?t=${Date.now()}`, '_blank');
if (win) {
win.focus();
} else {
showToast('Please allow popups to print', true);
}
}
async function markDone(id) {
try {
const res = await fetch(`/api/orders/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status: 'done' })
});
if (res.ok) {
showToast(currentLang === 'fa' ? 'سفارش به عنوان چاپ‌شده علامت خورد' : 'Order marked as done');
document.getElementById(`order-${id}`).style.display = 'none';
setTimeout(() => window.location.reload(), 1500);
} else {
showToast(currentLang === 'fa' ? 'خطا در به‌روزرسانی وضعیت' : 'Error updating status', true);
}
} catch (err) {
showToast('Network error', true);
}
}
async function undoDone(id) {
try {
const res = await fetch(`/api/orders/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status: 'pending' })
});
if (res.ok) {
showToast(currentLang === 'fa' ? 'سفارش به صف چاپ بازگشت' : 'Order returned to print queue');
setTimeout(() => window.location.reload(), 1000);
} else {
showToast(currentLang === 'fa' ? 'خطا در بازگردانی' : 'Error undoing status', true);
}
} catch (err) {
showToast('Network error', true);
}
}
// Auto-refresh pending queue every 10 seconds
setInterval(async () => {
try {
const res = await fetch('/api/orders?status=pending');
if (res.ok) {
const orders = await res.json();
document.getElementById('pending-count').textContent = orders.length;
// Simple approach: if count changes, just reload to get clean state
// In a larger app, we'd do smart DOM updates here
const currentItems = document.querySelectorAll('.queue-item:not([style*="display: none"])').length;
if (orders.length > currentItems) {
// New order arrived!
showToast(currentLang === 'fa' ? 'سفارش جدید دریافت شد!' : 'New order arrived!');
setTimeout(() => window.location.reload(), 2000);
}
}
} catch (err) {
console.log('Refresh failed silently');
}
}, 10000);