sdelay-sayt-dlya-razmescheni/state.js

90 lines
2.1 KiB
JavaScript

const DIRECTIONS = ["ОДС", "ДРБ", "ДКБ"];
const STATUSES = {
active: { label: "Активен", tone: "ok" },
paused: { label: "Приостановлен", tone: "warn" },
completed: { label: "Завершён", tone: "muted" },
planned: { label: "Запланирован", tone: "info" },
};
const state = {
items: [],
log: [],
dir: "all",
status: "all",
dirCounts: {},
dirAmounts: {},
};
function esc(v) {
return String(v ?? "").replace(/[&<>"']/g, (c) => (
{ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]
));
}
function fmtMoney(n) {
return new Intl.NumberFormat("ru-RU").format(Number(n) || 0) + " ₸";
}
function parseDDMMYYYY(s) {
const m = String(s || "").match(/^(\d{2})\.(\d{2})\.(\d{4})$/);
if (!m) return null;
return new Date(Number(m[3]), Number(m[2]) - 1, Number(m[1]));
}
function fmtDate(s) {
const d = parseDDMMYYYY(s);
return d ? s : s;
}
function periodOf(it) {
if (!it.startDate || !it.endDate) return "";
return it.startDate + " — " + it.endDate;
}
function todayLabel() {
return new Date().toLocaleDateString("ru-RU");
}
function statusPill(status) {
const s = STATUSES[status] || { label: status, tone: "muted" };
return '<span class="kt-ai-status-pill" data-status="' + s.tone + '">' + esc(s.label) + "</span>";
}
function dirChip(dir) {
const tone = dir === "ОДС" ? "blue" : dir === "ДРБ" ? "purple" : "orange";
return '<span class="kt-ai-chip" data-tone="' + tone + '">' + esc(dir) + "</span>";
}
async function j(url, opts) {
const r = await fetch(url, opts);
const b = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(b.error || "Ошибка запроса (" + r.status + ")");
return b;
}
async function apiLoad() {
const d = await j("api/items");
state.items = d.items || [];
state.log = d.log || [];
}
function apiAdd(b) {
return j("api/items", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(b),
});
}
function apiUpdate(b) {
return j("api/items/update", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(b),
});
}
function apiDelete(id) {
return j("api/items?id=" + id, { method: "DELETE" });
}