/* global usScheduler */ (function () { 'use strict'; const app = document.getElementById('us-group-app'); if (!app) return; const list = document.getElementById('us-group-list'); const confirm = document.getElementById('us-group-confirmation'); const errorBox = document.getElementById('us-group-error'); const { restUrl, nonce } = usScheduler; function apiFetch(path, options = {}) { return fetch(restUrl + path, { ...options, headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': nonce, ...(options.headers || {}), }, }).then(async (res) => { const data = await res.json(); if (!res.ok) throw new Error(data.message || 'Request failed'); return data; }); } function showError(message) { errorBox.textContent = message; errorBox.style.display = 'block'; } function clearError() { errorBox.style.display = 'none'; } function escHtml(str) { return String(str) .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } function questionField(q) { const name = `q_${q.id}`; const required = q.is_required ? 'required' : ''; let input; if (q.field_type === 'textarea') { input = ``; } else if (q.field_type === 'select') { const opts = (q.options || []).map((o) => ``).join(''); input = ``; } else if (q.field_type === 'checkbox') { input = ``; } else { input = ``; } return `

`; } function policyField(p) { return `

${escHtml(p.title)}

${p.body || ''}
`; } function renderClasses(offerings) { const groups = offerings.filter((o) => o.kind === 'group_class'); if (!groups.length) { list.innerHTML = '

No group classes are open for enrolment right now.

'; return; } list.innerHTML = groups.map((o) => `

${escHtml(o.title)}

${o.schedule_note ? `

${escHtml(o.schedule_note)}

` : ''} ${o.description ? `

${escHtml(o.description)}

` : ''}

${escHtml(Number(o.price).toFixed(2))} ${escHtml(o.currency)}

`).join(''); list.querySelectorAll('.us-enrol-btn').forEach((btn) => { const offering = groups.find((o) => String(o.id) === btn.dataset.offeringId); btn.addEventListener('click', () => openEnrolment(offering)); }); } function openEnrolment(offering) { clearError(); Promise.all([ apiFetch(`offerings/${offering.id}/questions`), apiFetch('policies?scope=booking'), ]) .then(([questions, policies]) => renderEnrolment(offering, questions, policies)) .catch((err) => showError(err.message)); } function renderEnrolment(offering, questions, policies) { list.innerHTML = `

${escHtml(offering.title)}

${questions.map(questionField).join('')} ${policies.map(policyField).join('')}

`; document.getElementById('us-group-cancel').addEventListener('click', loadClasses); document.getElementById('us-enrol-form').addEventListener('submit', (e) => { e.preventDefault(); submitEnrolment(e.target, offering, questions); }); } function submitEnrolment(form, offering, questions) { clearError(); const answers = {}; questions.forEach((q) => { const field = form.elements[`q_${q.id}`]; if (!field) return; answers[q.id] = field.type === 'checkbox' ? (field.checked ? '1' : '0') : field.value; }); const accepted = [...form.querySelectorAll('.us-policy-accept:checked')].map((c) => Number(c.value)); apiFetch('enrollments', { method: 'POST', body: JSON.stringify({ offering_id: offering.id, answers, accepted_policy_version_ids: accepted, }), }) .then(() => { list.style.display = 'none'; confirm.style.display = 'block'; }) .catch((err) => showError(err.message)); } function loadClasses() { clearError(); list.style.display = 'block'; confirm.style.display = 'none'; apiFetch('offerings?kind=group_class') .then(renderClasses) .catch((err) => showError(err.message)); } loadClasses(); }());