"use client"; import Link from "next/link"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { commissionIncomeDay, commissionIncomeShares } from "@/lib/commission/income"; type IncomeRow = { ticketId: string; title: string | null; creatorName: string; amountBaht: number; transferredAt: Date }; type Period = { month: string; year: number; number: number; previous: string; next: string; days: number; leadingDays: number }; const weekdays = ["จ.", "อ.", "พ.", "พฤ.", "ศ.", "ส.", "อา."]; const money = (satang: number) => `฿${(satang / 100).toLocaleString("th-TH", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; const time = new Intl.DateTimeFormat("th-TH", { timeZone: "Asia/Bangkok", hour: "2-digit", minute: "2-digit" }); const date = new Intl.DateTimeFormat("th-TH", { timeZone: "Asia/Bangkok", day: "numeric", month: "long", year: "numeric" }); function Totals({ amount }: { amount: number }) { const shares = commissionIncomeShares(amount); return รวม {money(amount * 100)} - Buzz {money(shares.buzzSatang)} - Gunshin {money(shares.gunshinSatang)}; } export function CommissionIncomeCalendar({ period, rows }: { period: Period; rows: IncomeRow[] }) { const byDay = new Map(); for (const row of rows) { const day = commissionIncomeDay(row.transferredAt); const list = byDay.get(day) ?? []; list.push(row); byDay.set(day, list); } const total = rows.reduce((sum, row) => sum + row.amountBaht, 0); const monthLabel = new Intl.DateTimeFormat("th-TH", { month: "long", year: "numeric", timeZone: "UTC" }) .format(new Date(Date.UTC(period.year, period.number - 1, 1))); return <>

{weekdays.map((day) =>
{day}
)} {Array.from({ length: period.leadingDays }, (_, index) =>
)} {Array.from({ length: period.days }, (_, index) => { const day = index + 1; const tickets = byDay.get(day) ?? []; const dayTotal = tickets.reduce((sum, row) => sum + row.amountBaht, 0); const label = date.format(new Date(`${period.month}-${String(day).padStart(2, "0")}T12:00:00+07:00`)); if (tickets.length === 0) return
{day}
; return }> {day} {tickets.length} tickets {money(dayTotal * 100)} {label}

{tickets.map((row) =>
{row.title || `Ticket #${row.ticketId.slice(0, 8)}`}

สร้างโดย {row.creatorName}

{money(row.amountBaht * 100)}
)}
; })}
; }