// time-selector.js - Управление выбором даты и времени для задач // Функция для установки времени задачи (создание) function setTaskTime(time) { const timeButtons = document.querySelectorAll('.time-btn'); const hiddenTimeInput = document.getElementById('due-time'); // Убираем активный класс со всех кнопок timeButtons.forEach(btn => { btn.classList.remove('active'); }); // Добавляем активный класс к нажатой кнопке const activeButton = Array.from(timeButtons).find(btn => { if (time === '12:00') { return btn.textContent.includes('До обеда'); } else { return btn.textContent.includes('После обеда'); } }); if (activeButton) { activeButton.classList.add('active'); } // Устанавливаем значение в скрытое поле hiddenTimeInput.value = time; // Обновляем отображение updateDateTimeDisplay(); } // Функция для установки времени задачи (редактирование) function setEditTaskTime(time) { const timeButtons = document.querySelectorAll('.edit-time-btn'); const hiddenTimeInput = document.getElementById('edit-due-time'); // Убираем активный класс со всех кнопок timeButtons.forEach(btn => { btn.classList.remove('active'); }); // Добавляем активный класс к нажатой кнопке const activeButton = Array.from(timeButtons).find(btn => { if (time === '12:00') { return btn.textContent.includes('До обеда'); } else { return btn.textContent.includes('После обеда'); } }); if (activeButton) { activeButton.classList.add('active'); } // Устанавливаем значение в скрытое поле hiddenTimeInput.value = time; // Обновляем отображение updateEditDateTimeDisplay(); } // Функция для обновления отображения даты и времени (создание) function updateDateTimeDisplay() { const dateInput = document.getElementById('due-date'); const timeInput = document.getElementById('due-time'); const timeButtons = document.querySelectorAll('.time-btn'); if (dateInput.value && timeInput.value) { // Обновляем текст кнопок с полной датой const selectedDate = new Date(dateInput.value); const formattedDate = selectedDate.toLocaleDateString('ru-RU', { weekday: 'short', day: 'numeric', month: 'short' }); timeButtons.forEach(btn => { const timeText = btn.textContent.includes('До обеда') ? 'До обеда' : 'После обеда'; btn.innerHTML = ` ${timeText} (${timeInput.value})`; }); } } // Функция для обновления отображения даты и времени (редактирование) function updateEditDateTimeDisplay() { const dateInput = document.getElementById('edit-due-date'); const timeInput = document.getElementById('edit-due-time'); const timeButtons = document.querySelectorAll('.edit-time-btn'); if (dateInput.value && timeInput.value) { timeButtons.forEach(btn => { const timeText = btn.textContent.includes('До обеда') ? 'До обеда' : 'После обеда'; btn.innerHTML = ` ${timeText} (${timeInput.value})`; }); } } // Функция для инициализации выбора времени function initializeTimeSelectors() { // Устанавливаем сегодняшнюю дату по умолчанию для создания задачи const today = new Date(); const tomorrow = new Date(); tomorrow.setDate(today.getDate() + 1); // Форматируем дату для input[type="date"] const formattedDate = tomorrow.toISOString().split('T')[0]; const dueDateInput = document.getElementById('due-date'); if (dueDateInput) { dueDateInput.value = formattedDate; // Устанавливаем время по умолчанию (12:00) document.getElementById('due-time').value = '19:00'; // Добавляем активный класс к первой кнопке const timeButtons = document.querySelectorAll('.time-btn'); if (timeButtons.length > 0) { timeButtons[0].classList.add('active'); } // Добавляем обработчик изменения даты dueDateInput.addEventListener('change', function() { updateDateTimeDisplay(); }); // Инициализируем отображение updateDateTimeDisplay(); } // Инициализация для редактирования const editDueDateInput = document.getElementById('edit-due-date'); if (editDueDateInput) { editDueDateInput.addEventListener('change', function() { updateEditDateTimeDisplay(); }); } } // Функция для форматирования полной даты из отдельных полей function getFullDateTime(dateInputId, timeInputId) { const dateValue = document.getElementById(dateInputId).value; const timeValue = document.getElementById(timeInputId).value; if (!dateValue || !timeValue) { return null; } return `${dateValue}T${timeValue}:00`; } // Функция для установки даты и времени в форму редактирования function setDateTimeForEdit(taskDueDate) { if (!taskDueDate) return; const dateTime = new Date(taskDueDate); const dateStr = dateTime.toISOString().split('T')[0]; const hours = dateTime.getHours().toString().padStart(2, '0'); const minutes = dateTime.getMinutes().toString().padStart(2, '0'); const timeStr = `${hours}:${minutes}`; // Устанавливаем значения полей document.getElementById('edit-due-date').value = dateStr; document.getElementById('edit-due-time').value = timeStr; // Устанавливаем активную кнопку времени const timeButtons = document.querySelectorAll('.edit-time-btn'); timeButtons.forEach(btn => btn.classList.remove('active')); // Определяем, какая кнопка должна быть активна const isBeforeLunch = parseInt(hours) < 12 || (parseInt(hours) === 12 && parseInt(minutes) === 0); const activeButton = isBeforeLunch ? document.querySelector('.edit-time-btn:first-child') : document.querySelector('.edit-time-btn:last-child'); if (activeButton) { activeButton.classList.add('active'); // Обновляем текст кнопки const timeText = isBeforeLunch ? 'До обеда' : 'После обеда'; activeButton.innerHTML = ` ${timeText} (${timeStr})`; // Обновляем другую кнопку const otherButton = isBeforeLunch ? document.querySelector('.edit-time-btn:last-child') : document.querySelector('.edit-time-btn:first-child'); const otherTimeText = isBeforeLunch ? 'После обеда' : 'До обеда'; const otherTimeValue = isBeforeLunch ? '19:00' : '12:00'; otherButton.innerHTML = ` ${otherTimeText} (${otherTimeValue})`; } }