/* Мой день — личная панель подростка.
Отмечать: уроки (отметка 2–5), спорт, дела по дому, режим дня.
Прогресс пересчитывается, состояние хранится в localStorage по дате. */
const GRADE_POINTS = { 5: 10, 4: 7, 3: 4, 2: 0 };
const LESSONS = [
{ id: "math", name: "Математика", sub: "Алгебра, параграф 12" },
{ id: "phy", name: "Физика", sub: "Лабораторная работа" },
{ id: "rus", name: "Русский язык", sub: "Сочинение-размышление" },
{ id: "his", name: "История", sub: "Датский период" },
{ id: "eng", name: "Английский", sub: "Speaking, unit 7" }
];
const SPORT = [
{ id: "training", name: "Дзюдо", sub: "Секция, 1,5 часа", pts: 10, done: false },
{ id: "phys", type: "group", name: "Физические занятия", sub: "Подтягивания и отжимания, 3 подхода", pts: 10, children: [
{ id: "pullups", name: "Подтягивания", sub: "3 подхода" },
{ id: "pushups", name: "Отжимания", sub: "3 подхода" }
] },
{ id: "walk", name: "Активность", sub: "Прогулка 6000+ шагов", pts: 5, done: false },
{ id: "sleep", name: "Сон", sub: "Выспался ≥ 8 часов", pts: 5, done: false }
];
const HOME = [
{ id: "clean", name: "Уборка комнаты", sub: "Личное пространство", pts: 5, done: false },
{ id: "dishes", name: "Помыть посуду", sub: "После ужина", pts: 3, done: false },
{ id: "laundry", name: "Постирать", sub: "Загрузить стиралку", pts: 3, done: false }
];
const ROUTINE = [
{ id: "wake", time: "7:00", name: "Подъём и утро", sub: "Зарядка, завтрак", icon: "sun", pts: 2, done: false },
{ id: "school", time: "8:30", name: "Уроки в школе", sub: "5 уроков", icon: "book", pts: 5, done: false },
{ id: "homework", time: "12:00", name: "Домашние задания", sub: "Сдать уроки", icon: "clipboard", pts: 5, done: false },
{ id: "sport", time: "16:00", name: "Спорт", sub: "Дзюдо", icon: "trophy", pts: 5, done: false },
{ id: "rest", time: "19:30", name: "Отдых и вечер", sub: "Чтение, хобби", icon: "moon", pts: 2, done: false },
{ id: "sleep", time: "22:30", name: "Сон", sub: "Отбой", icon: "moon", pts: 10, done: false }
];
const GRADE_LABELS = { 5: "Отлично", 4: "Хорошо", 3: "Удовл." };
const STORE_KEY = "myn-day";
let lessons = LESSONS.map((l) => ({ ...l, grade: 0 }));
let sport = SPORT.map((s) => ({ ...s }));
let home = HOME.map((t) => ({ ...t }));
let routine = ROUTINE.map((r) => ({ ...r }));
function todayStr() {
return new Date().toISOString().slice(0, 10);
}
function save() {
try {
localStorage.setItem(STORE_KEY, JSON.stringify({ day: todayStr(), lessons, sport, home, routine }));
} catch (e) {}
}
function load() {
let raw = null;
try { raw = localStorage.getItem(STORE_KEY); } catch (e) {}
if (!raw) return;
try {
const d = JSON.parse(raw);
if (!d || d.day !== todayStr()) return;
if (Array.isArray(d.lessons)) lessons = d.lessons;
if (Array.isArray(d.sport)) sport = d.sport.map((s) => {
const tmpl = SPORT.find((x) => x.id === s.id) || {};
return { ...s, pts: tmpl.pts, done: !!s.done, children: tmpl.children ? (s.children ? s.children.map((c) => ({ ...c, done: !!c.done })) : tmpl.children.map((c) => ({ ...c }))) : undefined };
});
if (Array.isArray(d.home)) home = d.home.map((t) => ({ ...t, pts: (HOME.find((x) => x.id === t.id) || {}).pts, done: !!t.done }));
if (Array.isArray(d.routine)) routine = d.routine.map((r) => ({ ...r, pts: (ROUTINE.find((x) => x.id === r.id) || {}).pts, done: !!r.done }));
} catch (e) {}
}
function esc(s) {
return String(s).replace(/[&<>"']/g, (c) => ({
"&": "&", "<": "<", ">": ">", '"': """, "'": "'"
}[c]));
}
function el(html) {
const t = document.createElement("template");
t.innerHTML = html.trim();
return t.content.firstElementChild;
}
// ---------- Рендер ----------
function renderDate() {
const d = new Date();
const label = d.toLocaleDateString("ru-RU", { weekday: "long", day: "numeric", month: "long" });
document.getElementById("date-label").textContent = label[0].toUpperCase() + label.slice(1);
}
function renderLessons() {
const box = document.getElementById("lessons-list");
box.innerHTML = "";
lessons.forEach((l) => {
const pts = l.grade ? GRADE_POINTS[l.grade] : 0;
const row = el(`
${esc(l.name)}
${esc(l.sub)}
${l.grade ? `
${GRADE_LABELS[l.grade]}` : ""}
+${pts}
${[5,4,3,2].map((g) => ``).join("")}
`);
box.appendChild(row);
});
}
function renderSport() {
syncSportGroups();
const box = document.getElementById("sport-list");
box.innerHTML = "";
sport.forEach((s) => {
if (s.type === "group") {
const doneCount = s.children.filter((c) => c.done).length;
const row = el(`
${esc(s.name)}
${esc(s.sub)} · ${doneCount}/${s.children.length}
+${s.pts}
`);
box.appendChild(row);
s.children.forEach((c) => {
const childRow = el(`
`);
box.appendChild(childRow);
});
return;
}
const row = el(`
${esc(s.name)}
${esc(s.sub)}
+${s.pts}
`);
box.appendChild(row);
});
}
function renderHome() {
const box = document.getElementById("home-list");
box.innerHTML = "";
home.forEach((t) => {
const row = el(`
`);
box.appendChild(row);
});
}
function renderRoutine() {
const box = document.getElementById("routine-list");
box.innerHTML = "";
routine.forEach((r) => {
const row = el(`
${esc(r.time)}
${esc(r.name)}
${esc(r.sub)}
+${r.pts}
`);
box.appendChild(row);
});
}
function syncSportGroups() {
sport.forEach((s) => {
if (s.type !== "group" || !s.children) return;
s.done = s.children.every((c) => c.done);
});
}
// ---------- Прогресс ----------
function counts() {
const ld = lessons.filter((l) => l.grade > 0).length;
const sd = sport.filter((s) => s.done).length;
const hd = home.filter((t) => t.done).length;
const rd = routine.filter((r) => r.done).length;
return { lessons: [ld, lessons.length], sport: [sd, sport.length], home: [hd, home.length], routine: [rd, routine.length] };
}
function setPair(textId, barId, done, total) {
document.getElementById(textId).innerHTML = `0/0`;
const c = done, t = total;
document.getElementById(textId).innerHTML = `${t}/${c}`;
const pct = t ? Math.round((c / t) * 100) : 0;
document.getElementById(barId).style.width = pct + "%";
}
function points() {
const lessonPts = lessons.reduce((a, l) => a + (GRADE_POINTS[l.grade] || 0), 0);
const sportPts = sport.reduce((a, s) => a + (s.done ? s.pts : 0), 0);
const homePts = home.reduce((a, t) => a + (t.done ? t.pts : 0), 0);
const routinePts = routine.reduce((a, r) => a + (r.done ? r.pts : 0), 0);
return lessonPts + sportPts + homePts + routinePts;
}
function renderProgress() {
const c = counts();
document.getElementById("kpi-points").textContent = points();
document.getElementById("kpi-lessons").textContent = `${c.lessons[0]} / ${c.lessons[1]}`;
document.getElementById("kpi-sport").textContent = `${c.sport[0]} / ${c.sport[1]}`;
document.getElementById("kpi-home").textContent = `${c.home[0]} / ${c.home[1]}`;
document.getElementById("kpi-routine").textContent = `${c.routine[0]} / ${c.routine[1]}`;
setPair("cnt-lessons", "bar-lessons", c.lessons[0], c.lessons[1]);
setPair("cnt-sport", "bar-sport", c.sport[0], c.sport[1]);
setPair("cnt-home", "bar-home", c.home[0], c.home[1]);
setPair("cnt-routine", "bar-routine", c.routine[0], c.routine[1]);
const total = c.lessons[1] + c.sport[1] + c.home[1] + c.routine[1];
const done = c.lessons[0] + c.sport[0] + c.home[0] + c.routine[0];
const pct = total ? Math.round((done / total) * 100) : 0;
document.getElementById("summary-count").textContent = `${done} из ${total}`;
document.getElementById("ring-val").textContent = pct + "%";
const R = 36, C = 2 * Math.PI * R;
const fg = document.getElementById("ring-fg");
fg.style.strokeDasharray = C;
fg.style.strokeDashoffset = C * (1 - done / (total || 1));
}
function renderAll() {
renderLessons();
renderSport();
renderHome();
renderRoutine();
renderProgress();
}
// ---------- Действия ----------
document.addEventListener("click", (e) => {
if (e.target.closest("#sport-list")) e.stopPropagation();
const task = e.target.closest("#home-list .task");
if (task) {
const item = home.find((t) => t.id === task.dataset.id);
if (item) { item.done = !item.done; }
renderAll(); save();
return;
}
const subTask = e.target.closest('#sport-list .task[data-child-of]');
if (subTask) {
const g = sport.find((s) => s.id === subTask.dataset.childOf);
if (g) {
const c = g.children.find((x) => x.id === subTask.dataset.id);
if (c) c.done = !c.done;
}
renderAll(); save();
return;
}
const grpBtn = e.target.closest("[data-toggle-group]");
if (grpBtn) {
const g = sport.find((s) => s.id === grpBtn.dataset.toggleGroup);
if (g && g.children) {
const toSet = !g.children.every((c) => c.done);
g.children.forEach((c) => { c.done = toSet; });
}
renderAll(); save();
return;
}
const gBtn = e.target.closest("[data-grade]");
if (gBtn) {
const row = gBtn.closest("[data-id]");
const id = row.dataset.id;
const g = Number(gBtn.dataset.grade);
const item = lessons.find((l) => l.id === id);
if (item) { item.grade = item.grade === g ? 0 : g; }
renderAll(); save();
return;
}
const sBtn = e.target.closest("#panel-sport .kt-ai-btn");
if (sBtn) {
const row = sBtn.closest("[data-id]");
const item = sport.find((s) => s.id === row.dataset.id);
if (item) { item.done = !item.done; }
renderAll(); save();
}
const tBtn = e.target.closest("#panel-routine .kt-ai-btn");
if (tBtn) {
const row = tBtn.closest("[data-id]");
const item = routine.find((r) => r.id === row.dataset.id);
if (item) { item.done = !item.done; }
renderAll(); save();
return;
}
});
// (home-list обрабатывается в основном click-обработчике выше)
document.getElementById("btn-reset").addEventListener("click", () => {
if (!confirm("Сбросить все отметки за сегодня?")) return;
lessons = LESSONS.map((l) => ({ ...l, grade: 0 }));
sport = SPORT.map((s) => ({ ...s, done: false }));
home = HOME.map((t) => ({ ...t, done: false }));
routine = ROUTINE.map((r) => ({ ...r, done: false }));
renderAll(); save();
});
// ---------- Init ----------
load();
renderDate();
renderAll();