110 lines
7.5 KiB
HTML
110 lines
7.5 KiB
HTML
<!doctype html>
|
|
<html lang="ru" data-look="a"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<meta name="robots" content="noindex">
|
|
<title>Правка данных</title>
|
|
<script>{const l=new URLSearchParams(location.search).get("look");if(/^[abc]$/.test(l||""))document.documentElement.dataset.look=l}</script>
|
|
<link rel="stylesheet" href="/style.css"></head><body>
|
|
<header><h1 data-i="admTitle"></h1><div class="langs"><button data-l="ru" class="on" aria-pressed="true" lang="ru" aria-label="Русский" title="Русский">RU</button><button data-l="kk" aria-pressed="false" lang="kk" aria-label="Қазақша" title="Қазақша">KK</button><button data-l="en" aria-pressed="false" lang="en" aria-label="English" title="English">EN</button></div><a class="back" href="/"><span aria-hidden="true">←</span> <span data-i="admBack"></span></a></header>
|
|
<main>
|
|
<div class="hero"><h2 class="headline" data-i="admTitle"></h2><p class="lead" data-i="admIntro"></p><p class="lead" data-i="admAutoDate"></p>
|
|
<div class="bar"><label class="switch"><input type="checkbox" id="onlyHidden"><span data-i="admOnlyHidden"></span></label><span id="count" class="lead count"></span></div></div>
|
|
<p id="saved" class="msg" hidden data-i="admSaved"></p>
|
|
<div id="list"></div>
|
|
<p id="fail" class="msg" 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); b.setAttribute("aria-pressed", String(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", "badge " + (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);
|
|
const field = (label, input) => { const l = el("label", "lbl", label); l.append(input); return l; };
|
|
|
|
function card(r) {
|
|
// общий стиль карточки (DIK-75): .card .body, подписи .lbl над полями, кнопки .btn / .btn.alt
|
|
const box = el("div", "card form" + (hidden(r) ? " hidden" : "")), c = el("div", "body"); box.append(c);
|
|
c.append(el("h2", "", r.name), el("div", "meta", 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", "btn alt", t.admToday); nowBtn.type = "button"; nowBtn.onclick = () => (checked.value = today());
|
|
const row = el("div", "row"); row.append(checked, nowBtn);
|
|
const g = el("div", "fields");
|
|
g.append(field(t.admPrice, price), field(t.admPromo, promo), field(t.admUntil, until), field(t.admChecked, row));
|
|
const save = el("button", "btn", 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", "fields"); 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", "btn", 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 box;
|
|
}
|
|
|
|
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>
|