"use client"; import { useState, useMemo } from "react"; import { CaretLeft, CaretRight, CheckCircle, PencilSimple, Clock, User, Buildings } from '@/lib/icons'; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import type { Activity, Customer, Property } from "@/lib/appwrite/schema"; import { ACTIVITY_TYPE_LABELS } from "@/lib/appwrite/schema"; const DAYS = ["Pzt", "Sal", "Çar", "Per", "Cum", "Cmt", "Paz"]; const MONTHS = [ "Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık", ]; const TYPE_COLORS: Record = { gorusme: "bg-blue-500", teklif: "bg-amber-500", ziyaret: "bg-emerald-500", arama: "bg-purple-500", not: "bg-gray-400", }; const TYPE_BADGE_COLORS: Record = { gorusme: "bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300", teklif: "bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300", ziyaret: "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300", arama: "bg-purple-100 text-purple-700 dark:bg-purple-900/40 dark:text-purple-300", not: "bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-300", }; function toDateKey(date: Date): string { return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`; } function activityDateKey(a: Activity): string | null { if (!a.dueDate) return null; const d = new Date(a.dueDate); if (isNaN(d.getTime())) return null; return toDateKey(d); } interface Props { activities: Activity[]; customers: Customer[]; properties: Property[]; onEdit: (a: Activity) => void; onComplete: (a: Activity) => void; } export function ActivityCalendar({ activities, customers, properties, onEdit, onComplete }: Props) { const today = new Date(); const [year, setYear] = useState(today.getFullYear()); const [month, setMonth] = useState(today.getMonth()); const [selectedKey, setSelectedKey] = useState(toDateKey(today)); function prevMonth() { if (month === 0) { setMonth(11); setYear(y => y - 1); } else setMonth(m => m - 1); } function nextMonth() { if (month === 11) { setMonth(0); setYear(y => y + 1); } else setMonth(m => m + 1); } // Index activities by date key const byDate = useMemo(() => { const map: Record = {}; for (const a of activities) { const key = activityDateKey(a); if (!key) continue; if (!map[key]) map[key] = []; map[key].push(a); } return map; }, [activities]); // Build calendar grid const cells = useMemo(() => { const firstDay = new Date(year, month, 1); // Monday-based: 0=Mon … 6=Sun let startOffset = firstDay.getDay() - 1; if (startOffset < 0) startOffset = 6; const daysInMonth = new Date(year, month + 1, 0).getDate(); const totalCells = Math.ceil((startOffset + daysInMonth) / 7) * 7; const result: Array<{ date: Date | null; key: string | null }> = []; for (let i = 0; i < totalCells; i++) { const dayNum = i - startOffset + 1; if (dayNum < 1 || dayNum > daysInMonth) { result.push({ date: null, key: null }); } else { const d = new Date(year, month, dayNum); result.push({ date: d, key: toDateKey(d) }); } } return result; }, [year, month]); const selectedActivities = selectedKey ? (byDate[selectedKey] ?? []) : []; const selectedDate = selectedKey ? new Date(selectedKey + "T12:00:00") : null; function customerName(id?: string | null) { if (!id) return null; return customers.find((c) => c.$id === id)?.name ?? null; } function propertyTitle(id?: string | null) { if (!id) return null; return properties.find((p) => p.$id === id)?.title ?? null; } const todayKey = toDateKey(today); return (
{/* === Calendar Grid === */}
{/* Month nav */}
{MONTHS[month]} {year}
{/* Day headers */}
{DAYS.map((d) => (
{d}
))}
{/* Day cells */}
{cells.map((cell, i) => { if (!cell.date || !cell.key) { return
; } const cellActivities = byDate[cell.key] ?? []; const isToday = cell.key === todayKey; const isSelected = cell.key === selectedKey; const visible = cellActivities.slice(0, 3); const overflow = cellActivities.length - 3; return ( ); })}
{/* Legend */}
{Object.entries(ACTIVITY_TYPE_LABELS).map(([key, label]) => (
{label}
))}
{/* === Day Panel === */}
{/* Panel header */}
{selectedDate ? (

{selectedDate.toLocaleDateString("tr-TR", { weekday: "long", day: "numeric", month: "long" })}

{selectedActivities.length === 0 ? "Bu gün için aktivite yok" : `${selectedActivities.length} aktivite`}

) : (

Gün seçin

)}
{/* Activity list */}
{selectedActivities.length === 0 ? (
Aktivite bulunmuyor
) : (
{selectedActivities.map((a) => ( ))}
)}
); } interface CardProps { activity: Activity; customerName: string | null; propertyTitle: string | null; typeBadgeColor: string; onEdit: (a: Activity) => void; onComplete: (a: Activity) => void; } function ActivityCard({ activity: a, customerName, propertyTitle, typeBadgeColor, onEdit, onComplete }: CardProps) { return (
{ACTIVITY_TYPE_LABELS[a.type] ?? a.type} {a.completedAt ? ( Tamam ) : ( Açık )}

{a.title}

{a.description && (

{a.description}

)}
{customerName && (
{customerName}
)} {propertyTitle && (
{propertyTitle}
)}
{!a.completedAt && ( )}
); }