'use client'; import { useEffect, useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import Image from 'next/image'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; import zhifubaoImg from "../assets/zhifubao.jpg"; import weixinImg from "../assets/weixin.jpg"; import { CalendarIcon, MinusIcon, PlusIcon } from './Icons'; dayjs.extend(utc); dayjs.extend(timezone); const DEFAULT_TZ = 'Asia/Shanghai'; const getBrowserTimeZone = () => { if (typeof Intl !== 'undefined' && Intl.DateTimeFormat) { const tz = Intl.DateTimeFormat().resolvedOptions().timeZone; return tz || DEFAULT_TZ; } return DEFAULT_TZ; }; const TZ = getBrowserTimeZone(); dayjs.tz.setDefault(TZ); const nowInTz = () => dayjs().tz(TZ); const toTz = (input) => (input ? dayjs.tz(input, TZ) : nowInTz()); const formatDate = (input) => toTz(input).format('YYYY-MM-DD'); export function DatePicker({ value, onChange, position = 'bottom' }) { const [isOpen, setIsOpen] = useState(false); const [currentMonth, setCurrentMonth] = useState(() => value ? toTz(value) : nowInTz()); useEffect(() => { const close = () => setIsOpen(false); if (isOpen) window.addEventListener('click', close); return () => window.removeEventListener('click', close); }, [isOpen]); const year = currentMonth.year(); const month = currentMonth.month(); const handlePrevMonth = (e) => { e.stopPropagation(); setCurrentMonth(currentMonth.subtract(1, 'month').startOf('month')); }; const handleNextMonth = (e) => { e.stopPropagation(); setCurrentMonth(currentMonth.add(1, 'month').startOf('month')); }; const handleSelect = (e, day) => { e.stopPropagation(); const dateStr = formatDate(`${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`); const today = nowInTz().startOf('day'); const selectedDate = toTz(dateStr).startOf('day'); if (selectedDate.isAfter(today)) return; onChange(dateStr); setIsOpen(false); }; const daysInMonth = currentMonth.daysInMonth(); const firstDayOfWeek = currentMonth.startOf('month').day(); const days = []; for (let i = 0; i < firstDayOfWeek; i++) days.push(null); for (let i = 1; i <= daysInMonth; i++) days.push(i); return (