"use client"; import { format, getDaysInMonth, startOfMonth } from "date-fns"; import { Check, ChevronLeft, ChevronRight, Circle, ExternalLink, ImageOff, ReceiptText, Trash2, } from "lucide-react"; import Image from "next/image"; import Link from "next/link"; import { useMemo, useTransition } from "react"; import { toast } from "sonner"; import { SimpleTooltip } from "@/components/tooltip"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Dialog, DialogClose, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; import { toggleMonth } from "../../api"; type Row = { id: string; name: string; queue: number; checked: boolean; server: "as" | "eu" | "us" | "tw"; expires: Date | null; price: number; service: string[]; slip: { id: string; ref: string; amount: string; data: unknown; } | null; paid: boolean; deleted: boolean; submit_day: Date; user: { uid: string; display: string; username: string; token: string; } | null; }; const DAYS = ["จ.", "อ.", "พ.", "พฤ.", "ศ.", "ส.", "อา."]; export function CalendarClient({ year, month, rows, total, monthly, }: { year: number; month: number; rows: Row[]; total: number; monthly: Record; }) { const current = new Date(year, month); const rowsByDay = useMemo(() => { const map: Record = {}; for (const r of rows) { if (!r.submit_day) continue; const d = new Date(r.submit_day).getDate(); (map[d] ??= []).push(r); } return map; }, [rows]); const ym = `${year}-${String(month + 1).padStart(2, "0")}`; const [pending, startTransition] = useTransition(); const monthChecked = monthly[ym] ?? false; const daysInMonth = getDaysInMonth(current); const firstDayOfWeek = startOfMonth(current).getDay(); // 0=Sun const monthLabel = format(current, "MMMM yyyy"); const prevY = month === 0 ? year - 1 : year; const prevM = month === 0 ? 11 : month - 1; const nextY = month === 11 ? year + 1 : year; const nextM = month === 11 ? 0 : month + 1; const prevHref = `/rubgram/admin/calendar?month=${prevY}-${String(prevM + 1).padStart(2, "0")}`; const nextHref = `/rubgram/admin/calendar?month=${nextY}-${String(nextM + 1).padStart(2, "0")}`; return (
{monthLabel}
รวม{" "} {total.toLocaleString()} {" "} บาท
{DAYS.map((d) => (
{d}
))} {/* some hack for month start */} {Array.from({ length: firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1, }).map((_, i) => (
))} {Array.from({ length: daysInMonth }, (_, i) => i + 1).map((day) => { const dayRows = rowsByDay[day] || []; const dayTotal = dayRows.reduce((s, r) => s + r.price, 0); const hasData = dayRows.length > 0; return ( {format( new Date(current.getFullYear(), current.getMonth(), day), "d MMM yyyy", )}
{dayRows.length === 0 && (

ไม่มีคิวในวันนี้

)} {dayRows.map((r) => (
{r.slip ? (
Slip
) : r.price <= 0 ? (
) : (
)}
{r.queue}. {r.name} {r.deleted && ( ลบแล้ว )}
{r.service.join(", ")}
{r.price} ฿ {r.user && ( {r.user.display} )} {r.slip && ( สลิป
Slip
)}
))}
รวม {dayTotal.toLocaleString()} บาท
); })}
); }