    </main>
</div>
<script>
function uploadFile(inputId, targetFieldName) {
    var fileInput = document.getElementById(inputId);
    var statusEl = document.getElementById('upload_status_' + inputId);
    if (!fileInput.files.length) { alert('Select a file first'); return; }
    
    var formData = new FormData();
    formData.append('file', fileInput.files[0]);
    formData.append('csrf_token', document.querySelector('input[name="csrf_token"]').value);
    
    statusEl.textContent = 'Uploading...';
    statusEl.style.color = '#0d6efd';
    
    fetch('/admin/upload', { method: 'POST', body: formData })
        .then(r => r.json())
        .then(data => {
            if (data.success) {
                var targetField = document.querySelector('[name="' + targetFieldName + '"]');
                if (targetField) targetField.value = data.url;
                statusEl.textContent = 'Uploaded!';
                statusEl.style.color = '#198754';
            } else {
                statusEl.textContent = data.error || 'Upload failed';
                statusEl.style.color = '#dc3545';
            }
        })
        .catch(e => { statusEl.textContent = 'Error'; statusEl.style.color = '#dc3545'; });
}
</script>
</body>
</html>
