import { apiGet } from '../api.js'; import { canEdit, showToast } from '../app.js'; export async function exportPage() { const app = document.getElementById('app'); app.innerHTML = `
`; try { const data = await apiGet('questionnaires.php'); renderExport(data.questionnaires || []); } catch (e) { showToast(e.message, 'error'); document.getElementById('exportContent').innerHTML = `

${esc(e.message)}

`; } } function renderExport(questionnaires) { const container = document.getElementById('exportContent'); if (!questionnaires.length) { container.innerHTML = `

No questionnaires

Create questionnaires first to export data.

`; return; } const bundleBlock = canEdit() ? `

Export questionnaires (JSON)

Download all questionnaires with questions, answer options, and every translation. Use Import questionnaires on the dashboard to restore this file on another server.

` : ''; container.innerHTML = ` ${bundleBlock}

Select a questionnaire and click Export to download a CSV file with all client responses (filtered by your role's visibility).

${questionnaires.map(q => ` `).join('')}
Questionnaire Version State Questions Actions
${esc(q.name)} ${esc(q.version || '—')} ${esc(q.state || 'draft')} ${q.questionCount || 0} View Results
`; document.getElementById('exportBundleBtn')?.addEventListener('click', async () => { const btn = document.getElementById('exportBundleBtn'); btn.disabled = true; const prev = btn.textContent; btn.textContent = 'Preparing…'; try { const token = localStorage.getItem('qdb_token'); const res = await fetch('../api/export?bundle=1', { headers: { Authorization: `Bearer ${token}` }, }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(err.error?.message || err.error || 'Export failed'); } const blob = await res.blob(); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; const stamp = new Date().toISOString().slice(0, 10); a.download = `questionnaires_bundle_${stamp}.json`; a.click(); URL.revokeObjectURL(url); showToast('Questionnaire bundle downloaded', 'success'); } catch (e) { showToast(e.message, 'error'); } finally { btn.disabled = false; btn.textContent = prev; } }); container.querySelectorAll('.export-btn').forEach(btn => { btn.addEventListener('click', async () => { btn.disabled = true; btn.textContent = 'Downloading...'; try { const token = localStorage.getItem('qdb_token'); const res = await fetch(`../api/export.php?questionnaireID=${encodeURIComponent(btn.dataset.id)}&format=csv`, { headers: { 'Authorization': `Bearer ${token}` } }); if (!res.ok) { const err = await res.json().catch(() => ({ error: 'Download failed' })); throw new Error(err.error); } const blob = await res.blob(); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${btn.dataset.name}_v${btn.dataset.version}.csv`; a.click(); URL.revokeObjectURL(url); showToast('Download started', 'success'); } catch (e) { showToast(e.message, 'error'); } finally { btn.disabled = false; btn.textContent = 'Export CSV'; } }); }); } function esc(s) { const d = document.createElement('div'); d.textContent = s ?? ''; return d.innerHTML; }