= 12 ? 'PM' : 'AM';
var h12 = h % 12; if (h12 === 0) h12 = 12;
return h12 + ':' + parts[1] + ' ' + suffix;
}
var extraServices = [];
var allServices = [];
Array.prototype.forEach.call(root.querySelectorAll('[data-rbbk-service] option[value]:not([value=""])'), function(opt) {
allServices.push({ id: opt.value, name: opt.textContent, price: parseInt(opt.dataset.price || '0', 10) });
});
var addServiceBtn = root.querySelector('[data-rbbk-add-service]');
var extraListEl = root.querySelector('[data-rbbk-extra-list]');
function availableExtraServices() {
var primaryEl = root.querySelector('[data-rbbk-service]');
var usedIds = extraServices.map(function(s) { return s.id; });
if (primaryEl && primaryEl.value) usedIds.push(primaryEl.value);
return allServices.filter(function(s) { return usedIds.indexOf(s.id) === -1; });
}
function updateAddBtnVisibility() {
if (!addServiceBtn) return;
addServiceBtn.style.display = availableExtraServices().length ? '' : 'none';
}
function renderExtraList() {
if (!extraListEl) return;
extraListEl.innerHTML = '';
extraServices.forEach(function(s, idx) {
var row = document.createElement('div');
row.className = 'rbbk-extra-row';
var label = document.createElement('span');
label.textContent = s.name;
row.appendChild(label);
var rm = document.createElement('button');
rm.type = 'button'; rm.className = 'rbbk-extra-remove'; rm.textContent = '×';
rm.addEventListener('click', function() {
extraServices.splice(idx, 1);
renderExtraList(); renderSummary(); updateAddBtnVisibility();
});
row.appendChild(rm);
extraListEl.appendChild(row);
});
}
if (addServiceBtn) {
addServiceBtn.addEventListener('click', function() {
var remaining = availableExtraServices();
if (!remaining.length) return;
var sel = document.createElement('select');
sel.className = 'rbbk-extra-select';
var placeholder = document.createElement('option');
placeholder.value = ''; placeholder.textContent = 'Select a service...';
sel.appendChild(placeholder);
remaining.forEach(function(s) {
var opt = document.createElement('option');
opt.value = s.id; opt.textContent = s.name;
sel.appendChild(opt);
});
addServiceBtn.style.display = 'none';
addServiceBtn.parentNode.insertBefore(sel, addServiceBtn);
sel.focus();
sel.addEventListener('change', function() {
var chosen = allServices.filter(function(s) { return s.id === sel.value; })[0];
sel.remove();
if (chosen) { extraServices.push(chosen); renderExtraList(); renderSummary(); }
updateAddBtnVisibility();
});
});
}
function summaryLine(name, price) {
var esc = name.replace(/&/g,'&').replace(/' + esc + '' + (price > 0 ? ' $' + (price/100).toFixed(2) + '' : '') + '
';
}
function renderSummary() {
var summary = root.querySelector('[data-rbbk-summary]');
var serviceEl = root.querySelector('[data-rbbk-service]');
var lines = [];
var total = 0, any = false;
if (serviceEl && serviceEl.selectedIndex > 0) {
var opt = serviceEl.options[serviceEl.selectedIndex];
var price = parseInt(opt.dataset.price || '0', 10);
total += price; any = true;
lines.push(summaryLine(opt.textContent, price));
}
extraServices.forEach(function(s) { total += s.price; any = true; lines.push(summaryLine(s.name, s.price)); });
Object.keys(customFieldAnswers).forEach(function(name) {
var a = customFieldAnswers[name];
if (!a.value) return;
any = true;
var escLabel = a.label.replace(/&/g,'&').replace(/' + escLabel + ': ' + escVal + '
');
});
if (selectedDate && selectedSlot) lines.push('