/*
* Charme du Sud – WPForms-Zeitfeld: belegte Uhrzeiten anzeigen.
* Einbinden: WPBakery-Element "Roher HTML-Code" auf /tischreservierung/, Inhalt in ein script-Tag setzen.
* WICHTIG: In diesem Kommentar keine HTML-Tags schreiben (ein schließendes script-Tag beendet das Skript).
* Quelle: https://tischbelegung.charme-du-sud.de/api/public/slot-status?date=YYYY-MM-DD
* (liefert je Uhrzeit nur free/busy/full, keine Personenzahlen; gleiche Regel wie der WhatsApp-Bot)
* - "full": in der Zeitauswahl als "fast ausgebucht" markiert + freundlicher Hinweis (weiter anfragbar)
* - "busy": in der Zeitauswahl als "sehr gefragt" markiert + Hinweis mit entspannterer Uhrzeit
* Fällt der Endpoint aus, bleibt das Formular unverändert nutzbar.
* Grundsatz: Es wird NICHTS gesperrt – jede Uhrzeit bleibt anfragbar; das Team bestätigt
* oder schlägt per Mail ("Alternative Uhrzeit") eine andere Zeit vor.
* AUSNAHME Frühschließung (Knopf in der Mitarbeiter-App): Uhrzeiten ab der letzten Ankunft
* sind "closed" (in der Liste als "geschlossen" markiert, Auswahl wird zurückgesetzt).
* BANNER: Ist heute eine Frühschließung eingetragen, erscheint oben auf der Seite ein Hinweis.
* Dafür dasselbe Snippet zusätzlich als "Roher HTML-Code" auf die Startseite setzen
* (ohne Formular läuft dort nur das Banner).
* QUELLE: Die Seite liest den URL-Parameter q (z. B. ?q=meta, ?q=plakat, ?q=tisch, ?q=newsletter),
* bereinigt ihn (nur a-z, 0-9 und Bindestrich, max. 30 Zeichen) und schreibt ihn in das
* WPForms-Hidden-Field "Quelle" (Standardwert dort: direkt). Erkannt wird das Feld an diesem
* Standardwert. Kein Cookie, kein localStorage: der Wert reist nur mit dem Formular.
*/
(function () {
var ENDPOINT = 'https://tischbelegung.charme-du-sud.de/api/public/slot-status?date=';
var CLOSED_DAYS = 'https://tischbelegung.charme-du-sud.de/api/public/closed-days?days=1';
var DATE_SEL = 'input.wpforms-datepicker';
var TIME_SEL = 'input.wpforms-timepicker';
var status = {};
var frueh = null; // {ab, grund, letzte} für das gewählte Datum
function toIso(v) {
var m = String(v || '').match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
return m ? m[3] + '-' + m[2] + '-' + m[1] : '';
}
function pad(n) { return (n < 10 ? '0' : '') + n; }
function toMin(t) { var p = String(t || '').split(':'); return p.length >= 2 ? (+p[0]) * 60 + (+p[1]) : -1; }
function toHhmm(m) { return pad(Math.floor(m / 60)) + ':' + pad(m % 60); }
function suggestion(t) {
var base = toMin(t), offsets = [15, 30, 45, 60, -15, -30];
for (var i = 0; i < offsets.length; i++) {
var c = toHhmm(base + offsets[i]);
if (status[c] === 'free') return c;
}
return '';
}
function hintEl(timeInp) {
var id = timeInp.id + '-cds-hint', el = document.getElementById(id);
if (!el) {
el = document.createElement('div');
el.id = id;
el.className = 'cds-slot-hint';
el.setAttribute('aria-live', 'polite');
timeInp.parentNode.appendChild(el);
}
return el;
}
function updateHint(timeInp) {
var t = String(timeInp.value || '').slice(0, 5), s = status[t], el = hintEl(timeInp), sug = suggestion(t);
if (s === 'closed' && frueh) {
el.className = 'cds-slot-hint cds-full';
el.textContent = 'An diesem Tag haben wir ausnahmsweise nur bis ' + frueh.ab + ' Uhr geöffnet' +
(frueh.grund ? ' (' + frueh.grund + ')' : '') + '. Bitte wähl eine Ankunftszeit bis ' + frueh.letzte + ' Uhr.';
timeInp.value = '';
} else if (frueh && !t) {
el.className = 'cds-slot-hint cds-full';
el.textContent = 'Hinweis: An diesem Tag haben wir ausnahmsweise nur bis ' + frueh.ab + ' Uhr geöffnet' +
(frueh.grund ? ' (' + frueh.grund + ')' : '') + ' – letzte Ankunft ' + frueh.letzte + ' Uhr.';
} else if (s === 'full') {
el.className = 'cds-slot-hint cds-full';
el.textContent = 'Um ' + t + ' Uhr ist es bei uns leider schon sehr voll' +
(sug ? ' – um ' + sug + ' Uhr haben wir noch gut Platz' : '') +
'. Du kannst ' + t + ' Uhr trotzdem gerne anfragen – wir melden uns mit einer Bestätigung oder einem Alternativvorschlag.';
} else if (s === 'busy') {
el.className = 'cds-slot-hint';
el.textContent = 'Um ' + t + ' Uhr kommen bereits viele Gäste gleichzeitig an' +
(sug ? ' – um ' + sug + ' Uhr ist es entspannter' : '') +
'. Natürlich kannst du ' + t + ' Uhr trotzdem anfragen.';
} else {
el.className = 'cds-slot-hint';
el.textContent = '';
}
}
function decorateList() {
var items = document.querySelectorAll('.ui-timepicker-wrapper li');
Array.prototype.forEach.call(items, function (li) {
var t = String(li.textContent || '').replace(/[^\d:]/g, '').slice(0, 5);
var s = status[t];
li.classList.toggle('cds-busy', s === 'busy');
li.classList.toggle('cds-full', s === 'full');
li.classList.toggle('cds-closed', s === 'closed');
});
}
function load(dateInp, timeInp) {
var iso = toIso(dateInp.value);
status = {};
frueh = null;
if (!iso) { updateHint(timeInp); return; }
fetch(ENDPOINT + iso).then(function (r) { return r.json(); }).then(function (d) {
status = (d && d.slots) || {};
frueh = d && d.geschlossen_ab ? {ab: d.geschlossen_ab, grund: d.grund || '', letzte: d.letzte_ankunft || d.geschlossen_ab} : null;
decorateList();
updateHint(timeInp);
}).catch(function () { status = {}; updateHint(timeInp); });
}
function banner() {
if (document.getElementById('cds-frueh-banner')) return;
fetch(CLOSED_DAYS).then(function (r) { return r.json(); }).then(function (d) {
var now = new Date(), iso = now.getFullYear() + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate());
var e = d && d.early_closures && d.early_closures[iso];
if (!e || document.getElementById('cds-frueh-banner')) return;
var b = document.createElement('div');
b.id = 'cds-frueh-banner';
b.setAttribute('role', 'status');
b.textContent = '⏰ Heute haben wir ausnahmsweise nur bis ' + e.ab + ' Uhr geöffnet' +
(e.grund ? ' – ' + e.grund : '') + '. Wir freuen uns auf euren Besuch!';
document.body.insertBefore(b, document.body.firstChild);
}).catch(function () {});
}
function cleanQuelle(v) {
return String(v || '').toLowerCase().replace(/[\s_]+/g, '-').replace(/[^a-z0-9-]/g, '')
.replace(/-+/g, '-').replace(/^-+|-+$/g, '').slice(0, 30);
}
function setQuelle() {
var m = location.search.match(/[?&]q=([^]*)/), q = '';
if (!m) return;
try { q = cleanQuelle(decodeURIComponent(m[1].replace(/\+/g, ' '))); } catch (e) { q = ''; }
if (!q) return;
var inps = document.querySelectorAll('form.wpforms-form input[type="hidden"][name^="wpforms[fields]"]');
Array.prototype.forEach.call(inps, function (i) {
if (i.value === 'direkt' || i.getAttribute('data-cds-quelle')) {
i.value = q;
i.setAttribute('data-cds-quelle', '1');
}
});
}
function init() {
setQuelle();
var bcss = document.createElement('style');
bcss.textContent = '#cds-frueh-banner{background:#7f1d1d;color:#fff;text-align:center;padding:10px 16px;' +
'font-size:15px;line-height:1.4;position:relative;z-index:9999}';
document.head.appendChild(bcss);
banner();
var dateInp = document.querySelector(DATE_SEL), timeInp = document.querySelector(TIME_SEL);
if (!dateInp || !timeInp) return;
var css = document.createElement('style');
css.textContent =
'.ui-timepicker-wrapper li.cds-busy::after{content:" · sehr gefragt";color:#b45309;font-size:.85em}' +
'.ui-timepicker-wrapper li.cds-full::after{content:" · fast ausgebucht";color:#9a3412;font-size:.85em}' +
'.ui-timepicker-wrapper li.cds-closed{color:#a8a29e;text-decoration:line-through}' +
'.ui-timepicker-wrapper li.cds-closed::after{content:" · geschlossen";text-decoration:none;font-size:.85em}' +
'.cds-slot-hint{font-size:.9em;margin-top:4px;color:#92400e}' +
'.cds-slot-hint.cds-full{color:#9a3412}';
document.head.appendChild(css);
dateInp.addEventListener('change', function () { load(dateInp, timeInp); });
timeInp.addEventListener('change', function () { updateHint(timeInp); });
if (window.jQuery) {
jQuery(timeInp).on('showTimepicker', decorateList).on('changeTime', function () { updateHint(timeInp); });
}
if (dateInp.value) load(dateInp, timeInp);
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init); else init();
})();