/* ================= WEB ALIGNMENT TEAM (Mondays 9:00 AM MT) ================= */
const WA_KEY = "sm-webalign-items-v1";
let waItems = [];
try { waItems = JSON.parse(localStorage.getItem(WA_KEY) || "[]"); } catch { waItems = []; }
function waSave() { localStorage.setItem(WA_KEY, JSON.stringify(waItems)); }
function waNextMeeting() {
const now = new Date();
const m = new Date(now); m.setHours(9, 0, 0, 0);
let add = (1 - now.getDay() + 7) % 7; // days until Monday
if (add === 0 && now >= m) add = 7; // Monday but the 9 AM slot is gone β next week
m.setDate(m.getDate() + add);
return m;
}
function renderWa() {
const now = new Date();
const m = waNextMeeting();
const tStr = m.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
const dStr = m.toLocaleDateString(undefined, { weekday: "long", month: "long", day: "numeric" });
const sameDay = m.toDateString() === now.toDateString();
const dayOnly = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const days = Math.round((m - dayOnly) / 86400000);
const when = sameDay
? `TODAY at ${tStr} (in ${Math.max(1, Math.round((m - now) / 60000))} min)`
: `${dStr} at ${tStr} Β· in ${days} day${days === 1 ? "" : "s"}`;
const el = document.getElementById("waWhen");
if (el) {
el.innerHTML = `π Next sync: ${when} Β· Mondays 9:00 AM MT`;
el.classList.toggle("today", sameDay);
}
const wrap = document.getElementById("waList");
const open = waItems.filter(i => !i.done);
const covered = waItems.filter(i => i.done);
const row = i => {
const idx = waItems.indexOf(i);
const subs = i.subs || [];
const subDone = subs.filter(s => s.done).length;
const subsRows = subs.map((s, si) =>
``).join("");
return `
${subsRows}
`;
};
let html = open.length
? open.map(row).join("")
: 'Nothing queued for Monday yet β add what you need to raise above π
';
if (covered.length) html += `β
Covered ${covered.length}
` + covered.map(row).join("");
wrap.innerHTML = html;
const b = document.getElementById("waBadge");
if (b) b.textContent = open.length ? String(open.length) : "β";
}
function addWaItem() {
const inp = document.getElementById("waInput");
const text = inp.value.trim();
if (!text) return;
waItems.push({ text, done: false, added: new Date().toLocaleDateString(undefined, { month: "short", day: "numeric" }) });
inp.value = "";
waSave(); renderWa(); inp.focus();
}
function waToggle(idx) { waItems[idx].done = !waItems[idx].done; waSave(); renderWa(); }
function waSubToggle(idx, si) { waItems[idx].subs[si].done = !waItems[idx].subs[si].done; waSave(); renderWa(); }
function waSubRemove(idx, si) { waItems[idx].subs.splice(si, 1); waSave(); renderWa(); }
function waShowSubInput(idx) {
const wrap = document.getElementById("waSubs" + idx);
if (!wrap || wrap.querySelector(".wa-subadd input")) return;
const d = document.createElement("div");
d.className = "wa-subadd";
const inp = document.createElement("input");
inp.type = "text";
inp.placeholder = "Sub-item / talking point⦠(Enter to add, Esc to cancel)";
d.appendChild(inp);
wrap.appendChild(d);
inp.focus();
let done = false;
inp.addEventListener("keydown", e => {
if (e.key === "Enter") {
const v = inp.value.trim();
done = true;
if (v) {
if (!waItems[idx].subs) waItems[idx].subs = [];
waItems[idx].subs.push({ text: v, done: false });
waSave(); renderWa(); waShowSubInput(idx); // stay open for the next one
}
} else if (e.key === "Escape") { done = true; d.remove(); }
});
inp.addEventListener("blur", () => { setTimeout(() => { if (!done && d.parentNode) d.remove(); }, 150); });
}
function waRemove(idx) { if (confirm("Remove this item?")) { waItems.splice(idx, 1); waSave(); renderWa(); } }
function waClearCovered() {
const n = waItems.filter(i => i.done).length;
if (!n) return;
if (!confirm(`Remove the ${n} covered item${n === 1 ? "" : "s"}?`)) return;
waItems = waItems.filter(i => !i.done);
waSave(); renderWa();
}
renderWa();