dika/admin/index.html

142 lines
9.5 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="ru"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="robots" content="noindex">
<title>Правка данных</title>
<style>
:root{--bg:#fff8f0;--ink:#2b2118;--mute:#7a6a5c;--accent:#e8590c;--ok:#2f9e44;--bad:#c92a2a;--card:#fff;--line:#f1e3d3}
@media (prefers-color-scheme:dark){:root{--bg:#1d1712;--ink:#f6ece2;--mute:#b3a293;--card:#2a2119;--line:#3b2f24}}
*{box-sizing:border-box}
body{font:16px/1.4 system-ui,sans-serif;margin:0;background:var(--bg);color:var(--ink)}
header{display:flex;align-items:center;gap:12px;padding:12px 16px;background:var(--accent);color:#fff;flex-wrap:wrap}
header h1{font-size:19px;margin:0;flex:1}
header a,header button{color:#fff;background:none;border:1px solid #fff8;border-radius:8px;padding:6px 10px;font:inherit;text-decoration:none;cursor:pointer}
header button.on{background:#fff3}
main{max-width:900px;margin:0 auto;padding:16px}
.bar{display:flex;gap:16px;align-items:center;flex-wrap:wrap;margin:12px 0;font-weight:600}
.card{background:var(--card);border:1px solid var(--line);border-radius:14px;padding:14px;margin:12px 0}
.card.hidden{border-color:var(--bad)}
.card h2{font-size:17px;margin:0}.card .addr{color:var(--mute);font-size:14px;margin-bottom:8px}
.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(170px,1fr));gap:10px;margin-top:8px}
label{display:block;font-size:13px;color:var(--mute)}
input,textarea{width:100%;padding:10px;margin-top:4px;border:1px solid var(--line);border-radius:10px;font:inherit;background:var(--bg);color:var(--ink)}
textarea{resize:vertical}
.row{display:flex;gap:6px}.row button{margin-top:4px}
button.b{padding:10px 14px;border:0;border-radius:10px;background:var(--accent);color:#fff;font:inherit;font-weight:700;cursor:pointer}
button.s{padding:6px 10px;border:1px solid var(--line);border-radius:10px;background:none;color:var(--ink);font:inherit;cursor:pointer}
.st{display:flex;gap:8px;flex-wrap:wrap;margin:8px 0}
.pill{padding:3px 10px;border-radius:20px;font-size:13px;font-weight:600;border:1px solid var(--line)}
.pill.ok{color:var(--ok);border-color:var(--ok)}.pill.bad{color:var(--bad);border-color:var(--bad)}
.foot{display:flex;gap:12px;align-items:center;margin-top:10px;font-size:14px}
.chk{font-weight:600;margin-bottom:6px}.hint{font-size:14px;margin-bottom:6px}
.ok-t{color:var(--ok)}.bad-t{color:var(--bad)}
</style></head><body>
<header><h1 data-i="admTitle"></h1><a href="/" data-i="admBack"></a>
<button data-l="ru" lang="ru" aria-label="Русский" title="Русский">RU</button><button data-l="kk" lang="kk" aria-label="Қазақша" title="Қазақша">KK</button><button data-l="en" lang="en" aria-label="English" title="English">EN</button></header>
<main>
<p data-i="admIntro"></p><p data-i="admAutoDate" style="color:var(--mute);font-size:14px"></p>
<div class="bar"><label for="onlyHidden" style="display:flex;gap:8px;align-items:center;font-size:16px;color:inherit"><input type="checkbox" id="onlyHidden" style="width:22px;margin:0"><span data-i="admOnlyHidden"></span></label><span id="count"></span></div>
<p id="saved" class="ok-t" hidden data-i="admSaved"></p>
<div id="list"></div>
<p id="fail" class="bad-t" hidden data-i="admLoadError"></p>
</main>
<script type="module">
const $ = (id) => document.getElementById(id);
let t = {}, rows = [];
const today = () => new Date(Date.now() + 5 * 3600000).toISOString().slice(0, 10);
const el = (tag, cls, text) => { const e = document.createElement(tag); if (cls) e.className = cls; if (text !== undefined) e.textContent = text; return e; };
const api = (url, opt) => fetch(url, opt);
async function setLang(l) {
t = await (await fetch(`/i18n/${l}.json`)).json();
try { localStorage.setItem("lang", l); } catch {}
document.documentElement.lang = l; document.title = t.admTitle;
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
document.querySelectorAll("[data-l]").forEach((b) => b.classList.toggle("on", b.dataset.l === l));
render();
}
const hidden = (r) => ["stale", "expired"].includes(r.status.lunch) || ["stale", "expired"].includes(r.status.promo);
const pill = (text, bad) => el("span", "pill " + (bad ? "bad" : "ok"), text);
const statusText = (s, none) => ({ none, active: t.admActive, stale: t.admHiddenStale, expired: t.admHiddenExpired })[s];
const fmtSrc = (tpl, src) => tpl.replace("{source}", src);
let fieldN = 0;
// Подпись привязана к полю через for/id; у поля даты рядом кнопка «Сегодня», поэтому поле ищем внутри
const field = (label, input) => {
const ctl = input.matches("input,select,textarea") ? input : input.querySelector("input,select,textarea");
ctl.id = "f" + ++fieldN; ctl.setAttribute("aria-label", label);
const l = el("label", "", label); l.htmlFor = ctl.id; l.append(input); return l;
};
function card(r) {
const c = el("div", "card" + (hidden(r) ? " hidden" : ""));
c.append(el("h2", "", r.name), el("div", "addr", r.address));
const fmt = (d) => d.split("-").reverse().join(".");
c.append(el("div", "chk", r.checked ? t.admCheckedOn.replace("{date}", fmt(r.checked)) : t.admNeverChecked));
if (hidden(r)) c.append(el("div", "hint bad-t", t.admHiddenHint));
const st = el("div", "st");
st.append(pill(`${t.admLunchLabel}: ${statusText(r.status.lunch, t.admNoLunch)}`, ["stale"].includes(r.status.lunch)),
pill(`${t.admPromoLabel}: ${statusText(r.status.promo, t.admNoPromo)}`, ["stale", "expired"].includes(r.status.promo)));
const price = el("input"); price.type = "number"; price.min = "1"; price.step = "1"; price.inputMode = "numeric";
if (r.lunch) price.value = r.lunch.price; else price.disabled = true;
const promo = el("input"); promo.type = "text"; promo.value = r.promo?.text || "";
const until = el("input"); until.type = "date"; until.value = r.promo?.until || "";
const checked = el("input"); checked.type = "date"; checked.value = r.checked || ""; checked.max = today();
const nowBtn = el("button", "s", t.admToday); nowBtn.type = "button"; nowBtn.onclick = () => (checked.value = today());
const row = el("div", "row"); row.append(checked, nowBtn);
const g = el("div", "grid");
g.append(field(t.admPrice, price), field(t.admPromo, promo), field(t.admUntil, until), field(t.admChecked, row));
const save = el("button", "b", t.admSave), msg = el("span");
save.onclick = async () => {
msg.className = ""; msg.textContent = "";
const changed = (r.lunch && Number(price.value) !== r.lunch.price) || promo.value !== (r.promo?.text || "") || until.value !== (r.promo?.until || "");
const body = { promo: promo.value, promoUntil: until.value, checked: changed && checked.value === (r.checked || "") ? today() : checked.value };
if (r.lunch) body.price = price.value;
const res = await api(`/admin/api/venues/${encodeURIComponent(r.id)}`, { method: "POST", body: JSON.stringify(body) }).catch(() => null);
if (res?.ok) { await reload(); $("saved").hidden = false; scrollTo(0, 0); return; }
const e = res ? (await res.json().catch(() => ({}))).error : "";
msg.className = "bad-t";
msg.textContent = { bad_price: t.admErrPrice, bad_date: t.admErrDate, future_date: t.admErrFuture }[e] || t.admErrOther;
};
const foot = el("div", "foot"); foot.append(save, msg);
// Описание и фото: отдельное сохранение, дату проверки цен оно не трогает
const desc = el("textarea"); desc.rows = 3; desc.maxLength = 300; desc.value = r.description;
const photo = el("input"); photo.type = "url"; photo.inputMode = "url"; photo.value = r.photo;
const g2 = el("div", "grid"); g2.append(field(t.admDescription, desc), field(t.admPhoto, photo));
const srcs = el("div", "hint");
const srcText = (s) => (s === "владелец" ? t.admSrcOwner : s);
srcs.textContent = [r.description ? fmtSrc(t.admDescSource, srcText(r.descriptionSource)) : t.admNoDescription,
r.photo ? fmtSrc(t.admPhotoSource, srcText(r.photoSource)) : t.admNoPhoto].join(" · ");
const save2 = el("button", "b", t.admSaveText), msg2 = el("span");
save2.onclick = async () => {
msg2.className = ""; msg2.textContent = "";
const res = await api(`/admin/api/venues/${encodeURIComponent(r.id)}`, { method: "POST", body: JSON.stringify({ description: desc.value, photo: photo.value }) }).catch(() => null);
if (res?.ok) { await reload(); $("saved").hidden = false; scrollTo(0, 0); return; }
const e = res ? (await res.json().catch(() => ({}))).error : "";
msg2.className = "bad-t";
msg2.textContent = { bad_description: t.admErrDescription, bad_photo: t.admErrPhoto }[e] || t.admErrOther;
};
const foot2 = el("div", "foot"); foot2.append(save2, msg2);
c.append(st, g, foot, g2, srcs, foot2);
return c;
}
function render() {
if (!t.admTitle) return;
const hid = rows.filter(hidden);
$("count").textContent = t.admHiddenCount.replace("{n}", hid.length);
const out = $("list"); out.textContent = "";
for (const r of $("onlyHidden").checked ? hid : rows) out.append(card(r));
}
async function reload() {
$("saved").hidden = true;
const res = await api("/admin/api/venues").catch(() => null);
$("fail").hidden = Boolean(res?.ok);
if (res?.ok) { rows = await res.json(); render(); }
}
$("onlyHidden").onchange = render;
document.querySelectorAll("[data-l]").forEach((b) => (b.onclick = () => setLang(b.dataset.l)));
await reload();
let l = "ru"; try { l = localStorage.getItem("lang") || "ru"; } catch {}
await setLang(["ru", "kk", "en"].includes(l) ? l : "ru");
</script>
</body></html>