45 lines
1.8 KiB
JavaScript
45 lines
1.8 KiB
JavaScript
/* Customer-only presentation. Financial and image validation remains in receipt-form.js.
|
|
Object URLs preview local files without uploading or persisting them. */
|
|
(() => {
|
|
const form = document.querySelector('.customer-receipt-form');
|
|
if (!form) return;
|
|
const input = form.elements.namedItem('receipt_image');
|
|
const preview = document.querySelector('#receipt-file-preview');
|
|
const selection = document.querySelector('#receipt-file-selection');
|
|
const status = document.querySelector('#receipt-file-status');
|
|
let previewUrl = null;
|
|
function releasePreview() {
|
|
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
|
previewUrl = null;
|
|
preview.removeAttribute('src');
|
|
preview.hidden = true;
|
|
}
|
|
function updateFile() {
|
|
releasePreview();
|
|
const file = input.files[0];
|
|
selection.hidden = !file;
|
|
status.textContent = file ? file.name + ' · ' + new Intl.NumberFormat('fa-IR', {maximumFractionDigits: 1}).format(file.size / 1024) + ' کیلوبایت' : '';
|
|
if (file && /^image\/(jpeg|png|gif|webp)$/.test(file.type)) {
|
|
previewUrl = URL.createObjectURL(file);
|
|
preview.src = previewUrl;
|
|
preview.hidden = false;
|
|
}
|
|
}
|
|
input.addEventListener('change', updateFile);
|
|
document.querySelector('#receipt-clear-file').addEventListener('click', () => {
|
|
input.value = '';
|
|
input.dispatchEvent(new Event('change', {bubbles: true}));
|
|
input.focus();
|
|
});
|
|
window.addEventListener('pagehide', releasePreview);
|
|
const origin = form.elements.namedItem('origin_bank');
|
|
const other = form.elements.namedItem('origin_other');
|
|
function updateOrigin() {
|
|
const visible = origin.selectedOptions[0]?.textContent.trim() === 'سایر';
|
|
other.closest('[data-field]').hidden = !visible;
|
|
other.disabled = !visible;
|
|
}
|
|
origin.addEventListener('change', updateOrigin);
|
|
updateOrigin();
|
|
})();
|