diff --git a/app.js b/app.js new file mode 100644 index 0000000..9dc9e32 --- /dev/null +++ b/app.js @@ -0,0 +1,290 @@ +// ============================================================ +// π Hunde-Business Tracker β App-Logik +// ============================================================ + +"use strict"; + +/* ---------- Login (Token, Client-seitig, ohne Ambitions) ---------- */ + +function toHash(str) { + let h = 5381; + for (let i = 0; i < str.length; i++) h = ((h << 5) + h + str.charCodeAt(i)) >>> 0; + return h.toString(36); +} + +function saveSession() { + const exp = Date.now() + SESSION_HOURS * 3600 * 1000; + localStorage.setItem("hb_session", JSON.stringify({ h: toHash(APP_TOKEN), exp })); +} + +function hasValidSession() { + try { + const s = JSON.parse(localStorage.getItem("hb_session")); + return s && s.h === toHash(APP_TOKEN) && s.exp > Date.now(); + } catch { return false; } +} + +function showLogin() { + document.getElementById("login-screen").classList.remove("hidden"); + document.getElementById("app-screen").classList.add("hidden"); +} + +function showApp() { + document.getElementById("login-screen").classList.add("hidden"); + document.getElementById("app-screen").classList.remove("hidden"); + refreshList(); +} + +document.getElementById("login-btn").addEventListener("click", tryLogin); +document.getElementById("token-input").addEventListener("keydown", (e) => { + if (e.key === "Enter") tryLogin(); +}); + +function tryLogin() { + const val = document.getElementById("token-input").value.trim(); + const err = document.getElementById("login-error"); + if (val === APP_TOKEN) { + err.classList.add("hidden"); + saveSession(); + showApp(); + } else { + err.classList.remove("hidden"); + document.getElementById("token-input").value = ""; + } +} + +document.getElementById("logout-btn").addEventListener("click", () => { + localStorage.removeItem("hb_session"); + showLogin(); +}); + +/* ---------- Konsistenz-Slider (1β5) ---------- */ + +const KOT_LEVELS = { + 1: { emoji: "π", caption: "Perfekt! PreisverdΓ€chtig! π" }, + 2: { emoji: "π", caption: "Alles gut, nichts zu meckern π" }, + 3: { emoji: "π€", caption: "Etwas weich, aber noch okay π€" }, + 4: { emoji: "π", caption: "Ziemlich breiigβ¦ Durchfall-Alarm π" }, + 5: { emoji: "π¨", caption: "Voller Durchfall! Water bitte! π¨" }, +}; + +const URIN_LEVELS = { + 1: { emoji: "π§", caption: "Nur ein Tropfen π§" }, + 2: { emoji: "π¦", caption: "Kleine Menge π¦" }, + 3: { emoji: "π", caption: "Normale Menge π" }, + 4: { emoji: "π", caption: "Ziemlich viel π" }, + 5: { emoji: "π", caption: "Richtig viel! Wer war das? π" }, +}; + +let currentType = "kot"; // oder "urin" + +const kotSlider = document.getElementById("consistency"); +const kotEmoji = document.getElementById("consistency-emoji"); +const kotCaption = document.getElementById("consistency-caption"); +const diarrheaAlert = document.getElementById("diarrhea-alert"); + +const urinSlider = document.getElementById("urin-amount"); +const urinEmoji = document.getElementById("urin-emoji"); +const urinCaption = document.getElementById("urin-caption"); +const urinWrap = document.getElementById("urin-slider-wrap"); + +function renderKotSlider() { + const lvl = KOT_LEVELS[kotSlider.value]; + kotEmoji.textContent = lvl.emoji; + kotCaption.textContent = lvl.caption; + const isDiarrhea = Number(kotSlider.value) >= 4; + diarrheaAlert.classList.toggle("hidden", !isDiarrhea || currentType !== "kot"); +} + +function renderUrinSlider() { + const lvl = URIN_LEVELS[urinSlider.value]; + urinEmoji.textContent = lvl.emoji; + urinCaption.textContent = lvl.caption; +} + +kotSlider.addEventListener("input", renderKotSlider); +urinSlider.addEventListener("input", renderUrinSlider); + +document.querySelectorAll(".type-btn").forEach((btn) => { + btn.addEventListener("click", () => { + document.querySelectorAll(".type-btn").forEach((b) => b.classList.remove("selected")); + btn.classList.add("selected"); + currentType = btn.dataset.type; + const isKot = currentType === "kot"; + document.getElementById("consistency-label").textContent = "Konsistenz"; + document.getElementById("consistency-label").style.display = isKot ? "block" : "none"; + kotSlider.closest(".slider-row").style.display = isKot ? "flex" : "none"; + document.getElementById("consistency-caption").style.display = isKot ? "block" : "none"; + document.querySelector(".slider-scale").style.display = isKot ? "flex" : "none"; + urinWrap.classList.toggle("hidden", isKot); + diarrheaAlert.classList.toggle("hidden", !isKot || Number(kotSlider.value) < 4); + }); +}); + +/* ---------- Zeitstempel ---------- */ + +function nowLocalISO() { + const d = new Date(); + const pad = (n) => String(n).padStart(2, "0"); + return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`; +} + +function initTimestamp() { + document.getElementById("timestamp").value = nowLocalISO(); +} + +function formatTime(iso) { + const d = new Date(iso); + if (isNaN(d)) return iso; + return d.toLocaleString("de-DE", { + weekday: "short", day: "2-digit", month: "2-digit", + hour: "2-digit", minute: "2-digit", + }); +} + +/* ---------- Speichern ---------- */ + +document.getElementById("save-btn").addEventListener("click", async () => { + const ts = document.getElementById("timestamp").value || nowLocalISO(); + const note = document.getElementById("note").value.trim(); + + const entry = { + id: crypto.randomUUID(), + type: currentType, + ts, + note, + createdAt: Date.now(), + }; + if (currentType === "kot") entry.consistency = Number(kotSlider.value); + else entry.amount = Number(urinSlider.value); + + try { + await dbAdd(entry); + const fb = document.getElementById("save-feedback"); + fb.classList.remove("hidden"); + fb.textContent = currentType === "kot" ? "Eingetragen! π©β¨" : "Eingetragen! π¦β¨"; + setTimeout(() => fb.classList.add("hidden"), 2200); + + document.getElementById("note").value = ""; + initTimestamp(); + refreshList(); + } catch (err) { + alert("Speichern fehlgeschlagen: " + err); + } +}); + +/* ---------- Liste ---------- */ + +function entryIcon(e) { + if (e.type === "urin") return "π¦"; + return e.consistency >= 4 ? "π¨" : "π©"; +} + +function entryTitle(e) { + if (e.type === "urin") return `Pipi (Menge: ${e.amount}/5)`; + const labels = { 1: "Top-Form! π", 2: "Solide π", 3: "Weich π€", 4: "Breiig π", 5: "Durchfall π¨" }; + return `Kacka β ${labels[e.consistency] || "?"}`; +} + +async function refreshList() { + const list = document.getElementById("entry-list"); + const empty = document.getElementById("empty-list"); + const entries = (await dbGetAll()).sort((a, b) => (a.ts < b.ts ? 1 : -1)); + + list.innerHTML = ""; + empty.classList.toggle("hidden", entries.length > 0); + + for (const e of entries) { + const div = document.createElement("div"); + div.className = "entry"; + const noteHtml = e.note ? `