const iosEventsList = document.getElementById("ios-events-list"); const iosSyncStatus = document.getElementById("ios-sync-status"); const iosForm = document.getElementById("ios-event-form"); const iosCalGrid = document.getElementById("ios-cal-grid"); const iosMonthLabel = document.getElementById("ios-month-label"); const iosDayDetail = document.getElementById("ios-day-detail"); let iosEventsCache = []; /** @type {Date} mois affiché (jour ignoré) */ let iosMonthCursor = new Date(); let iosView = "month"; function escHtml(s) { const d = document.createElement("div"); d.textContent = s ?? ""; return d.innerHTML; } /** SQL datetime → valeur input datetime-local */ function sqlToDatetimeLocal(s) { if (!s) return ""; const m = /^(\d{4})-(\d{2})-(\d{2})[\sT](\d{2}):(\d{2})/.exec(String(s).trim()); if (!m) return ""; return `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}`; } /** datetime-local → SQL pour l’API */ function localDatetimeToSql(s) { if (!s) return ""; const t = s.includes("T") ? s.replace("T", " ") : s; return t.length === 16 ? `${t}:00` : t; } async function iosApi(action, options = {}) { const method = options.method || "GET"; const headers = { Accept: "application/json", "X-Requested-With": "XMLHttpRequest", ...(options.headers || {}), }; if (method !== "GET") { headers["X-CSRF-Token"] = window.CSRF_TOKEN || ""; headers["Content-Type"] = "application/json"; } const response = await fetch(`/modules/calendar-ios/api.php?action=${encodeURIComponent(action)}`, { method, credentials: "same-origin", headers, body: options.body ? JSON.stringify(options.body) : undefined, }); const json = await response.json(); if (!json.ok) throw new Error(json.error || "Erreur inconnue"); return json.data; } function formatDate(iso) { const d = new Date(String(iso).replace(" ", "T")); return Number.isNaN(d.getTime()) ? iso : d.toLocaleString(); } function fillForm(evt) { document.getElementById("ios-event-id").value = evt.id; document.getElementById("ios-title").value = evt.title || ""; document.getElementById("ios-location").value = evt.location || ""; document.getElementById("ios-description").value = evt.description || ""; document.getElementById("ios-start").value = sqlToDatetimeLocal(evt.start_at); document.getElementById("ios-end").value = sqlToDatetimeLocal(evt.end_at); } function resetForm() { iosForm.reset(); document.getElementById("ios-event-id").value = ""; } function eventsForLocalDay(y, m, d) { return iosEventsCache.filter((evt) => { const t = new Date(String(evt.start_at).replace(" ", "T")); return !Number.isNaN(t.getTime()) && t.getFullYear() === y && t.getMonth() === m && t.getDate() === d; }); } function renderMonthGrid() { if (!iosCalGrid) return; const y = iosMonthCursor.getFullYear(); const m = iosMonthCursor.getMonth(); if (iosMonthLabel) { iosMonthLabel.textContent = new Date(y, m, 1).toLocaleDateString("fr-FR", { month: "long", year: "numeric" }); } const first = new Date(y, m, 1); const last = new Date(y, m + 1, 0); let startWeekday = first.getDay() - 1; if (startWeekday < 0) startWeekday = 6; const frag = document.createDocumentFragment(); for (let i = 0; i < startWeekday; i++) { const c = document.createElement("div"); c.className = "ios-cal-cell ios-cal-cell--pad"; frag.appendChild(c); } for (let d = 1; d <= last.getDate(); d++) { const cell = document.createElement("button"); cell.type = "button"; cell.className = "ios-cal-cell"; const dayEvents = eventsForLocalDay(y, m, d); if (dayEvents.length) cell.classList.add("ios-cal-cell--has"); const today = new Date(); if (y === today.getFullYear() && m === today.getMonth() && d === today.getDate()) { cell.classList.add("ios-cal-cell--today"); } cell.innerHTML = `${d}`; if (dayEvents.length) { const dots = document.createElement("div"); dots.className = "ios-cal-dots"; dayEvents.slice(0, 4).forEach(() => { const dot = document.createElement("span"); dot.className = "ios-cal-dot"; dots.appendChild(dot); }); cell.appendChild(dots); } cell.addEventListener("click", () => showDayDetail(y, m, d)); frag.appendChild(cell); } iosCalGrid.innerHTML = ""; iosCalGrid.appendChild(frag); } function showDayDetail(y, mo, d) { if (!iosDayDetail) return; const list = eventsForLocalDay(y, mo, d); if (!list.length) { iosDayDetail.innerHTML = `Aucun événement le ${d}/${mo + 1}/${y}.`; return; } const label = new Date(y, mo, d).toLocaleDateString("fr-FR", { weekday: "long", day: "numeric", month: "long" }); iosDayDetail.innerHTML = `