diff --git a/frontend/admin/static/script.js b/frontend/admin/static/script.js index 165c985..d25e91b 100644 --- a/frontend/admin/static/script.js +++ b/frontend/admin/static/script.js @@ -19,56 +19,153 @@ function getWeekStart(dateStr = null) { if (!dateStr) { dateStr = getTodayInLondon(); } - const date = new Date(dateStr + 'T00:00:00'); - const day = date.getDay(); - const diff = date.getDate() - day + (day === 0 ? -6 : 1); // Понедельник = 1 - const monday = new Date(date.setDate(diff)); - return monday.toISOString().split('T')[0]; + // Создаем дату из строки YYYY-MM-DD в локальном времени + const [year, month, day] = dateStr.split('-').map(Number); + const date = new Date(year, month - 1, day); + // getDay() возвращает 0=воскресенье, 1=понедельник, ..., 6=суббота + // Находим понедельник текущей недели + const dayOfWeek = date.getDay(); + // Если воскресенье (0), идем на 6 дней назад, иначе на (dayOfWeek-1) дней назад + const diff = dayOfWeek === 0 ? -6 : -(dayOfWeek - 1); + const monday = new Date(date); + monday.setDate(date.getDate() + diff); + // Проверяем, что получился понедельник + if (monday.getDay() !== 1) { + console.error('Error: getWeekStart did not return Monday!', monday.getDay()); + } + // Возвращаем в формате YYYY-MM-DD + const yearStr = monday.getFullYear(); + const monthStr = String(monday.getMonth() + 1).padStart(2, '0'); + const dayStr = String(monday.getDate()).padStart(2, '0'); + return `${yearStr}-${monthStr}-${dayStr}`; } function formatDate(dateStr) { - const date = new Date(dateStr + 'T00:00:00'); - return `${weekdaysShort[date.getDay()]}, ${date.getDate()}`; + // Создаем дату из строки YYYY-MM-DD в локальном времени + const [year, month, day] = dateStr.split('-').map(Number); + const date = new Date(year, month - 1, day); + // getDay() возвращает 0=воскресенье, 1=понедельник, ..., 6=суббота + // Конвертируем в формат где 0=понедельник, 6=воскресенье + const jsDay = date.getDay(); + const weekdayIndex = jsDay === 0 ? 6 : jsDay - 1; // 0=пн, 6=вс + return `${weekdaysShort[weekdayIndex]}, ${date.getDate()}`; } function formatDateFull(dateStr) { - const date = new Date(dateStr + 'T00:00:00'); + // Создаем дату из строки YYYY-MM-DD в локальном времени + const [year, month, day] = dateStr.split('-').map(Number); + const date = new Date(year, month - 1, day); const months = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря']; - return `${weekdays[date.getDay()]}, ${date.getDate()} ${months[date.getMonth()]}`; + // getDay() возвращает 0=воскресенье, 1=понедельник, ..., 6=суббота + // Конвертируем в формат где 0=понедельник, 6=воскресенье + const jsDay = date.getDay(); + const weekdayIndex = jsDay === 0 ? 6 : jsDay - 1; // 0=пн, 6=вс + return `${weekdays[weekdayIndex]}, ${date.getDate()} ${months[date.getMonth()]}`; } function getWeekDates(startDate) { + // startDate должен быть понедельником (в формате YYYY-MM-DD) const dates = []; - const start = new Date(startDate + 'T00:00:00'); + // Создаем дату из строки в локальном времени + const [year, month, day] = startDate.split('-').map(Number); + const start = new Date(year, month - 1, day); + // Убеждаемся, что это понедельник + const dayOfWeek = start.getDay(); + if (dayOfWeek !== 1) { + // Если не понедельник, находим понедельник + const diff = dayOfWeek === 0 ? -6 : -(dayOfWeek - 1); + start.setDate(start.getDate() + diff); + } + // Возвращаем 7 дней: понедельник - воскресенье + const baseDate = new Date(start); for (let i = 0; i < 7; i++) { - const date = new Date(start); - date.setDate(start.getDate() + i); - dates.push(date.toISOString().split('T')[0]); + const date = new Date(baseDate); + date.setDate(baseDate.getDate() + i); + // Форматируем в YYYY-MM-DD + const yearStr = date.getFullYear(); + const monthStr = String(date.getMonth() + 1).padStart(2, '0'); + const dayStr = String(date.getDate()).padStart(2, '0'); + dates.push(`${yearStr}-${monthStr}-${dayStr}`); + } + // Проверяем, что первый день - понедельник, последний - воскресенье + const [firstYear, firstMonth, firstDayNum] = dates[0].split('-').map(Number); + const firstDate = new Date(firstYear, firstMonth - 1, firstDayNum); + const [lastYear, lastMonth, lastDayNum] = dates[6].split('-').map(Number); + const lastDate = new Date(lastYear, lastMonth - 1, lastDayNum); + const firstDay = firstDate.getDay(); + const lastDay = lastDate.getDay(); + if (firstDay !== 1 || lastDay !== 0) { + console.error('Error: Week should start on Monday and end on Sunday!', { + first: dates[0], firstDay, + last: dates[6], lastDay + }); } return dates; } async function loadSchedule() { + // Убеждаемся, что currentWeekStart - это понедельник + if (!currentWeekStart) { + currentWeekStart = getWeekStart(); + } else { + currentWeekStart = getWeekStart(currentWeekStart); + } + const weekDates = getWeekDates(currentWeekStart); const fromDate = weekDates[0]; const toDate = weekDates[6]; - console.log('Loading schedule from', fromDate, 'to', toDate); + // Проверяем, что неделя начинается с понедельника и заканчивается воскресеньем + const [mondayYear, mondayMonth, mondayDay] = weekDates[0].split('-').map(Number); + const monday = new Date(mondayYear, mondayMonth - 1, mondayDay); + const [sundayYear, sundayMonth, sundayDay] = weekDates[6].split('-').map(Number); + const sunday = new Date(sundayYear, sundayMonth - 1, sundayDay); + if (monday.getDay() !== 1 || sunday.getDay() !== 0) { + console.error('Week should start on Monday and end on Sunday! Fixing...', { + monday: weekDates[0], mondayDay: monday.getDay(), + sunday: weekDates[6], sundayDay: sunday.getDay() + }); + // Исправляем автоматически без рекурсии + currentWeekStart = getWeekStart(); + const correctedWeekDates = getWeekDates(currentWeekStart); + // Используем исправленные даты напрямую + const correctedFromDate = correctedWeekDates[0]; + const correctedToDate = correctedWeekDates[6]; + + try { + const response = await fetch(`/api/schedule?from_date=${correctedFromDate}&to_date=${correctedToDate}`); + const data = await response.json(); + renderSchedule(correctedWeekDates, data.items); + document.getElementById('week-range').textContent = + `${formatDateFull(correctedWeekDates[0])} - ${formatDateFull(correctedWeekDates[6])}`; + updateWeekNavigationButtons(); + } catch (error) { + console.error('Error loading schedule:', error); + } + return; + } try { const response = await fetch(`/api/schedule?from_date=${fromDate}&to_date=${toDate}`); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } const data = await response.json(); - console.log('Received items:', data.items); + if (!data.items) { + console.error('Invalid response format:', data); + return; + } renderSchedule(weekDates, data.items); // Обновляем заголовок недели - const startDate = new Date(weekDates[0] + 'T00:00:00'); - const endDate = new Date(weekDates[6] + 'T00:00:00'); document.getElementById('week-range').textContent = `${formatDateFull(weekDates[0])} - ${formatDateFull(weekDates[6])}`; + + // Обновляем кнопки навигации + updateWeekNavigationButtons(); } catch (error) { console.error('Error loading schedule:', error); } @@ -76,21 +173,14 @@ async function loadSchedule() { function renderSchedule(weekDates, items) { const grid = document.getElementById('schedule-grid'); + if (!grid) { + console.error('Schedule grid not found!'); + return; + } grid.innerHTML = ''; - console.log('Rendering schedule for dates:', weekDates); - console.log('All items:', items); - weekDates.forEach(dateStr => { - const dayItems = items.filter(item => { - const match = item.date === dateStr; - if (!match && items.length > 0) { - console.log(`Item date ${item.date} !== ${dateStr}`); - } - return match; - }); - console.log(`Date ${dateStr}: ${dayItems.length} items`); - const date = new Date(dateStr + 'T00:00:00'); + const dayItems = items.filter(item => item.date === dateStr); const column = document.createElement('div'); column.className = 'day-column'; @@ -107,14 +197,12 @@ function renderSchedule(weekDates, items) { } function renderDayItems(items) { - console.log('Rendering items for day:', items); if (items.length === 0) { return '