## Summary - Translate Data Entry labels preview when language is Persian - Enable `dir=rtl` for FA (toast position mirrored) - Add badge `gap` so page counts show as `4 Page(s)` not `4Page(s)` ## Test plan - [ ] Data Entry in FA shows e.g. `گالن: 20 (5 صفحه) | کارتن: 5 (2 صفحه)` - [ ] Toggling language refreshes the preview line - [ ] Print Queue badges have space before Page(s)/صفحه - [ ] FA layout is RTL Reviewed-on: #7 Co-authored-by: Diaco Soltanpour <dyaco8464@yahoo.com> Co-committed-by: Diaco Soltanpour <dyaco8464@yahoo.com>
163 lines
5.8 KiB
JavaScript
163 lines
5.8 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
const productSelect = document.getElementById('product');
|
|
const dateInput = document.getElementById('prod-date');
|
|
const nextControlInput = document.getElementById('next-control');
|
|
const totalWeightInput = document.getElementById('total-weight');
|
|
const canisterWeightInput = document.getElementById('canister-weight');
|
|
const canistersPerBoxInput = document.getElementById('canisters-per-box');
|
|
const previewDisplay = document.getElementById('preview-display');
|
|
const form = document.getElementById('order-form');
|
|
|
|
// Default dates (local calendar — valueAsDate is UTC and shifts in +0330)
|
|
function localYmd(d) {
|
|
const y = d.getFullYear();
|
|
const m = String(d.getMonth() + 1).padStart(2, '0');
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
return `${y}-${m}-${day}`;
|
|
}
|
|
dateInput.value = localYmd(new Date());
|
|
|
|
// Auto calculate +6 months
|
|
function updateNextControl() {
|
|
const [y, m, day] = (dateInput.value || '').split('-').map(Number);
|
|
if (!y) return;
|
|
const d = new Date(y, m - 1, day);
|
|
d.setMonth(d.getMonth() + 6);
|
|
nextControlInput.value = localYmd(d);
|
|
}
|
|
updateNextControl();
|
|
dateInput.addEventListener('change', updateNextControl);
|
|
|
|
// Auto calculate previews
|
|
function updatePreview() {
|
|
const tw = parseFloat(totalWeightInput.value) || 0;
|
|
const cw = parseFloat(canisterWeightInput.value) || 1;
|
|
const cpb = parseInt(canistersPerBoxInput.value) || 1;
|
|
|
|
const cc = Math.ceil(tw / cw);
|
|
const cp = Math.ceil(cc / 4);
|
|
|
|
const bc = Math.ceil(cc / cpb);
|
|
const bp = Math.ceil(bc / 4);
|
|
|
|
const t = translations[currentLang] || translations.en;
|
|
previewDisplay.textContent =
|
|
`${t.canisters_unit}: ${cc} (${cp} ${t.preview_pages}) | ${t.boxes_unit}: ${bc} (${bp} ${t.preview_pages})`;
|
|
}
|
|
window.updateLabelsPreview = updatePreview;
|
|
|
|
// Listeners for updates
|
|
totalWeightInput.addEventListener('input', updatePreview);
|
|
canisterWeightInput.addEventListener('input', updatePreview);
|
|
canistersPerBoxInput.addEventListener('input', updatePreview);
|
|
updatePreview();
|
|
|
|
// Auto weight from product selection
|
|
productSelect.addEventListener('change', (e) => {
|
|
const opt = e.target.options[e.target.selectedIndex];
|
|
if (opt && opt.dataset.weight) {
|
|
canisterWeightInput.value = opt.dataset.weight;
|
|
updatePreview();
|
|
}
|
|
});
|
|
|
|
// Submit new order
|
|
form.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const payload = {
|
|
product_id: parseInt(productSelect.value),
|
|
batch_number: document.getElementById('batch').value,
|
|
production_date: dateInput.value,
|
|
next_control_date: nextControlInput.value,
|
|
total_weight: parseFloat(totalWeightInput.value),
|
|
canister_weight: parseFloat(canisterWeightInput.value),
|
|
canisters_per_box: parseInt(canistersPerBoxInput.value)
|
|
};
|
|
|
|
try {
|
|
const res = await fetch('/api/orders', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload)
|
|
});
|
|
|
|
if (res.ok) {
|
|
showToast(currentLang === 'fa' ? 'سفارش به صف چاپ اضافه شد' : 'Order added to print queue!');
|
|
form.reset();
|
|
dateInput.value = localYmd(new Date());
|
|
updateNextControl();
|
|
updatePreview();
|
|
setTimeout(() => window.location.reload(), 1000);
|
|
} else {
|
|
const err = await res.json();
|
|
console.error(err);
|
|
showToast('Error creating order', true);
|
|
}
|
|
} catch (err) {
|
|
showToast('Network error', true);
|
|
}
|
|
});
|
|
});
|
|
|
|
// Inline Product addition
|
|
function showAddProduct() {
|
|
document.getElementById('add-product-panel').style.display = 'block';
|
|
}
|
|
|
|
function hideAddProduct() {
|
|
document.getElementById('add-product-panel').style.display = 'none';
|
|
}
|
|
|
|
async function addProduct() {
|
|
const code = document.getElementById('new-code').value;
|
|
const desc = document.getElementById('new-desc').value;
|
|
const weight = parseFloat(document.getElementById('new-weight').value);
|
|
|
|
if (!code || !desc) {
|
|
showToast('Code and Description are required', true);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await fetch('/api/products', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ code, description: desc, default_net_weight: weight })
|
|
});
|
|
|
|
if (res.ok) {
|
|
const p = await res.json();
|
|
const select = document.getElementById('product');
|
|
const opt = document.createElement('option');
|
|
opt.value = p.id;
|
|
opt.dataset.weight = p.default_net_weight;
|
|
opt.textContent = `${p.code} — ${p.description}`;
|
|
select.appendChild(opt);
|
|
select.value = p.id;
|
|
document.getElementById('canister-weight').value = p.default_net_weight;
|
|
updatePreview();
|
|
|
|
hideAddProduct();
|
|
showToast('Product added');
|
|
} else {
|
|
showToast('Error adding product', true);
|
|
}
|
|
} catch (err) {
|
|
showToast('Network error', true);
|
|
}
|
|
}
|
|
|
|
async function deleteOrder(id) {
|
|
if (!confirm('Cancel this order?')) return;
|
|
|
|
try {
|
|
const res = await fetch(`/api/orders/${id}`, { method: 'DELETE' });
|
|
if (res.ok) {
|
|
window.location.reload();
|
|
}
|
|
} catch (err) {
|
|
showToast('Error', true);
|
|
}
|
|
}
|