import { useMemo, useState } from 'preact/hooks'; import { getStatusLabel } from '../../stores/taskStore'; import LeftSlideDrawer from '../layout/LeftSlideDrawer'; import { getTaskCraneKind, getTaskCraneOwner, getTaskCraneSerial } from '../../lib/taskCraneDisplay'; const WEEKDAY_LABELS = ['Po', 'Ut', 'Sr', 'Če', 'Pe', 'Su', 'Ne']; const MONTH_NAMES = [ 'Siječanj', 'Veljača', 'Ožujak', 'Travanj', 'Svibanj', 'Lipanj', 'Srpanj', 'Kolovoz', 'Rujan', 'Listopad', 'Studeni', 'Prosinac', ]; function toIsoDateKey(dateStr) { if (!dateStr) return null; const d = new Date(dateStr); if (Number.isNaN(d.getTime())) return null; return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; } function formatDayLabel(year, month, day) { return `${String(day).padStart(2, '0')}.${String(month + 1).padStart(2, '0')}.${year}`; } export default function TaskCalendarWidget({ tasks = [], notes = [], onTaskClick }) { const [viewYear, setViewYear] = useState(() => new Date().getFullYear()); const [viewMonth, setViewMonth] = useState(() => new Date().getMonth()); const [selectedDay, setSelectedDay] = useState(null); // Map: "YYYY-MM-DD" -> task[] const tasksByDate = useMemo(() => { const map = {}; for (const task of tasks) { const key = toIsoDateKey(task.scheduled_date); if (!key) continue; if (!map[key]) map[key] = []; map[key].push(task); } return map; }, [tasks]); const notesByDate = useMemo(() => { const map = {}; for (const note of notes) { const key = toIsoDateKey(note.note_date); if (!key) continue; if (!map[key]) map[key] = []; map[key].push(note); } return map; }, [notes]); // Calendar grid cells for current month const calendarDays = useMemo(() => { const firstDay = new Date(viewYear, viewMonth, 1); let startDow = firstDay.getDay(); // 0=Sun startDow = (startDow + 6) % 7; // shift to Mon=0 const daysInMonth = new Date(viewYear, viewMonth + 1, 0).getDate(); const cells = []; for (let i = 0; i < startDow; i++) cells.push(null); for (let d = 1; d <= daysInMonth; d++) cells.push(d); return cells; }, [viewYear, viewMonth]); const selectedDayKey = selectedDay ? `${viewYear}-${String(viewMonth + 1).padStart(2, '0')}-${String(selectedDay).padStart(2, '0')}` : null; const selectedDayTasks = selectedDayKey ? (tasksByDate[selectedDayKey] ?? []) : []; const selectedDayNotes = selectedDayKey ? (notesByDate[selectedDayKey] ?? []) : []; const today = new Date(); function prevMonth() { setSelectedDay(null); if (viewMonth === 0) { setViewYear((y) => y - 1); setViewMonth(11); } else setViewMonth((m) => m - 1); } function nextMonth() { setSelectedDay(null); if (viewMonth === 11) { setViewYear((y) => y + 1); setViewMonth(0); } else setViewMonth((m) => m + 1); } function handleDayClick(day) { setSelectedDay((prev) => (prev === day ? null : day)); } function handleTaskClick(task, closePanel) { onTaskClick?.(task); closePanel?.(); } const totalScheduled = Object.values(tasksByDate).flat().length; const totalNoteReminders = Object.values(notesByDate).flat().length; return ( {({ close }) => ( <> {/* Month navigation */}
{MONTH_NAMES[viewMonth]} {viewYear}
{/* Calendar grid */}
{WEEKDAY_LABELS.map((label) => (
{label}
))}
{calendarDays.map((day, idx) => { if (!day) return
; const key = `${viewYear}-${String(viewMonth + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`; const hasTasks = Boolean(tasksByDate[key]?.length); const hasNotes = Boolean(notesByDate[key]?.length); const taskCount = tasksByDate[key]?.length ?? 0; const noteCount = notesByDate[key]?.length ?? 0; const isToday = today.getFullYear() === viewYear && today.getMonth() === viewMonth && today.getDate() === day; const isSelected = selectedDay === day; return ( ); })}
{/* Divider */}
{/* Task list for selected day OR hint */}
{selectedDay ? ( <>

{formatDayLabel(viewYear, viewMonth, selectedDay)}

{selectedDayTasks.length === 0 && selectedDayNotes.length === 0 ? (

Nema događaja za odabrani datum.

) : (
{selectedDayNotes.length > 0 && (

Bilješke

    {selectedDayNotes.map((note) => (
  • {note.note} {note.task_title || note.work_order_label || 'Servisni kontekst'}
  • ))}
)} {selectedDayTasks.length > 0 && (

Zadaci

    {selectedDayTasks.map((task) => (
  • ))}
)}
)} ) : ( <>

Odaberite datum u kalendaru za prikaz zadataka.

{totalScheduled > 0 && (

{totalScheduled} zadatak/a s planiranim datumom.

)} {totalNoteReminders > 0 && (

{totalNoteReminders} bilješki/podsjetnika u kalendaru.

)} {totalScheduled === 0 && (

Nijedan zadatak nema postavljen planirani datum.

)} )}
)} ); }